数据结构//C——动态分配—离散存储(非循环单链表)2

# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
# include <stdbool.h>
//  # include <string.h>  strcpy(q->name, "张三")

typedef struct Student{
    char name[100];
    int id;
    struct Student * pNext;
}Stu;

Stu * Creat_list();
void Show_list(Stu *);
bool bool_empty_list(Stu *);
int length_list(Stu *);
bool insert_list(Stu *, int);
bool delete_list(Stu *, int);
bool deleteID_list(Stu *, int);//                         根据 学号 删除信息写不出来
void sort_list(Stu *);

int main (void){

    Stu * pHead;
    pHead = Creat_list();
    Show_list(pHead);

//    insert_list(pHead,0);
//    delete_list(pHead,1);
//    deleteID_list(pHead, 1);

    printf("学生人数:%d位\n", length_list(pHead));
//    sort_list(pHead);
    Show_list(pHead);

    return 0;
}
Stu * Creat_list(){
    Stu *pList;
    Stu * pHead = (Stu *) malloc(sizeof(Stu));

    int len;
    printf("请输入学生人数:");
    scanf("%d", &len);
    if(pHead->pNext == NULL || len == 0){
        printf("录入失败!\n");
        exit(-1);
    }
    pHead->pNext = NULL;
    pList = pHead;

    for (int i = 0; i < len; ++i){
        Stu * pNew = (Stu *) malloc(sizeof(Stu));

        printf("第%d名学生信息:\n", i+1);
        printf("姓名:");
        scanf("%s", pNew->name);
        printf("学号:");
        scanf("%d",&(pNew->id));

        pList->pNext = pNew;
        pNew->pNext = NULL;
        pList = pNew;
    }
    return pHead;
}
void Show_list(Stu * pHead){
    Stu * q = pHead->pNext;
    if(q != NULL){
        printf("学生信息如下:\n");
    } else{
        printf("无信息!");
    }
    while (q != NULL){
        printf("姓名:%s  学号:%d\n", q->name, q->id);
        q = q->pNext;
    }

}
bool bool_empty_list(Stu * pHead){
    if(pHead->pNext == NULL){
        printf("无信息,无法遍历!");
        return true;
    } else{
        return false;
    }
}
int length_list(Stu * pHead){
    int length = 0;
    Stu * q = pHead->pNext;
    while (q != NULL){
        length++;
        q = q->pNext;
    }
    return length;
}
bool insert_list(Stu * pHead, int pos){
    Stu * pNew = (Stu *) malloc(sizeof(Stu));

    int i = 0;
    Stu * q = pHead;
//                 while执行几次     q = pHead              插入位置
//  pos= 1,i= 0         0           q = pHead               0——1         pHead 后面
//  pos= 2,i= 0,1       1           q = pHead->pNext        1——2        第 1 个后面
//  pos= 3,i= 0,1,2     2                                   2——3        第 2 个后面
    while (q != NULL && i < pos-1){
        q = q->pNext;
        i++;
    }
    if(i <= 0 || q == NULL){
        printf("插入位置出错!error:%d\n", pos);
        return false;
    }

    printf("请录入插入学生信息:\n");
    printf("姓名:");
    scanf("%s", pNew->name);
    printf("学号:");
    scanf("%d",&(pNew->id));

//  q 后面插入 数据,插入位置是在 q 后面
    pNew->pNext = q->pNext;
    q->pNext = pNew;
    return true;
}
bool delete_list(Stu * pHead, int pos){
    int i = 0;
    Stu * q = pHead;
    while (q != NULL && i < pos-1){
        ++i;
        q = q->pNext;
    }
    if(pos <= 0 || q == NULL){
        printf("删除位置出错!error:%d\n", pos);
        return false;
    } else{
        printf("删除成功,您删除的信息:姓名:%s  学号:%d\n", q->pNext->name, q->pNext->id);
    }
    Stu * p = q->pNext;
    q->pNext = q->pNext->pNext;
    free(p);
    return true;
}
void sort_list(Stu * pHead){

    int i;
    int j;
    Stu * p;
    Stu * q;
    int len = length_list(pHead);
    for (i = 0, p = pHead->pNext; i < len-1; ++i, p = p->pNext) {
        for (j = i+1, q = p->pNext; j < len; ++j, q = q->pNext) {
            if(p->id > q->id){
                Stu * t = p;
                p = q;
                q = t;
            }
        }
    }
    return;
}
上一篇:最强解析面试题:链表中倒数最后k个结点


下一篇:剑指offer 链表中倒数最后k个结点