【C語言】單鏈表的所有操作的實現(包括PopBack、PushBack、PopFront、PushFront、Insert)

http://blog.csdn.net/hanjing_1995/article/details/51539563

[cpp]?view plain?copy
  1. #define?_CRT_SECURE_NO_WARNINGS?1??
  2. #include<iostream>??
  3. using?namespace?std;??
  4. ??
  5. ??
  6. //單鏈表的實現??
  7. #include<assert.h>??
  8. ??
  9. ??
  10. typedef?int?DataType;??
  11. ??
  12. typedef?struct?SListNode??
  13. {??
  14. ????DataType?_data;??
  15. ????struct?SListNode*?_next;??
  16. }SListNode;??
  17. ??
  18. ??
  19. SListNode*?_CreateNode(DataType?x)??
  20. {??
  21. ????SListNode*?head?=?(SListNode*)malloc(sizeof(SListNode));??
  22. ????head->_data?=?x;??
  23. ????head->_next?=?NULL;??
  24. ????return?head;??
  25. }??
  26. ??
  27. ??
  28. void?PushBack(SListNode*&?head,?DataType?x)??
  29. {??
  30. ????if?(head?==?NULL)??
  31. ????{??
  32. ????????head?=?_CreateNode(x);??
  33. ????????head->_next?=?NULL;??
  34. ????}??
  35. ????else??
  36. ????{??
  37. ????????SListNode*?cur?=?head;??
  38. ????????while?(cur->_next?!=?NULL)??
  39. ????????{??
  40. ????????????cur?=?cur->_next;??
  41. ????????}??
  42. ????????cur->_next?=?_CreateNode(x);??
  43. ????}??
  44. ??????
  45. }??
  46. ??
  47. ??
  48. void?PopBack(SListNode*&?head)??
  49. {??
  50. ????if?(head?==?NULL)??
  51. ????{??
  52. ????????return;??
  53. ????}???
  54. ????else?if?(head->_next?==?NULL)??
  55. ????{??
  56. ????????free(head);??
  57. ????????head?=?NULL;??
  58. ????}??
  59. ????else??
  60. ????{??
  61. ????????SListNode*?cur?=?head;??
  62. ????????SListNode*?next?=?head;??
  63. ??
  64. ????????while?(cur)??
  65. ????????{??
  66. ????????????next?=?cur->_next;??
  67. ????????????if?(next?!=?NULL?&&?next->_next?==?NULL)??
  68. ????????????{??
  69. ????????????????free(next);??
  70. ????????????????cur->_next?=?NULL;??
  71. ????????????????return;??
  72. ????????????}??
  73. ??
  74. ????????????cur?=?cur->_next;??
  75. ????????}??
  76. ????}??
  77. ??????
  78. }??
  79. ??
  80. ??
  81. void?PushFront(SListNode*&?head,DataType?x)??
  82. {??
  83. ????if?(head?==?NULL)??
  84. ????{??
  85. ????????head?=?_CreateNode(x);??
  86. ????}??
  87. ????else??
  88. ????{??
  89. ????????SListNode*?pcur?=?_CreateNode(x);??
  90. ????????pcur->_next?=?head;??
  91. ????????head?=?pcur;??
  92. ????}??
  93. }??
  94. ??
  95. ??
  96. void?PopFront(SListNode*&?head)??
  97. {??
  98. ????if?(head?==?NULL)??
  99. ????{??
  100. ????????return;??
  101. ????}??
  102. ????else?if?(head->_next?==?NULL)??
  103. ????{??
  104. ????????free(head);??
  105. ????????head?=?NULL;??
  106. ????}??
  107. ????else??
  108. ????{??
  109. ????????SListNode*?del?=?head;??
  110. ????????SListNode*?next?=?head->_next;??
  111. ????????free(del);??
  112. ????????del?=?NULL;??
  113. ????????head?=?next;??
  114. ????}??
  115. }??
  116. ??
  117. ??
  118. void?Insert(SListNode*?head,int?pos,DataType?x)??
  119. {??
  120. ????assert(pos?>=?0);??
  121. ????SListNode*?cur?=?head;??
  122. ????while?(--pos?&&?cur)??
  123. ????{??????????????
  124. ????????cur?=?cur->_next;??????????
  125. ????}??
  126. ????if?(pos?>?0)??
  127. ????{??
  128. ????????printf("pos位置大于鏈表長度!\n");??
  129. ????????return;??
  130. ????}??
  131. ????SListNode*?newcur?=?_CreateNode(x);??
  132. ????if?(cur->_next)??
  133. ????{??
  134. ????????SListNode*?next?=?cur->_next;??
  135. ??????
  136. ????????cur->_next?=?newcur;??
  137. ????????newcur->_next?=?next;??
  138. ????}??
  139. ????else?if?(cur->_next?==?NULL)??
  140. ????{??
  141. ????????cur->_next?=?newcur;??
  142. ????}??
  143. }??
  144. ??
  145. ??
  146. size_t?Length(SListNode*&?head)??
  147. {??
  148. ????size_t?count?=?0;??
  149. ????SListNode*?cur?=?head;??
  150. ????while?(cur)??
  151. ????{??
  152. ????????count++;??
  153. ????????cur?=?cur->_next;??
  154. ????}??
  155. ????return?count;??
  156. }??
  157. ??
  158. ??
  159. void?PrintSList(SListNode*&?head)??
  160. {??
  161. ????if?(head?==?NULL)??
  162. ????{??
  163. ????????return;??
  164. ????}??
  165. ????SListNode*?cur?=?head;??
  166. ????while?(cur)??
  167. ????{??
  168. ????????printf("%d->",?cur->_data);??
  169. ????????cur?=?cur->_next;??
  170. ????}??
  171. ????printf("\n");??
  172. }??
  173. ??
  174. ??
  175. void?Test()??
  176. {??
  177. ????SListNode*?sList?=NULL;??
  178. ????PushBack(sList,?1);??
  179. ????PushBack(sList,?2);??
  180. ????PushBack(sList,?3);??
  181. ????PushBack(sList,?4);??
  182. ????PushBack(sList,?5);??
  183. ????PrintSList(sList);??
  184. ??
  185. ????PopBack(sList);??
  186. ????PrintSList(sList);??
  187. ??
  188. ????PushFront(sList,?0);??
  189. ????PrintSList(sList);??
  190. ??
  191. ????PopFront(sList);??
  192. ????PrintSList(sList);??
  193. ??
  194. ????Insert(sList,?3,?10);??
  195. ????PrintSList(sList);??
  196. ??
  197. ????int?ret?=?Length(sList);??
  198. ????printf("單鏈表長度為:%d\n",?ret);??
  199. }??
  200. ??
  201. ??
  202. int?main()??
  203. {??
  204. ????Test();??
  205. ????system("pause");??
  206. ????return?0;??
  207. }??

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384154.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384154.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384154.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Shuffle'm Up——簡單模擬

