链表详细解析

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stud_node{
    int num;
    char name[20];
    int score; 
    struct stud_node *next;
};
struct stud_node *Create_Stu_Doc(); //新建
struct stud_node *InsertDoc(struct stud_node *head,struct stud_node *stu);  //插入
struct stud_node *DeleteDoc(struct stud_node *head,int num);//删除
void Print_Stu_Doc(struct stud_node *head);//遍历
int main()
{
    struct stud_node *head,*p;
    int choice,num,score;
    char name[20];
    int size=sizeof(struct  stud_node);
    do{
        printf("1: Create 2:Insert 3:Delete 4:Print 0:Exit\n");
        scanf("%d",&choice);
        switch(choice){
            case 1:
                head=Create_Stu_Doc();
                break;
                case 2:
                    printf("Input num,name,score:\n");
                    scanf("%d%s%d",&num,name,&score);
                    p=(struct stud_node *)malloc(size);
                    p->num=num;
                    strcpy(p->name,name);
                    p->score=score;
                    head=InsertDoc(head,p);
                   break;
                   case 3:
                       printf("Input the num:\n");
                       scanf("%d",&num);
                       head=DeleteDoc(head,num);
                       break;
                       case 4:
                           Print_Stu_Doc(head);
                           break;
                           case 0:
                               break;
        }
    }while(choice!=0);
    return 0;
}
//建立
struct stud_node *Create_Stu_Doc()
{
    struct stud_node *head,*p;
    int num,score;
    char name[20];
    int size=sizeof(struct stud_node);
    head=NULL;
    printf("Input num,name,scroe:");
    scanf("%d%s%d",&num,name,&score);
    while(num!=0){
        p=(struct stud_node *)malloc(size);
        p->num=num;
        strcpy(p->name,name);
        p->score=score;
        head=InsertDoc(head,p);
        scanf("%d%s%d",&num,name,&score);
    }//输入0以后还得输入一次scanf,但是不会被记录
    return head;
}
//插入操作
struct stud_node *InsertDoc(struct stud_node *head,struct stud_node *stu)
{
    struct stud_node *ptr,*ptr1,*ptr2;
    ptr2=head;
    ptr=stu;//ptr表示待插入的节点
    //原链表为空时插入
    if(head==NULL){
        head=ptr;
        head->next=NULL;
    }
    else{//原链表不为空时插入
    while((ptr->num>ptr2->num) && (ptr2->next!=NULL)){
        ptr1=ptr2;
        ptr2=ptr2->next;//插入节点为头节点
    }
    if(ptr->num<=ptr2->num){
        if(head==ptr2) head=ptr;//在ptr1~ptr2间插入节点
        else ptr1->next=ptr;
        ptr->next=ptr2;
    }
        else{
        
            ptr2->next=ptr;
            ptr->next=NULL;        
}
}
    
    return head;
}
//删除节点
struct stud_node *DeleteDoc(struct stud_node *head,int num)
{
    struct stud_node *ptr1,*ptr2;
    //要被删除节点为头节点
    while(head!=NULL && head->num==num){
        ptr2=head;
        head=head->next;
        free(ptr2);
    }
    if(head==NULL){
        return NULL;
    }
    //要被删除节点非表头结点
    ptr1=head;
    ptr2=head->next;
    while(ptr2!=NULL){
        if(ptr2->num==num){//从表头的下一节点搜索
        ptr1->next=ptr2->next;
        free(ptr2);
            
        }
        else{
            ptr1=ptr2;
            ptr2=ptr1->next;
        }
    }
    return head;
}
//遍历操作
void Print_Stu_Doc(struct stud_node *head)
{
    struct stud_node *ptr;
    if(head==NULL){
        printf("\nNo Records \n");
        return ;
    }
    printf("\n the students' Records Are: \n");
    printf("num\t name\t score\n");
    for(ptr=head;ptr!=NULL;ptr=ptr->next){
        printf("%d\t %s\t %d\n",ptr->num,ptr->name,ptr->score);
    }
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码骑士one

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值