码迷,mamicode.com
首页 > 其他好文 > 详细

单向链表程序

时间:2017-06-22 18:42:04      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:内存   play   下标   ext   display   复制   调用   学生   search   

  1 /*****************************************************************************
  2 *                                    程序说明
  3 *            程序名称:简单单向链表
  4 *
  5 *            功能:    实现单向链表的创建、插入、删除、遍历和查找
  6 *
  7 *            作者:    Veis
  8 *
  9 *            注:此代码已在VC6.0及VS2010环境下编译通过,能正常实现所对应的功能
 10 *            程序元素排序与数组下标一样,从0开始。
 11 ******************************************************************************/
 12 #include<stdio.h>
 13 #include<string.h>
 14 #include<malloc.h>
 15 #include<stdlib.h>
 16 
 17 //宏定义数组大小,便于修改
 18 #define N 10        
 19 
 20 struct student
 21 {
 22     char name[20];                //数据域
 23     struct student *next;        //指针域
 24 };
 25 //判断查找和删除时对应位置的内容是否为空
 26 bool judge(struct student *p, int position)
 27 {
 28     int count;
 29     
 30     //初始化很重要
 31     count = 0;
 32 
 33     while( NULL != p )
 34     {
 35         p = p->next;
 36         count++;
 37     }
 38     //    printf("count:%d\n",count);
 39     if( position > (count - 1) )
 40     {
 41         return false;
 42     }else 
 43     {
 44         return true;
 45     }
 46 }
 47 //创建链表
 48 struct student * create()
 49 {
 50     // head链表的头指针,即链表的首地址
 51     // current当前处理的元素
 52     //next下一个状态的指针
 53     struct student *current, *head, *next;
 54     char str[N];
 55     char flag;
 56     
 57     //提示用户输入信息
 58     printf("请输入学生姓名:\n");
 59     scanf("%s",str);
 60     
 61     //动态申请内存
 62     head = (struct student *)malloc(sizeof(struct student));
 63     //字符串之间不能直接复制,需要调用字符串处理函数strcpy,包含在库string.h
 64     strcpy( head->name , str );
 65 
 66     //把链表的头指针赋给当前状态指针
 67     current = head;
 68     
 69     //输入y/n
 70     printf("是否继续输入?");
 71     scanf("%s",&flag);
 72 
 73     //排除回车符的干扰
 74     getchar();            
 75 
 76     while(flag != n)
 77     {
 78         //提示用户输入信息
 79         printf("请输入学生姓名:\n");
 80         scanf("%s",str);
 81 
 82         //动态申请内存
 83         next = (struct student *)malloc(sizeof(struct student));
 84         
 85         //判断是否分配内存成功
 86         if( NULL == next )
 87         {
 88             printf("Error:内存分配失败!");
 89             //退出程序,作用和 return 相似
 90             exit(-1);        
 91         }
 92         strcpy( next->name , str );
 93 
 94         //把当前状态指针的下一个指向下一个链表
 95         current->next = next;
 96 
 97         //把当前状态指针指往后移一个
 98         current = next;
 99     
100         printf("是否继续输入?");
101         scanf("%s",&flag);
102 
103         //排除回车符的干扰
104         getchar();    
105     }
106 
107     //输入完成后把当前状态指针的下一个状态指向NULL
108     current->next = NULL;
109 
110     return head;
111 }
112 
113 // 遍历链表
114 void list(struct student *p)
115 {
116     while(1)
117     {
118         printf("%s \n", p->name);
119 
120         if( p->next != NULL )
121         {
122             p = p->next;
123         }else
124         {
125             break;
126         }
127     }
128 }
129 
130 //插入元素到链表
131 void insert(struct student *p)
132 {
133     // insert要插入的元素
134     // current当前处理的元素
135     struct student *insert, *current;
136     char str[N];
137     int position;
138 
139     current = p;
140 
141     printf("请输入需要插入的学生姓名:\n");
142     scanf("%s",str);
143     
144     //动态申请内存
145     insert = (struct student *)malloc(sizeof(struct student));
146 
147     //判断是否分配内存成功
148     if( NULL == insert )
149     {
150         printf("Error:内存分配失败!");
151         exit(-1);
152     }
153     strcpy( insert->name , str );
154 loop:
155     printf("请输入需要插入的位置:\n");
156     scanf("%d",&position);
157 
158     
159     //查询并判断元素插入位置
160     if( position > 0 )
161     {
162         while( position > 1 )
163         {
164             current = current->next;
165             position--;
166         }
167         //插入的元素指向当前元素的下一个
168         insert->next = current->next;
169 
170         //当前元素的下一个指向插入的元素
171         current->next = insert;
172     }
173     else if( position == 0 ) 
174     {
175         p = insert;
176         insert->next = current;
177     }
178     else 
179     {
180         printf("\nError:输入错误!\n");
181         goto loop;
182     }
183     list(p);
184 }
185 //删除链表元素
186 void delet(struct student *p)
187 {
188     // insert用来保存要删除的元素
189     // current当前处理的元素
190     struct student *current, *delet;
191     int position;
192 
193     current = p;
194     delet = current;
195 loop:
196     printf("请输入需要删除的位置:\n");
197     scanf("%d",&position);
198     
199     if( judge(p,position) == true)
200     {
201         if( position > 0 )
202         {
203             while( position > 1 )
204             {
205                 current = current->next;
206                 position--;
207             }
208             //记录删除的节点
209             delet = current->next;
210 
211             //使当前指针指向下一个状态
212             current->next = delet->next;
213 
214             //释放要删除的内存
215             free(delet);
216         
217         }else if( position == 0 )
218         {
219             //使当前指针直接指向下一个节点
220             p = current->next;
221 
222             //释放内存
223             free(current);
224         }
225         else 
226         {
227             printf("\nError:输入错误!\n");
228             goto loop;
229         }
230         //调用查看效果
231         list(p);
232     }else 
233     {
234         printf("\nError:输入错误!\n");
235         goto loop;
236     }
237 }
238 
239 //回显查找的元素,可用于以后的拓展,故写成一个函数
240 void display(struct student *p)
241 {
242     printf("%s \n", p->name);
243 }
244 
245 //查找元素
246 void search(struct student *p)
247 {
248     struct student *current;
249     int position;
250     
251     current = p;
252 loop:
253     printf("请输入需要查找的位置:\n");
254 
255     scanf("%d",&position);
256     if( judge(p,position) == true)
257     {
258         //根据输入查找对应位置
259         if( position > 0 )
260         {
261             while( position > 1 )
262             {
263                 current = current->next;
264                 position--;
265             }
266 
267             p = current->next;
268 
269         }else if( position == 0 )
270         {
271             p = current;
272         }
273         else 
274         {
275             printf("Error:输入错误!");
276             goto loop;
277         }
278         //显示查找的内容
279         display(p);
280     }else
281     {
282         printf("\nError:输入错误!\n");
283         goto loop;
284     }
285 }
286 void main()
287 {
288     struct student *p;
289 
290     //创建链表
291     p = create();
292 
293     //删除链表
294 //    delet(p);
295 
296     //插入元素
297 //    insert(p);
298 
299     //查找元素
300     search(p);
301 }

查找功能效果如下图所示:

技术分享

 

单向链表程序

标签:内存   play   下标   ext   display   复制   调用   学生   search   

原文地址:http://www.cnblogs.com/veis/p/7066366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!