热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

简单链表练习

今天的任务:简单链表练习用C语言明天贴上源代码以下代码并未通过测试,25#include6#include78typedefstructst

 

      今天的任务 :简单链表 练习  用 C 语言

       明天 贴上 源代码

       

//以下 代码 并未 通过 测试,

 


  2 
  5 #include <malloc.h>
  6 #include <iostream>
  7 
  8 typedef struct studentInfo
  9 {
 10     char *  No;
 11     char * Name;    
 12     float Score;
 13     struct studentInfo * next;
 14     
 15 
 16 }student;
 17 
 18 
 19 /*对 节点 数据进行初始化*/
 20 void InitStudent(student * s)
 21 {
 22     if(s)
 23     {
 24         s->Name &#61; (char * )malloc(18);
 25         s->next &#61; NULL;
 26         s->No &#61; (char * )malloc(18);
 27         s->Score &#61; -1;
 28         
 29         //初始化为空格
 30         memset(s->Name,032,sizeof(s->Name));
 31         memset(s->No,032,sizeof(s->No));
 32 
 33     }
 34 }
 35 
 36 
 37 /*为  分配内存空间*/
 38  student * NewStudent()
 39  {
 40      student * s &#61; (student * )malloc(sizeof(student));    
 41      InitStudent(s);
 42 
 43      return s;
 44  }
 45 
 46 
 47 
 48 
 49 
 50 /*初始化链表头*/
 51 bool InitListHead(student * head)
 52 {
 53     head &#61; (student * )malloc(sizeof(student));
 54     if(head!&#61;NULL)
 55     {
 56         InitStudent(head);
 57         return true;
 58     }
 59     else
 60     {
 61         return false;
 62     }
 63 }
 64 
 65 
 66 /*DestroyList  销毁整个线性表*/
 67 void DestroyList(student * head)
 68 {
 69     student * h &#61; head;
 70     
 71     /*临时变量 用于指向 需要 销毁 的 内存空间*/
 72     student * temp &#61;h;
 73 
 74     /*注意保留下一个节点 的 地址 ,否则无法 全部回收 数据 --leak memory!*/
 75     while(temp)
 76     {    
 77         
 78         student * temp &#61; h;
 79         
 80         /*在销毁之前 移动到 下一位 以免空指针 错误*/
 81         h &#61; h->next;
 82         
 83         //记得销毁 NO, Name
 84         free(temp->Name);
 85         free(temp->No);
 86         
 87         //销毁结构体
 88         free(temp);
 89     }
 90 }
 91 
 92 
 93 /*删除链表中的所有数据*/
 94 void ClearList(student * head)
 95 {
 96     /*养成好习惯 保存好 重要数据,以免改动.函数中 使用 h 替代 head*/
 97     student * h &#61; head;
 98 
 99     do
100     {
101         h->No &#61; NULL;
102         h->Name &#61; NULL;
103         h->Score &#61; -1;
104 
105     }while(h->next);
106 
107 }
108 
109 /*判断 是否为 空*/
110 bool isEmpty(student * head)
111 {
112     /*head 为 NULL*/
113     if(head&#61;&#61;NULL && head->next&#61;&#61;NULL)
114     {
115         return true;
116     }
117     else  /*head 不为 NULL*/
118     {
119         return false;
120     }
121 }
122 
123 
124 int ListLength(student * head)
125 {
126     /*again ,指针操作 ,记得保存啊*/
127     student  * h &#61; head;
128     
129     int index &#61; 0 ;
130 
131     while(h)
132     {
133         index&#43;&#43;;
134 
135         h &#61; h->next;
136     }
137     return index;
138 }
139 
140 
141 /*注意:next 会被置为 NULL,. 老外的习惯 目标←源, 老子不太习惯,cao!,,*/
142 void CopyTo(student * destination,student * source)
143 {
144     if(source!&#61;NULL)
145     {
146         destination->No &#61; source->No;
147         destination->Name &#61; source->Name;
148         destination->Score &#61; source->Score;
149         destination->next &#61;NULL;
150     }
151     else
152     {
153         destination &#61; NULL;
154     }
155 }
156 
157 
158 /* 用户交互. 请求用户输入数据
159 */
160 void GetValue(student * s)
161 {
162     if(s)
163     {
164         printf("\nplease input the No of the student:");
165         //scanf("%s",s->No);
166         gets(s->No);
167         
168         printf("\nplease input the Name of the student:");
169         //scanf("%s",s->Name);
170         gets(s->Name);
171 
172         printf("\nplease input the Score of the student:");
173         scanf("%f",&(s->Score));
174     }
175 }
176 
177 
178 /*setValue  用于在 代码中 直接设置 节点 中的数据*/
179 
180 bool setValue(student * item,char * no,char * name,float score)
181 {
182     if(item &#61;&#61; NULL)
183     {
184         return false;
185     }
186     else
187     {
188         item->No &#61; no;
189         item->Name &#61; name;
190         item->Score &#61; score;
191         return true;
192     }
193 }
194 
195 
196 /*获取指定节点 , 获取 第i 个,并取出 其值 赋给 tmp
197 */
198 bool GetElement(student * head,int i,student *tmp )
199 {
200     
201     student * h &#61; head;
202     
203     int index &#61; 0;
204 
205     if(isEmpty(h)||i>ListLength(h))
206     {
207         return false;
208     }
209     else
210     {        
211         while(h)
212         {
213             index&#43;&#43;;
214             if(index !&#61; i)
215             {
216                 h &#61; h->next;            
217             }
218             else
219             {
220                 /*将数据 写到 tmp 指向 的 内出空间 ,这样 外部就可以使用了*/
221                 CopyTo(tmp , h);
222                 return true;                
223             }
224         }
225     }
226 }
227 
228 /*定义一个 指向函数的指针类型, 此函数 返回 bool
229 */
230 typedef bool (*compare)(student * one, student * another);
231 
232 bool comp(student * one, student * another)
233 {
234     if(one->No &#61;&#61; another->No && one->Name &#61;&#61; another->Name && one->Score &#61;&#61; another->Score &&    one->next &#61;&#61; another->next)
235     {
236         return true;
237     }
238     else
239     {
240         return false;
241     }
242 
243 }
244 
245 bool compName(student * one, student * another)
246 {
247     if(strcmp(one->Name,another->Name ) &#61;&#61; 0)
248     {
249         return true;
250     }    
251     else /*为加上 else 语句 会默认返回 true.,可能是环境问题*/
252     {
253         return false;
254     }
255 }
256 
257 
258 /*将函数作为参数 ,并根据此函数的 返回值(bool), 判断是否匹配
259 */
260 int LocateElem(student * head,student * e, compare comp)
261 {
262     student * h &#61; head;
263     int i &#61;0;
264 
265     while(h)
266     {
267         i&#43;&#43;;
268 
269         if(comp(h,e))
270         {
271             CopyTo(e,h);
272 
273             return i;
274         }
275         else
276         {
277             h &#61; h->next;
278         }
279     }
280     return 0 ;
281 }
282 
283 
284 /*找到前一个节点
285 */
286 student * PriorEle(student *head,student *cur,student *pre)
287 {
288     /*/由于 非双向 也 非循环 ,故需要两个指针 ,first ,second , second 用于判断当前指向是否与cur相同 ,相同则返回 first,否则继续,没找到返回 NULL
289     */
290     student * first &#61; head;
291     
292     student * second &#61; first->next;
293 
294     if(first &#61;&#61; cur)
295     {
296         return NULL;
297     }
298     else
299     {
300 
301 
302         while(second)
303         {
304             if (comp(second,cur))
305             {
306                 CopyTo(pre,first);
307                 return pre;
308             }
309             else
310             {
311                 first &#61; second;
312                 second&#61; second->next;
313             }
314         }
315 
316         return NULL;
317     }
318 }
319 
320 
321 student * NextEleOfThis(student * head,student *cur,student * nextEle)
322 {
323     student * h &#61; head;
324 
325     while(h)
326     {
327         if(comp(cur,h))
328         {
329             CopyTo(nextEle,h->next);
330 
331             return nextEle;
332         }
333     }
334     return NULL;
335 }
336 
337 /*/把 e 插到 p 后面.  /*还存在问题
338 */
339 bool   AddItem(student * p ,student * e)
340 {
341     if(p &#61;&#61;NULL || e &#61;&#61; NULL)
342     {
343         #ifdef DEBUG
344                 printf("AddItem(student * p ,student *e) function  error!  null pointer !");
345         #endif
346 
347         return false;
348     }
349     else
350     {
351         student * temp &#61; p->next;
352         p->next &#61; e;
353         e->next &#61; temp;
354 
355         return true;
356     }
357 }
358 
359 /*此处考虑 尚不周全 */
360 bool ListInsert(student * head,int i,student * e)
361 {
362     student * h &#61; head;
363 
364     int index &#61; 0;
365     int length &#61; ListLength(h);
366 
367     /* 判断 i  是否合格 */
368     if0 < i <&#61; length )
369     {
370         /* 找到适合 的位置 然后 插入 */
371         while(h)
372         {
373             index&#43;&#43;;
374             
375             /*要求插入到 第 一 位 的情况 */
376             if(i&#61;&#61;index && i&#61;&#61;1)
377             {
378                 e->next &#61; h;
379                 head &#61; e;
380                 return true;
381             }
382             else if(i &#61;&#61; index)
383             {                
384                 AddItem(h,e);
385                 
386                 return true;
387             }
388             
389             /* 插入到第二位 的 时候 , 会出现问题 ,因为 第一次之后 就已经 换到了 第三个 */
390             if(index >&#61; 2)
391             {
392                 h &#61; h->next;
393             }
394         }
395     }
396     else
397     {
398         return false;
399     }
400 }
401 
402 
403 /*删除第i 个 , 并用 e 返回其值.
404 ********注意 head 被删除的 情况.!!!!!!!!!!!!!!!!!!!!!!!!!
405 */
406 bool ListDelete(student * head ,int i, student * e)
407 {
408     student * h &#61; head;
409     int index &#61; 0;
410     
411     //
412     if0 < i < ListLength(h) )
413     {
414         while(h)
415         {
416             index&#43;&#43;;
417             if(h!&#61;NULL)
418             {
419                 
420                 if(i&#61;&#61;index)
421                 {
422                     CopyTo(h,e);
423                     
424                     
425                     /*保存好要删除的 元素节点 的 指针*/
426                     student * temp &#61; h;
427                     
428                     /*修改 链表 ,*/
429                     if(index !&#61; 1)
430                     {                        
431                         h &#61; PriorEle(head,h,h);            
432                         h->next &#61; h->next->next;
433                     }
434                     /* 删除 头指针 的 情况*/
435                     else
436                     {
437                         head &#61; h->next;
438                     }
439 
440                     /*销毁 数据*/
441                     free(temp);
442                 }
443 
444                 /*移动指针*/
445                 h &#61; h->next;
446             }
447             else
448             {
449                 InitStudent(e);
450                 return false;
451             }
452             
453         }
454     }
455 }
456 
457 void Display(student * head,char * title)
458 {
459     student *  h &#61; head;
460     printf("\n\n&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;%18s&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;\n",title);
461     while(h)
462     {
463         printf("NO:%-15s \n",h->No);
464         printf("Name:%-15s \n",h->Name);
465         printf("Score:%-15f \n",h->Score);
466         h &#61; h->next;
467     }
468     printf("\n\n&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;\n");
469 }
470 
471 /*将函数作为参数 的 第二种写法,---第一种见上面.*/
472 bool ListTraverse(student * head,bool (*visit)(student * p))
473 {
474     student * h &#61; head;
475     
476     while(h)
477     {
478         if(!visit(h))
479         {
480             return false;
481         }
482         else
483         {
484             h &#61; h->next;
485         }
486     }
487 
488     return true;
489 }
490 
491 
492 
493 
494 int _tmain(int argc, _TCHAR* argv[])
495 {
496     
497     
498     student  * head &#61; NewStudent();
499     
500     
501     /*isEmpty() Test! */
502     printf("is empty ? %s",isEmpty(head)? "true" : "false");
503 
504     GetValue(head);
505     
506     /*isEmpty() Test! */
507     printf("is empty ? %s \n",isEmpty(head)? "true" : "false");
508 
509     student * item &#61;  NewStudent();    
510     setValue(item,"80001060011","second",80.5);
511     Display(item,"Second&#39;s Value");
512 
513     student * third &#61;  NewStudent();    
514     setValue(third,"8000106047","third",90);
515     Display(third,"Third&#39;s Value");
516 
517     student * fourth&#61;  NewStudent();
518     
519     /*检验copyTo()函数  将数据复制到fourth 中, 然后修改,*/
520     CopyTo(fourth,third);
521     Display(fourth,"The Fourth&#39;s Value");
522     fourth->Name &#61; "Test Insert -Fourth";
523     Display(fourth,"The Fourth Value");
524 
525     AddItem(head,item);
526 
527     AddItem(head ,third );
528 
529     /*ListInsert(head,4,fourth); Test! */
530     ListInsert(head,3,fourth);
531 
532     /*NextEleOfThis(head,head,tmp); Test! */
533     student * tmp &#61; NewStudent();    
534     NextEleOfThis(head,head,tmp);    
535     Display(tmp," tmp Data");
536 
537     
538     /*LocateElem(student * head,student * e, compare comp) */
539     int i &#61; LocateElem(head,third,compName);
540     printf("\n Locate at the  %dth  item \n",i);
541 
542     /*ListLength(head) Test! */
543     printf("Length:%d",ListLength(head));
544     
545     Display(head,"  ALL Students !");
546 
547     /*ListDelete(head,6,fourth);  Test!
548     *此时 第六号 不存在, 
549     */    
550     ListDelete(head,6,fourth);
551     Display(fourth,"Item deleted  list");    
552 
553     scanf("%d",i);
554 
555     return 0;
556 }
557 

 

       争取爱上 数据结构....

 

      Orz...  累死了..

 

   洗洗睡吧 

 

 



推荐阅读
  • 本文详细介绍了C语言中链表的两种动态创建方法——头插法和尾插法,包括具体的实现代码和运行示例。通过这些内容,读者可以更好地理解和掌握链表的基本操作。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 题目Link题目学习link1题目学习link2题目学习link3%%%受益匪浅!-----&# ... [详细]
  • Linux设备驱动程序:异步时间操作与调度机制
    本文介绍了Linux内核中的几种异步延迟操作方法,包括内核定时器、tasklet机制和工作队列。这些机制允许在未来的某个时间点执行任务,而无需阻塞当前线程,从而提高系统的响应性和效率。 ... [详细]
  • Codeforces Round #566 (Div. 2) A~F个人题解
    Dashboard-CodeforcesRound#566(Div.2)-CodeforcesA.FillingShapes题意:给你一个的表格,你 ... [详细]
  • 在多线程编程环境中,线程之间共享全局变量可能导致数据竞争和不一致性。为了解决这一问题,Linux提供了线程局部存储(TLS),使每个线程可以拥有独立的变量副本,确保线程间的数据隔离与安全。 ... [详细]
  • C++: 实现基于类的四面体体积计算
    本文介绍如何使用C++编程语言,通过定义类和方法来计算由四个三维坐标点构成的四面体体积。文中详细解释了四面体体积的数学公式,并提供了两种不同的实现方式。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文探讨了 C++ 中普通数组和标准库类型 vector 的初始化方法。普通数组具有固定长度,而 vector 是一种可扩展的容器,允许动态调整大小。文章详细介绍了不同初始化方式及其应用场景,并提供了代码示例以加深理解。 ... [详细]
  • 本实验主要探讨了二叉排序树(BST)的基本操作,包括创建、查找和删除节点。通过具体实例和代码实现,详细介绍了如何使用递归和非递归方法进行关键字查找,并展示了删除特定节点后的树结构变化。 ... [详细]
  • 本文详细探讨了VxWorks操作系统中双向链表和环形缓冲区的实现原理及使用方法,通过具体示例代码加深理解。 ... [详细]
  • 本题涉及一棵由N个节点组成的树(共有N-1条边),初始时所有节点均为白色。题目要求处理两种操作:一是改变某个节点的颜色(从白变黑或从黑变白);二是查询从根节点到指定节点路径上的第一个黑色节点,若无则输出-1。 ... [详细]
  • 本题探讨如何通过最大流算法解决农场排水系统的设计问题。题目要求计算从水源点到汇合点的最大水流速率,使用经典的EK(Edmonds-Karp)和Dinic算法进行求解。 ... [详细]
author-avatar
mobiledu2502859223
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有