【題目描述】 A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several diff…

C++ explicit關鍵字詳解

http://www.cnblogs.com/ymy124/p/3632634.html 首先, C中的explicit關鍵字只能用于修飾只有一個參數的類構造函數, 它的作用是表明該構造函數是顯示的, 而非隱式的, 跟它相對應的另一個關鍵字是implicit, 意思是隱藏的,類構造函數默認情況下即聲明為implicit(隱式). 那么顯示聲…

Fire!——兩個BFS

【題目描述】 【題目分析】 看到題目后很清楚是兩個BFS&#xff0c;可是我覺得對于火的BFS可以轉換成判斷&#xff0c;我的做法是將火的位置全部記錄下來&#xff0c;然后判斷某個位置距離每個火的步數是否小于當前步數&#xff0c;可是錯了&#xff0c;還不清楚為什么&#x…

函數調用過程(棧楨)

棧楨 首先來看一段代碼 #include<stdio.h> int add(int x, int y) {int z x y;return z; } int main() {int a 10;int b 20;int ret add(a, b);printf("ret %d\n",ret);return 0; } 此處是為了給a,b分別開辟空間,這時棧楨如圖所示 兩條push命令將a,b變…

C++智能指針簡單剖析

http://blog.csdn.net/lanxuezaipiao/article/details/41603883 導讀 最近在補看《C Primer Plus》第六版&#xff0c;這的確是本好書&#xff0c;其中關于智能指針的章節解析的非常清晰&#xff0c;一解我以前的多處困惑。C面試過程中&#xff0c;很多面試官都喜歡問智能指針相…

非常可樂——BFS

【題目描述】 大家一定覺的運動以后喝可樂是一件很愜意的事情&#xff0c;但是seeyou卻不這么認為。因為每次當seeyou買了可樂以后&#xff0c;阿牛就要求和seeyou一起分享這一瓶可樂&#xff0c;而且一定要喝的和seeyou一樣多。但seeyou的手中只有兩個杯子&#xff0c;它們的容…

整型數據存儲

//代碼1 #include<stdio.h> int main() {char a -1;signed char b -1;unsigned char c -1;printf("a %d, b %d, c %d", a, b, c);return 0; } 1000 0000 0000 0001 -> -1源碼 1111 1111 1111 1110 -> -1反碼 1111 1111 1111 1111 -> -1補碼 對于…

聊聊gcc參數中的-I, -L和-l

