c语言_链表实例讲解(两个经典例子)

建立一个学生成绩的线性链表,对其实现插入,删除,输出,最后销毁。

demo1

  1 // lianbiao.cpp : Defines the entry point for the console application.
  2 //
  3 
  4 #include "stdafx.h"
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 
  8 struct grade {
  9     int score;
 10     struct grade *next;
 11 };
 12 typedef struct grade NODE;                     //typedef为C语言的关键字,作用是为一种数据类型定义一个新名字。
 13                                             //使用typedef目的一般有两个,一个是给变量一个易记且意义明确的新名字,
 14                                             //另一个是简化一些比较复杂的类型声明。
 15 struct grade *create();                        //创建链表
 16 void insert(NODE *head,NODE *pnew,int i);   //插入链表
 17 void pdelete(NODE *head,int i);                //删除列表
 18 void display(NODE *head);                    //输出链表
 19 void Pfree(NODE *head);                        //销毁链表
 20 
 21 int main(int argc, char *argv[]) {
 22     struct grade *head,*pnew;
 23     head=create();
 24 
 25     if (head==NULL)
 26         return 0;
 27     printf("输出创建的链表:");
 28     display(head);
 29     pnew=(NODE *)malloc(sizeof(NODE));
 30     if (pnew==NULL) {
 31         printf("创建失败!");
 32         return 0;
 33     }
 34     pnew->score=88;
 35     insert(head,pnew, 3);   //将新节点插入节点3的后面
 36     printf("插入后的链表:");
 37     display(head);
 38     pdelete(head,3);   //删除节点3
 39     printf("删除后的链表:");
 40     display(head);
 41     Pfree(head);
 42     return 0;
 43 }
 44 
 45 struct grade *create() {
 46     NODE *head,*tail,*pnew;
 47     int score;
 48     head=(NODE *)malloc(sizeof(NODE));             //创建头节点。
 49 
 50     if (head==NULL) {                             //创建失败返回
 51         printf("创建失败!");
 52         return NULL;
 53     }
 54 
 55     head->next=NULL;                            //头节点指针域置NULL
 56     tail=head;                                    //开始时尾指针指向头节点
 57     printf("输入学生成绩:");
 58     while (1) {                                    //创建链表
 59         scanf("%d", &score);
 60         if (score<0)                            //成绩为负是退出循环
 61             break;
 62         pnew=(NODE *)malloc(sizeof(NODE));         //创建新节点
 63         if (pnew==NULL) {                        //创建失败返回
 64             printf("创建失败!");
 65             return NULL;
 66         }
 67         pnew->score=score;                        //新节点数据域存放输入的成绩
 68         pnew->next=NULL;                        //新节点指针域置NULL
 69         tail->next=pnew;                        //新节点插入到表尾
 70         tail=pnew;                                  //为指针指向当前的尾节点
 71     }
 72     return head;                                  //返回创建链表的头指针
 73 }
 74 void insert(NODE *head,NODE *pnew,int i) {
 75     NODE *p; //当前指针
 76     int j;
 77 
 78     p=head;
 79     for (j=0; j<i&&p!=NULL; j++) //p指向要插入的第i个节点
 80         p=p->next;
 81 
 82     if (p==NULL) { //节点i不存在
 83         printf("与插入的节点不存在!");
 84         return;
 85     }
 86 
 87     pnew->next=p->next;   //插入节点的指针域指向第i个节点的后继节点
 88     p->next=pnew;    //犟第i个节点的指针域指向插入的新节点
 89 }
 90 
 91 void pdelete(NODE *head,int i) {
 92     NODE *p,*q;
 93     int j;
 94     if (i==0) //删除的是头指针,返回
 95         return;
 96     p=head;
 97     for (j=1; j<i&&p->next!=NULL; j++)
 98         p=p->next;  //将p指向要删除的第i个节点的前驱节点
 99     if (p->next==NULL) { //表明链表中的节点不存在
100         printf("不存在!");
101         return;
102     }
103     q=p->next;  //q指向待删除的节点
104     p->next=q->next;  //删除节点i,也可写成p->next=p->next->next
105     free(q);   //释放节点i的内存单元
106 }
107 
108 void display(NODE *head) {
109     NODE *p;
110     for (p=head->next; p!=NULL; p=p->next)
111         printf("%d ",p->score);
112     printf("\n");
113 }
114 
115 void pfree(NODE *head) {
116     NODE *p,*q;
117 
118     p=head;
119     while (p->next!=NULL) { //每次删除头节点的后继节点
120         q=p->next;
121         p->next=q->next;
122         free(q);
123     }
124     free(head);   //最后删除头节点
125 }
126 
127 void Pfree(NODE *head) {
128     NODE *p,*q;
129     p=head;
130     while (p->next!=NULL) {
131         q=p->next;
132         p->next=q->next;
133         free(q);
134     }
135     free(p);
136 }

