自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++鏈表操作實(shí)際應(yīng)用技巧分享

開(kāi)發(fā) 后端
我們?cè)谶@里將會(huì)以一段代碼示例來(lái)為大家詳細(xì)講解有關(guān)C++鏈表操作的實(shí)現(xiàn)方法,希望大家可以從中掌握到其中的應(yīng)用技巧。

C++編程語(yǔ)言應(yīng)用范圍廣泛,在開(kāi)發(fā)人員眼中,它占據(jù)著非常重要的地位。在這里我們可以通過(guò)對(duì)C++鏈表操作的相關(guān)技巧,來(lái)充分了解一下這一語(yǔ)言的應(yīng)用方式以及他的應(yīng)用能給我們帶來(lái)哪些不同的感受。

C++鏈表操作代碼示例:

  1. // linklist.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。   
  2. #include "stdafx.h"   
  3. #include "malloc.h"   
  4. #include "stdlib.h"   
  5. #define NULL 0   
  6. #define LEN sizeof(struct student)   
  7. struct student   
  8. {   
  9. long num;   
  10. float score;   
  11. struct student* next;   
  12. };   
  13. int n;   
  14. struct student* creat()   
  15. {   
  16. struct student *head;   
  17. struct student *p1,*p2;   
  18. n=0;   
  19. p1=p2=(struct student*)malloc(LEN);   
  20. scanf("%ld",&p1->num);   
  21. scanf("%f",&p1->score);   
  22. head=NULL;   
  23. while (p1->num!=0)   
  24. {   
  25. n++;   
  26. if (n==1)   
  27. {   
  28. head=p1;   
  29. }   
  30. else   
  31. {   
  32. p2->next=p1;   
  33. }   
  34. p2=p1;   
  35. p1=(struct student*)malloc(LEN);   
  36. scanf("%ld",&p1->num);   
  37. if (p1->num==0)   
  38. break;   
  39. scanf("%f",&p1->score);   
  40. }   
  41. p2->next=NULL;   
  42. return (head);   
  43. };   
  44. void print(struct student *head)   
  45. {   
  46. struct student *p;   
  47. printf("\n New,These %d records are:\n",n);   
  48. p=head;   
  49. if (head!=NULL)   
  50. {   
  51. do   
  52. {   
  53. printf("%d %5.1f\n",p->num,p->score);   
  54. pp=p->next;   
  55. }while (p!=NULL);   
  56. }   
  57. }   
  58. struct student* insert(struct student *head,struct student *stud)   
  59. {   
  60. struct student *p0, *p1, *p2;   
  61. p1=head;   
  62. p0=stud;   
  63. if (head==NULL)   
  64. {   
  65. head=p0;   
  66. p0->next=NULL;//insert into head point   
  67. }   
  68. else   
  69. {   
  70. while ((p0->num>p1->num)&&(p1->next!=NULL))   
  71. {   
  72. p2=p1; //p2 is point to just p1 point to node;   
  73. p1p1=p1->next;   
  74. }   
  75. if (p0->num<=p1->num)   
  76. {   
  77. if (p1==head)   
  78. {   
  79. head=p0;//insert into before first node   
  80. }   
  81. else   
  82. {   
  83. p2->next=p0;//insert into after point p2   
  84. }   
  85. p0->next=p1;   
  86. }   
  87. else   
  88. {   
  89. p1->next=p0; //insert into after last point   
  90. p0->next=NULL;   
  91. }   
  92. }   
  93. n++;   
  94. return(head);   
  95. };   
  96. struct student* del(struct student *head,long num)   
  97. {   
  98. struct student *p1, *p2;   
  99. if (head==NULL)   
  100. {   
  101. printf("\n list Null!\n");   
  102. return (head);   
  103. }   
  104. p1=head;   
  105. while (num!=p1->num&&p1->next!=NULL)   
  106. //find num if equal p1->num   
  107. {   
  108. p2=p1;   
  109. p1p1=p1->next;   
  110. }   
  111. if (num==p1->num)   
  112. {   
  113. if (p1==head)   
  114. head=p1->next;//delete head node because num=head.num   
  115. else   
  116. p2->next=p1->next;//delete node. node is not head point   
  117. printf("delete:%ld\n",num);   
  118. n--;   
  119. }   
  120. else   
  121. {   
  122. printf("%ld not been found!\n",num);   
  123. }   
  124. return (head);   
  125. };   
  126. int _tmain(int argc, _TCHAR* argv[])   
  127. {   
  128. struct student *head,*end;   
  129. head=creat();   
  130. print(head);   
  131. struct student insertnode;   
  132. insertnode.num=3;   
  133. insertnode.score=900;   
  134. head=insert(head,&insertnode);   
  135. print(head);   
  136. head=del(head,3);   
  137. print(head);   
  138. return 0;   

C++鏈表操作的相關(guān)實(shí)現(xiàn)方法就為大家介紹到這里。

【編輯推薦】

  1. C++產(chǎn)生隨機(jī)數(shù)具體實(shí)現(xiàn)方法詳解
  2. C++打印地址信息實(shí)現(xiàn)方法介紹
  3. C++格式化字符串相關(guān)應(yīng)用解析
  4. C++枚舉子相關(guān)類(lèi)型解析
  5. C++枚舉類(lèi)型用途及定義詳解
責(zé)任編輯:曹凱 來(lái)源: 博客園
相關(guān)推薦

2010-02-01 11:13:00

C++ Traits

2010-02-06 13:47:08

C++標(biāo)準(zhǔn)擴(kuò)展

2010-02-04 14:58:06

C++內(nèi)存分配

2010-02-05 13:44:06

C++ eof()函數(shù)

2010-03-01 13:06:49

WCF繼承

2010-02-06 16:16:01

C++冒泡排序

2010-02-05 18:04:21

C++剪切板

2011-07-13 16:36:11

C++

2010-02-03 15:35:00

C++輸入輸出漢字

2010-02-05 17:25:26

C++標(biāo)識(shí)符命名規(guī)則

2010-03-03 16:25:41

Python字符串顯示

2010-03-01 17:52:03

WCF選擇綁定

2010-02-06 13:52:39

C++ profile

2010-02-04 11:15:28

C++模板限制

2010-02-06 10:24:48

C++二維數(shù)組初始化

2010-02-06 17:09:29

C++文件拷貝

2010-02-04 11:38:43

C++獲取當(dāng)前路徑

2010-02-02 10:46:51

C++獲取文件大小

2009-08-19 11:28:41

C#操作Word

2009-08-25 17:02:20

C#串口操作
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)