当前位置:   article > 正文

C++单链表最简单的创建,插入,删除功能实现_单链表的插入y总c++

单链表的插入y总c++

 C++单链表最简单的创建,插入,删除功能实现

   这里是对数据结构的复习,好久没有动C++了,现在再来写写链表, 怕真的以后看都看不懂了就尴尬了。逻辑语句不难,应该能看懂。大神别骂我为什么不用C++模板,因为我只想单纯写写链表看看自己还会不会。另外main函数可以自己写写清屏语句,我就不写了。

 

  1. #include <iostream>
  2. using namespace std;
  3. /* 创建一个单链表 */
  4. struct Node {
  5. int data;
  6. Node* next;
  7. };
  8. class Linklist
  9. {
  10. public:
  11. Linklist();
  12. ~Linklist();
  13. void Creatlist(int num);
  14. void Insertlist(int position, int num);
  15. void Showlist();
  16. void Delete(int position);
  17. private:
  18. Node *Head;
  19. };
  20. Linklist::Linklist()
  21. {
  22. Head = new Node;
  23. Head->next = NULL;
  24. }
  25. Linklist::~Linklist()
  26. {
  27. Node *p;
  28. while (Head) {
  29. p = Head;
  30. Head = Head->next;
  31. delete p;
  32. }
  33. Head = NULL;
  34. }
  35. void Linklist::Creatlist(int num) {
  36. Node *L, *Y;
  37. Y = Head;
  38. cout << "Please enter your " << num << "number:" << endl;
  39. for (int i = 0; i < num; i++) {
  40. L = new Node;
  41. cin >> L->data;
  42. L->next = Y->next;
  43. Y->next = L;
  44. Y = L;
  45. };
  46. }
  47. void Linklist::Insertlist(int position, int num) {
  48. int i = 0;
  49. Node *L;
  50. L = Head;
  51. while (L&&i < position - 1) {
  52. L = L->next;
  53. i++;
  54. };
  55. if (!L||i>position-1) {
  56. cout << "插入位置不对!" << endl;
  57. }
  58. else {
  59. Node *Y;
  60. Y = new Node;
  61. Y->data = num;
  62. Y->next = L->next;
  63. L->next = Y;
  64. }
  65. }
  66. void Linklist::Showlist() {
  67. Node *L;
  68. L = Head->next;
  69. while (L) {
  70. cout << "--->"<<L->data ;//主义这一句和下一句的顺序,颠倒了就会出现很难发现的问题。
  71. L = L->next;
  72. }
  73. cout << endl;
  74. }
  75. void Linklist::Delete(int position) {
  76. int i = 0;
  77. Node *L;
  78. Node *Y = Head;
  79. L = Head;
  80. while (L->next&&i < position-1) {
  81. L = L->next;
  82. i++;
  83. };
  84. if (!L->next||i>position-1) {
  85. cout << "删除位置不对!!" << endl;
  86. }
  87. else {
  88. Y = L->next;
  89. L->next = Y->next;
  90. delete Y;
  91. }
  92. }
  93. int main()
  94. {
  95. //这是我最开始检测链表功能的语句
  96. //Linklist P;
  97. //P.Creatlist(4);
  98. //P.Showlist();
  99. //P.Insertlist(1, 2);
  100. //P.Showlist();
  101. //P.Delete(5);
  102. //P.Showlist();
  103. //return 0;
  104. int num, position;
  105. Linklist P;
  106. system("cls");
  107. int chioce;
  108. do {
  109. cout << endl;
  110. cout << "\t\t\t 链表最基本操作\n" << endl;
  111. cout << "\t\t---------------------------------\n";
  112. cout << "\t\t---------------------------------\n";
  113. cout << " ***1.创建链表***\n";
  114. cout << " ***2.插入节点(已预定、未预定、)***\n";
  115. cout << " ***3.删除节点***\n";
  116. cout << " ***4.打印链表***\n";
  117. cout << " ***5.退出系统***\n";
  118. cout << "\t\t----------------------------------\n";
  119. cout << "\t\t----------------------------------\n";
  120. cout << "\t\t----*****输入chioce:" << " " << endl;
  121. cin >> chioce;
  122. switch (chioce)
  123. {
  124. case 1:
  125. int i;
  126. cout << "请输入创建的链表的长度:" << endl;
  127. cin >> i;
  128. P.Creatlist(i);
  129. cout << "创建完成!" << endl;
  130. break;
  131. case 2:
  132. {
  133. cout << "请输入你需要插入的位置" << endl;
  134. cin >> position;
  135. cout << endl;
  136. cout << "请输入你需要插入的数:" << endl;
  137. cin >> num;
  138. P.Insertlist(position, num);
  139. cout << "插入完成!" << endl;
  140. }break;
  141. case 3: {
  142. int position;
  143. cout << "请输入你需要删除的位置" << endl;
  144. cin >> position;
  145. cout << endl;
  146. P.Delete(position);
  147. cout << "删除完成!" << endl;
  148. } break;
  149. case 4: {
  150. cout << "链表目前如下:" << endl;
  151. P.Showlist();
  152. } break;
  153. case 5:cout << "\n感谢使用本系统欢迎您下次使用!\n";
  154. exit(0);
  155. default:
  156. cout << "Invalid chioce!\n";
  157. }
  158. } while (chioce != 5);
  159. return 0;
  160. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/893338
推荐阅读
相关标签
  

闽ICP备14008679号