链表操作

链表操作

建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去,输出删除前和删除后的链表信息。

要求用4个函数实现: 建立节点函数、建立链表函数、遍历链表函数、  删除链表节点函数

Sample input:

i
1001      Tom      man        11
i
1002      lusy     woman        12
i
1003      jack     man         13
q
12

Sample output:

按 'i' 增加一个新节点;

按 'q' 退出!

i

学号:1001

姓名:Tom

性别:男

年龄:11

成功建立一个节点!

按 'i' 增加一个新节点;

按 'q' 退出!

i

学号:1002

姓名:Lusy

性别:女

年龄:12

成功建立一个节点!

按 'i' 增加一个新节点;

按 'q' 退出!

i

学号:1003

姓名:Jack

性别:男

年龄:13

成功建立一个节点!

按 'i' 增加一个新节点;

按 'q' 退出!

q

退出链表建立:

显示链表详细信息:

学号      姓名     性别     年龄

1001      Tom      男        11

1002      lusy       女        12

1003      jack      男        13

删除链表节点:

请输入需删除节点的学生年龄:12

 

成功删除节点!

 

删除链表节点后链表详细信息:

学号      姓名     性别     年龄

1001      Tom      男        11

1003      jack      男         13

 在网上找了好久也没找到正中的写法。刚好作业布置了,就参考教科书借花献佛发上我写的一篇。

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 typedef struct Link
  5 {
  6     struct stu
  7     {
  8         int num;
  9         char name[20];
 10         int year;
 11         char sex[8];
 12     }data;
 13     struct Link *next;
 14 }IA;
 15 
 16 IA *Create();
 17 IA *Insert(IA *head,IA *stud);
 18 IA *Delete(IA *head,int num);
 19 void Print(IA *head);
 20 
 21 int main()
 22 {
 23     //freopen("a.txt","r",stdin);
 24     IA *head;
 25     int year;
 26 
 27     head=Create();
 28 
 29     printf("删除链表节点: 
请输入需删除节点的学生年龄:");
 30     scanf("%d",&year);
 31     printf("

");
 32     head=Delete(head,year);
 33 
 34     return 0;
 35 }
 36 
 37 IA *Create()
 38 {
 39     IA *head,*p;
 40     int num,year;
 41     char sex[8],name[20];
 42     char choice;
 43     int size=sizeof(IA);
 44     int cls=0;
 45 
 46     head=NULL;
 47     printf("按 'i' 增加一个新节点;
按 'q' 退出!
");
 48         scanf("%c",&choice);
 49     while(choice!='q')
 50     {
 51         if(choice=='i')
 52         {
 53             scanf("%d%s%s%d",&num,name,sex,&year);
 54             p=(IA *)malloc(size);
 55             p->data.num=num;
 56             strcpy(p->data.name,name);
 57             strcpy(p->data.sex,sex);
 58             p->data.year=year;
 59             head=Insert(head,p);
 60         }
 61         getchar();
 62         printf("成功建立一个节点!
按 'i' 增加一个新节点;
按 'q' 退出!
");
 63             scanf("%c",&choice);
 64         if(choice=='q')
 65         {
 66             printf("退出链表建立:
显示链表详细信息:
");
 67             Print(head);
 68         }
 69     }
 70     return head;
 71 }
 72 
 73 IA *Insert(IA *head,IA *stu)
 74 {
 75     IA *ptr,*ptr1,*ptr2;
 76 
 77     ptr2=head;
 78     ptr=stu;
 79 
 80     if(head==NULL)
 81     {
 82         head=ptr;
 83         head->next=NULL;
 84     }
 85     else
 86     {
 87         while((ptr->data.num>ptr2->data.num)&&(ptr2->next!=NULL))
 88         {
 89             ptr1=ptr2;
 90             ptr2=ptr2->next;
 91         }
 92         if(ptr->data.num<=ptr2->data.num)
 93         {
 94             if(head==ptr2) head=ptr;
 95             else
 96                 ptr1->next=ptr;
 97             ptr->next=ptr2;
 98         }
 99         else
100         {
101             ptr2->next=ptr;
102             ptr->next=NULL;
103         }
104     }
105     return head;
106 }
107 
108 IA *Delete(IA *head,int year)
109 {
110     IA *ptr1,*ptr2;
111 
112     while(head!=NULL&&head->data.year==year)
113     {
114         ptr2=head;
115         head=head->next;
116         free(ptr2);
117     }
118 
119     if(head==NULL)
120         return NULL;
121 
122     ptr1=head;
123     ptr2=head->next;
124     while(ptr2!=NULL)
125     {
126         if(ptr2->data.year==year)
127         {
128             ptr1->next=ptr2->next;
129             free(ptr2);
130         }
131         else
132             ptr1=ptr2;
133         ptr2=ptr1->next;
134     }
135     printf("成功删除节点!

删除链表节点后链表详细信息:
");
136     Print(head);
137     return head;
138 }
139 
140 void Print(IA *head)
141 {
142     IA *p;
143     if(head==NULL)
144     {
145         printf("
No Records
");
146         return;
147     }
148      printf("学号      姓名     性别     年龄
");
149      for(p=head;p!=NULL;p=p->next)
150         printf("%d	%s	%s	%d
",p->data.num,p->data.name,p->data.sex,p->data.year);
151 }
学生信息链表

我把操作简化一下,把每个节点的成员只留下了num。

功能是能把输入的num们从下到大排序,还能删除。。。这回是我自己写的,结果老超时,看了半天结果错在了插入节点是,没有考率是头结点的情况

Sample input:

 1 45 23 96 78 20 12 98 65 78 24 31 65 87 20 0 14 89 63 34 57 86 31 45 85 45 -1
45

Sample output:

0 1 12 14 20 20 23 24 31 31 34 45 45 45 57 63 65 65 78 78 85 86 87 89 96 98

0 1 12 14 20 20 23 24 31 31 34 57 63 65 65 78 78 85 86 87 89 96 98

建议大家别在CFree上测试,看不出来有没有超时的。-.-

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 
  5 typedef struct Link
  6 {
  7     int num;
  8     struct Link *next;
  9 }IA;
 10 
 11 IA *Create();
 12 IA *Insert(IA *head,IA *p);
 13 void *Delete(IA *head,int num);
 14 void Print(IA *head);
 15 
 16 int main()
 17 {
 18    // freopen("a.txt","r",stdin);
 19     IA *head=NULL;
 20     int num;
 21     head=Create();
 22     scanf("%d",&num);
 23     Delete(head,num);
 24     return 0;
 25 }
 26 
 27 IA *Create()
 28 {
 29     IA *head=NULL,*p;
 30     int num;
 31    // head=(IA *)malloc(sizeof(IA));
 32     while(scanf("%d",&num)!=EOF,num>=0)
 33     {
 34         p=(IA *)malloc(sizeof(IA));
 35         p->num=num;
 36         head=Insert(head,p);
 37     }
 38     Print(head);
 39     return head;
 40 }
 41 
 42 IA *Insert(IA *head,IA *p)
 43 {
 44     IA *ptr1,*ptr,*ptr2;
 45     ptr=p;
 46     ptr2=head;
 47     if(head==NULL)
 48     {
 49         head=p;
 50         p->next=NULL;
 51     }
 52     else
 53     {
 54         while( ptr->num > ptr2->num && ptr2->next!=NULL)
 55         {
 56             ptr1=ptr2;
 57             ptr2=ptr2->next;
 58         }
 59         if( ptr->num <= ptr2->num)
 60         {
 61             if(ptr2==head)
 62                 head=ptr;
 63             else
 64                 ptr1->next=ptr;
 65             ptr->next=ptr2;
 66         }
 67         else
 68         {
 69             ptr2->next=ptr;
 70             ptr->next=NULL;
 71         }
 72     }
 73     return head;
 74 }
 75 
 76 void *Delete(IA *head,int num)
 77 {
 78   //  printf("-2");
 79     IA *ptr2,*ptr1;
 80     if(head==NULL)
 81     {
 82 //        printf("-5") ;
 83         return;
 84     }
 85   //  printf("-4");
 86     while(head->num==num)
 87     {
 88     //    printf("-3");
 89         ptr1=head;
 90         head=head->next;
 91         free(ptr1);
 92     }
 93     ptr1=head;
 94     ptr2=head->next;
 95     while(ptr2!=NULL)
 96     {
 97       //  printf("-1");
 98         if(ptr2->num==num)
 99         {
100             ptr1->next=ptr2->next;
101             free(ptr2);
102         }
103         else
104             ptr1=ptr2->next;
105         ptr2=ptr1->next;
106     }
107     Print(head);
108 }
109 
110 void Print(IA *head)
111 {
112     IA *p;
113     if(head==NULL)
114         return;
115     for(p=head;p!=NULL;p=p->next)
116     {
117         printf("%d ",p->num);
118     }
119     printf("

");
120 }
链表


    

原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4213568.html