单链表

  1 /*用链表实现学生成绩信息的管理*/
  2 #include "stdio.h"
  3 #include "stdlib.h"
  4 #include "string.h"
  5 struct stud_node
  6 {
  7     int num;
  8     char name[20];
  9     int score;
 10     struct stud_node*next;
 11 };
 12 
 13 /*函数声明*/
 14 struct stud_node *Creat_Stu_Doc();//新建链表
 15 struct stud_node *InsertDoc(struct stud_node *head,struct stud_node *stud);//插入
 16 struct stud_node *DeleteDoc(struct stud_node *head,int num);//删除
 17 void Print_Stu_Doc(struct stud_node *head);//遍历
 18 
 19 /*主函数*/
 20 int main()
 21 {
 22     struct stud_node *head,*p;
 23     int choice,num,score;
 24     char name[20];
 25     int size=sizeof(struct stud_node);
 26 
 27     do 
 28     {
 29         printf("1.Creat 2.Insert 3.Delete 4.Print 0.Exit \n");
 30         scanf("%d",&choice);
 31         switch (choice)
 32         {
 33         case 1:
 34             head=Creat_Stu_Doc();
 35             break;
 36         case 2:
 37             printf("Input num,name and score:\n");
 38             scanf("%d%s%d",&num,&name,&score);
 39             p=(struct stud_node*)malloc(size);
 40             p->num=num;
 41             strcpy(p->name,name);
 42             p->score=score;
 43             head=InsertDoc(head,p);
 44             break;
 45         case 3:
 46             printf("Input num:\n");
 47             scanf("%d",&num);
 48             head=DeleteDoc(head,num);
 49             break;
 50         case 4:
 51             Print_Stu_Doc(head);
 52             break;
 53         case 0:
 54             break;
 55         }
 56     } while (choice!=0);
 57 
 58     return 0;
 59 }
 60 
 61 //新建链表
 62 struct stud_node *Creat_Stu_Doc()
 63 {
 64     struct stud_node *head,*p;
 65     int num,score;
 66     char name[20];
 67     int size=sizeof(struct stud_node);
 68 
 69     head=NULL;
 70     printf("Input num,name and score:\n");
 71     scanf("%d%s%d",&num,&name,&score);
 72     while (num!=0)
 73     {
 74         p=(struct stud_node*)malloc(size);
 75         p->num=num;
 76         strcpy(p->name,name);
 77         p->score=score;
 78         head=InsertDoc(head,p);
 79         scanf("%d%s%d",&num,&name,&score);
 80     }
 81     return head;
 82 }
 83 
 84 //插入操作
 85 struct stud_node *InsertDoc(struct stud_node *head,struct stud_node *stud)
 86 {
 87     struct stud_node *ptr,*ptr1,*ptr2;
 88 
 89     ptr2=head;
 90     ptr=stud;//ptr指向待插入的新的学生记录结点
 91     //原链表为空时的插入
 92     if (head==NULL)
 93     {
 94         head=ptr;            //新插入结点成为头结点
 95         head->next=NULL;
 96     }
 97     else                    //原链表不为空时的插入
 98     {
 99         while ((ptr->num>ptr2->num)&&(ptr2->next!=NULL))
100         {
101             ptr1=ptr2;                    //ptr1,ptr2各后移一个结点
102             ptr2=ptr2->next;
103         }
104         if (ptr->num<=ptr2->num)        //在ptr1与ptr2之间插入新结点
105         {
106             if(head==ptr2)
107                 head=ptr;
108             else
109                 ptr1->next=ptr;
110         }
111         else                //新插入结点成为尾结点
112         {
113             ptr2->next=ptr;
114             ptr->next=NULL;
115         }
116     }
117     return head;
118 }
119 
120 //删除操作
121 struct stud_node *DeleteDoc(struct stud_node *head,int num)
122 {
123     struct stud_node *ptr1,*ptr2;
124 
125     //要被删除结点为表头结点
126     while (head!=NULL&&head->num==num)
127     {
128         ptr2=head;
129         head=head->next;
130         free(ptr2);
131     }
132     if(head==NULL)        //链表空
133         return NULL;
134     //要被删除结点为非表头结点
135     ptr1=head;
136     ptr2=head->next;    //从表头的下一个结点搜索所有符合删除要求的结点
137     while(ptr2!=NULL)
138     {
139         if (ptr2->num==num)        //ptr2所指结点符合删除要求
140         {
141             ptr1->next=ptr2->next;
142             free(ptr2);
143         }
144         else
145             ptr1=ptr2;        //ptr1后移一个结点
146         ptr2=ptr1->next;    //ptr2指向ptr1的后一个结点
147     }
148     return head;
149 }
150 
151 //遍历操作
152 void Print_Stu_Doc(struct stud_node *head)
153 {
154     struct stud_node *ptr;
155     if (head==NULL)
156     {
157         printf("\nNo Records\n");
158         return;
159     }
160     printf("\nThe Students' Records Are:\n");
161     printf("Num\t Name\t Score\n");
162     for(ptr=head;ptr!=NULL;ptr=ptr->next)
163         printf("%d\t%s\t%d\n",ptr->num,ptr->name,ptr->score);
164 }

 

你可能感兴趣的:(单链表)