双向链表的创建、增加和删除,销毁

双向链表的创建,增加,删除和查看

#include<stdio.h>
#include<stdlib.h>
typedef struct student{
    int id;
    char name[20];
    struct student*pre,*next;
}Student;
Student* Create();//创建双向循环链表
void Print1(Student*h);//按博主所认为的输入逆向打印链表信息
void Print(Student*h);//按输入顺序打印链表
void DestroyLink(Student*h);//销毁链表
void InsertNode(Student*h);//链表的插入操作=
void DeleteNode(Student*h);//链表的删除操作
int main(){
    Student*h;
    h=Create();
    Print(h);
    printf("\n+++++++++++++++++\n");
    Print1(h);
    InsertNode(h);
    Print(h);
    DeleteNode(h);
    Print(h);
    DestroyLink(h);
}

Student* Create(){//此处使用尾插法创建链表
    Student*p,*h,*q;
    h=(Student*)malloc(sizeof(Student));
    if(h==NULL)
    {
        printf("错误!\n");
        return;
        }
    h->next=NULL;
    h->pre=NULL;
    h->name[0]='\0';
    q=h;
    while(1){
        p=(Student*)malloc(sizeof(Student));
        printf("请输入学生学号:\n");
        scanf("%d",&p->id);
        if(p->id==-1)break;
        printf("请输入姓名:\n");
        scanf("%s",p->name);
        q->next=p;
        p->pre=q;
        p->next=NULL;
        q=p;
    }
    q->next=h;
    h->pre=q;
    return h;
}
void Print1(Student*h){
    Student*temp;
    temp=h->pre;
    printf("反向输出链表内容:\n");
    printf("%-5s%-20s\n","ID","NAME");
    while(temp!=h){    
        printf("%-5d%-20s\n",temp->id,temp->name);
        temp=temp->pre;
    }

}
void Print(Student*h){
    Student*temp;
    temp=h->next;
    printf("学生信息如下:\n");
    printf("%-5s%-20s\n","ID","NAME");
    while(temp!=h){
        printf("%-5d%-20s\n",temp->id,temp->name);
        temp=temp->next;
    }
}
void DestroyLink(Student*h){//为了避免内存泄露,每次创建链表运行完程序应将创建的链表销毁,避免内存泄露
    Student*temp;
    Student*pre;
    temp=h->next;
    while(temp!=h){
        pre=temp;
        temp=temp->next;
        free(pre);
    }
    free(h);
}
void InsertNode(Student*h){
    Student*temp,*pNew,*pro,*prev1,*prev2;
    temp=h;
    pNew=(Student*)malloc(sizeof(Student));
    printf("请输入学号:\n");
    scanf("%d",&pNew->id);
    printf("请输入姓名:\n");
    scanf("%s",pNew->name);
    pro=h;
    int id;
    printf("请输入要插入的位置号码:");
    scanf("%d",&id);
    int flag=0;
    while(1){
        temp=temp->next;//按照所给学号信息,按照输入顺序找对应学生位置
        pro=pro->pre;//按照输入的反方向开始查找
        if(temp->id==id){
            pNew->next=temp;
            (temp->pre)->next=pNew;
            pNew->pre=temp->pre;
            temp->pre=pNew;
            break;
        }
        if(pro->id==id){
            pNew->pre=pro;
            (pro->next)->pre=pNew;
            pNew->next=pro->next;
            pro->next=pNew;
            break;
        }
        if(pro==temp){//判断如果两查找节点重合,说明链表中不存在输入ID号的学生信息
            printf("输入学号不存在!\n");
            return;
        }
        }

    printf("插入信息完成!\n");
}
void DeleteNode(Student*h){
    Student*temp,*pro;
    temp=h;
    int id;
    printf("请输入要删除的学生学号:\n");
    scanf("%d",&id);
    while(1){//和插入一样,同样两头开始找
        temp=temp->next;
        pro=temp->pre;
        if(temp->id==id){
            (temp->pre)->next=temp->next;
            (temp->next)->pre=temp->pre;
            free(temp);
            break;
            }
        if(pro->id==id){
            (pro->next)->pre=pro->pre;
            (pro->pre)->next=pro->next;
            free(pro);
            break;
            }
        if(temp==pro){
            printf("输入数据不存在!\n");
            return;
        }
    }
    printf("输入删除成功!\n");
}

猜你喜欢

转载自blog.csdn.net/qq_41681241/article/details/80645915