http://blog.csdn.net/stpeace/article/details/49408665 在本文中&#xff0c; 我們來聊聊gcc中三個常見的參數&#xff0c; 也即-I, -L和-l 一. 先說 -I (注意是大寫的i) 我們先來看簡單的程序&#xff1a; main.c: [cpp] view plaincopy #include <stdio.h> #incl…

Pots——BFS

【題目描述】 You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i) empty the pot i to the drain; POUR(i,j) pour from pot i to pot j;…

HDU - 4578Transformation——線段樹+區間加法修改+區間乘法修改+區間置數+區間和查詢+區間平方和查詢+區間立方和查詢

【題目描述】 HDU - 4578Transformation Problem Description Yuanfang is puzzled with the question below: There are n integers, a1, a2, …, an. The initial values of them are 0. There are four kinds of operations. Operation 1: Add c to each number between ax …

[C++基礎]034_C++模板編程里的主版本模板類、全特化、偏特化(C++ Type Traits)

http://www.cnblogs.com/alephsoul-alephsoul/archive/2012/10/18/2728753.html 1. 主版本模板類 首先我們來看一段初學者都能看懂&#xff0c;應用了模板的程序&#xff1a; 1 #include <iostream>2 using namespace std;3 4 template<class T1, class T2>5 clas…

自定義類型: 結構體,枚舉,聯合

1.結構體 個人認為結構體和數組特別相似&#xff0c;只不過結構體和數組的區別在于結構體的成員可以是不同類型&#xff0c;而數組成員類型是相同的。 &#xff08;1&#xff09;結構體的聲明 struct tag {成員列表//至少得有一個成員 }值列表;//值列表可以省略 struct {int a…

詳解C++中的函數調用和下標以及成員訪問運算符的重載

http://www.jb51.net/article/78436.htm 這篇文章主要介紹了詳解C中的函數調用和下標以及成員訪問運算符,講到了這些二元運算符使用的語法及重載,需要的朋友可以參考下函數調用 使用括號調用的函數調用運算符是二元運算符。 語法 ?1primary-expression ( expression-list )備…

A計劃——BFS

【題目描述】 可憐的公主在一次次被魔王擄走一次次被騎士們救回來之后&#xff0c;而今&#xff0c;不幸的她再一次面臨生命的考驗。魔王已經發出消息說將在T時刻吃掉公主&#xff0c;因為他聽信謠言說吃公主的肉也能長生不老。年邁的國王正是心急如焚&#xff0c;告招天下勇士…

使用openssl的md5庫

http://blog.csdn.net/sinat_35297665/article/details/78244523 在linux機器上&#xff0c;有一個命令可以計算出文件的md5值&#xff0c;那就是md5sum&#xff0c;如果沒有的話&#xff0c;就需要安裝RPM包&#xff1a;coreutils。 現在我們使用openssl的庫也可以方便的計算出…

主席樹入門

今天學習了一下主席樹&#xff08;名字這么強的嘛&#xff09; 雖然直接理解起來不容易&#xff0c;但是這種解決問題的思想其實并不陌生。 我們可以首先來看維護整個區間第K大的線段樹 我們將[l,r]區間內數字的多少用線段樹進行維護&#xff0c;這樣的話為了求取區間第k大&…

Socket網絡編程--小小網盤程序(1)

http://www.cnblogs.com/wunaozai/p/3886588.html 這個系列是準備講基于Linux Socket進行文件傳輸。簡單的文件傳輸就是客戶端可以上傳文件&#xff0c;可以從服務器端下載文件。就這么兩個功能如果再加上身份驗證&#xff0c;就成了FTP服務器了&#xff0c;如果對用戶的操作再…

使用 Verdaccio 構建自己的私有 npm 倉庫

前言 無論你是公司的開發者&#xff0c;還是個人開發者&#xff0c;你可能都聽說過或者使用過 npm&#xff0c;這是一個使用廣泛的 JavaScript 包管理器。但是&#xff0c;你是否遇到過以下的問題&#xff1a;你需要一個私有的包存放地方&#xff0c;或者你需要在離線環境下使…

HDU - 4348To the moon——主席樹+區間修改

HDU - 4348To the moon 【題目描述】 【題目分析】 題目中說明每次更新后時間都會加1&#xff0c;而且還會需要查詢以前的區間&#xff0c;還會需要返回以前的時間&#xff0c;所以是很裸的主席樹。區間查詢的話我們同樣需要用到lazy標記 通過這道題我發現線段樹的操作還是很靈…

進入一個目錄需要那些權限

1.文件訪問者的分類 文件的訪問者具體可分為以下幾類&#xff1a; (1)擁有者 (2)組擁有者 (3)其他用戶 這些都代表什么意思呢&#xff1f; 其中r表示只讀&#xff0c;w表示只寫&#xff0c;x表示可執行&#xff0c;第一個字母代表了文件的類型&#xff0c;其中文件類型可以分為…