C语言链表使用详解

1. [代码]demo1     

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

2. [代码]demo2     

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值