demo2

  1 #include <stdio.h>
  2 #include <malloc.h>
  3 #include <conio.h>
  4 #include <stdlib.h>
  5  
  6 //链表单元定义,链表相关变量
  7 struct student {
  8     int id;
  9     float score;
 10     struct student *next;
 11 } *head,*pthis;
 12  
 13 //输入数据创建链表
 14 void input() {
 15     struct student *tmp;
 16     printf("\n\n请输入学生的信息以学号为0结束:\n");
 17     do {
 18         printf("ID\t成绩\n");
 19         if ((tmp=(struct student *)malloc(sizeof(struct student)))==NULL) {
 20             printf("\n错误!不能申请所需的内存!\n");
 21             exit(0);
 22         }
 23         scanf("%d\t%f",&tmp->id,&tmp->score);
 24         tmp->next=NULL;
 25         if (tmp->id!=0) {
 26             if (head==NULL) {
 27                 head=tmp;
 28                 pthis=head;
 29             } else {
 30                 pthis->next=tmp;
 31                 pthis=pthis->next;
 32             }
 33         }
 34     } while (tmp->id!=0);
 35     free(tmp);
 36 }
 37  
 38 //搜索链表找到第一个符合条件的项目输出
 39 void search(int id) {
 40     printf("\n\n查询结果\n");
 41     printf("ID\t成绩\n");
 42     printf("-------------------------------\n");
 43     if (head==NULL) {
 44         printf("\n错误!没有数据!\n");
 45         return;
 46     }
 47     pthis=head;
 48     while (pthis!=NULL) {
 49         if (pthis->id==id) {
 50             printf("%d\t%.2f\n",pthis->id,pthis->score);
 51             return;
 52         } else {
 53             pthis=pthis->next;
 54         }
 55     }
 56     printf("\n没有找到!\n");
 57 }
 58  
 59 //列表输出链表中的所有项
 60 void list() {
 61     printf("\n\n数据列表\n");
 62     printf("ID\t成绩\n");
 63     printf("-------------------------------\n");
 64     if (head==NULL) {
 65         printf("错误,没有数据!\n");
 66         return;
 67     }
 68     pthis=head;
 69     while (pthis!=NULL) {
 70         printf("%d\t%.2f\n",pthis->id,pthis->score);
 71         pthis=pthis->next;
 72     }
 73 }
 74  
 75 //插入数据
 76 void insert() {
 77     int i,p;
 78     struct student *tmp;
 79     if (head==NULL) {
 80         printf("\n\n数据不存在,无法插入!\n");
 81         return;
 82     }
 83     printf("\n请输入插入点:\n");
 84     scanf("%d",&p);
 85     if (p<0) {
 86         printf("输入不合法!");
 87         return;
 88     }
 89     printf("\n\n请输入学生的信息:\nID\t成绩\n");
 90     if ((tmp=(struct student *)malloc(sizeof(struct student)))==NULL) {
 91         printf("\n错误!不能申请所需的内存!\n");
 92         exit(0);
 93     }
 94     scanf("%d\t%f",&tmp->id,&tmp->score);
 95     tmp->next=NULL;
 96     if (tmp->id!=0) {
 97         pthis=head;
 98         if (p==0) {
 99             tmp->next=head;
100             head=tmp;
101         } else {
102             for (i=0; i<p-1; i++) {
103                 if (pthis->next->next==NULL) {
104                     printf("\n找不到插入点,您输入的数据太大!\n");
105                     return;
106                 }
107                 pthis=pthis->next;
108             }
109             tmp->next=pthis->next;
110             pthis->next=tmp;
111         }
112     } else {
113         printf("\n数据无效!\n");
114         free(tmp);
115     }
116 }
117  
118 //追加数据
119 void append() {
120     struct student *tmp;
121     printf("\n\n请输入学生的信息:\nID\t成绩\n");
122     if ((tmp=(struct student *)malloc(sizeof(struct student)))==NULL) {
123         printf("\n错误!不能申请所需的内存!\n");
124         exit(0);
125     }
126     scanf("%d\t%f",&tmp->id,&tmp->score);
127     tmp->next=NULL;
128     if (tmp->id!=0) {
129         if (head==NULL) {
130             head=tmp;
131         } else {
132             pthis=head;
133             while (pthis->next!=NULL) {
134                 pthis=pthis->next;
135             }
136             pthis->next=tmp;
137         }
138     } else {
139         free(tmp);
140         printf("\n数据无效!\n");
141     }
142 }
143  
144 //删除数据
145 void del() {
146     int p,i;
147     struct student *tmp;
148     if (head==NULL) {
149         printf("\n\n没有数据,无法删除!\n");
150         return;
151     }
152     printf("\n\n请输入要删除的记录号:\n");
153     scanf("%d",&p);
154     if (p<0) {
155         printf("\n输入不合法!\n");
156         return;
157     }
158     if (p==0) {
159         pthis=head;
160         head=pthis->next;
161         free(pthis);
162         pthis=head;
163     } else {
164         pthis=head;
165         for (i=0; i<p-1; i++) {
166             pthis=pthis->next;
167             if (pthis->next==NULL) {
168                 printf("\n\n指定记录不存在,无法删除!\n");
169                 return;
170             }
171         }
172         tmp=pthis->next;
173         pthis->next=pthis->next->next;
174         free(tmp);
175     }
176 }
177  
178 //程序主函数
179 void main() {
180     char command=0;
181     int id=0;
182  
183 //主循环
184     do {
185         printf("\n\n\t 菜单\n");
186         printf("-------------------------------\n");
187         printf("\ta,输入数据\n");
188         printf("\tb,查询记录\n");
189         printf("\tc,数据列表\n");
190         printf("\td,追加记录\n");
191         printf("\te,插入记录\n");
192         printf("\tf,删除记录\n");
193         printf("\tg,退出系统\n");
194         printf("-------------------------------\n");
195         printf("\t请选择:");
196         command=getch();
197  
198 //命令处理
199         switch (command) {
200         case 'a':
201             if (head==NULL) {
202                 input();
203                 break;
204             } else {
205                 printf("\n\n数据已经存在!\n");
206                 break;
207             }
208         case 'b':
209             printf("\n\n要查询的ID:");
210             scanf("%d",&id);
211             search(id);
212             break;
213         case 'c':
214             list();
215             break;
216         case 'd':
217             append();
218             break;
219         case 'e':
220             insert();
221             break;
222         case 'f':
223             del();
224             break;
225         }
226     } while (command!='g');
227 }

http://www.oschina.net/code/snippet_105637_43706

posted on 2016-11-28 17:18  zhuguanhao  阅读(2607)  评论(0编辑  收藏  举报

导航