ZBLOG

使用单链表的其他方法实现该代码,代码插入位置的规则不变:#ifndef NULL #define NULL 0 #endif #include using namespace std; //定义单链表结点结构 struct LNode { int data; LNode* next; LNode() {}; LNode(int e, LNode* n = NULL) :data(e), next(n) {}; }; //单链表list类创建 class Li...

这里是使用另一种方式实现单链表的代码,仍然保持插入位置规则不变:

  1. #ifndef NULL
  2. #define NULL 0
  3. #endif
  4. #include<iostream>
  5. using namespace std;
  6. //定义单链表结点结构
  7. struct LNode
  8. {
  9. int data;
  10. LNode* next;
  11. LNode() {};
  12. LNode(int e, LNode* n = NULL) :data(e), next(n) {};
  13. };
  14. //单链表list类创建
  15. class List
  16. {
  17. protected:
  18. LNode* head;
  19. int _size;
  20. public:
  21. //构造函数
  22. List() {
  23. head = new LNode();
  24. head->next = NULL;
  25. _size = 0;
  26. }
  27. //析构函数
  28. ~List() {
  29. clear();
  30. delete head;
  31. head = NULL;
  32. };
  33. //清空链表
  34. void clear()
  35. {
  36. LNode* p = head->next;
  37. while (p != NULL)
  38. {
  39. LNode* temp = p->next;
  40. delete p;
  41. p = temp;
  42. }
  43. head->next = NULL; // 清空后头结点指针为空指针
  44. _size=0; // 更新大小为零
  45. }
  46. //头插法
  47. void insert(int e)
  48. {
  49. _size++;
  50. LNode* p = new LNode(e);
  51. p->next = head->next;
  52. head->next=p;
  53. };
  54. void insert(int index, int a)
  55. {
  56. if (index < 1 || index > _size + 1)
  57. {
  58. return; // 无效的插入位置,直接返回
  59. }
  60. _size++;
  61. LNode* p = head;
  62. // 移动 p 到要插入位置的前一个节点
  63. for (int i = 1; i < index; i++)
  64. {
  65. p = p->next;
  66. }
  67. LNode* tmp = new LNode(a);
  68. tmp->next = p->next;
  69. p->next = tmp;
  70. }
  71. void print_L()
  72. {
  73. LNode* p = head->next;
  74. while (p != NULL)
  75. {
  76. cout << p->data << " ";
  77. p = p->next;
  78. }
  79. cout << endl;
  80. }
  81. int change()
  82. {
  83. int x, y;
  84. cin >> x >> y;
  85. if (x<0 || x>_size || y<0 || y>_size)
  86. {
  87. cout << "error" << endl;
  88. return -1;
  89. }
  90. // 找到第 x 和第 y 个结点的前驱结点
  91. LNode* px_pre = head;
  92. for(int i=0;i<x-1;++i){
  93. px_pre=px_pre->next;
  94. }
  95. LNode* py_pre=head;
  96. for(int i=0;i<y-1;++i){
  97. py_pre=py_pre->next;
  98. }
  99. // 获取第 x 和第 y 个结点
  100. LNode* px=px_pre->next;
  101. LNode* py=py_pre->next;
  102. // 交换两个结点
  103. px_pre -> next=py;
  104. py_pre -> next=px;
  105. // 更新两个结点后面的指针
  106. LNode *tpx_next,*tpy_next;
  107. tpx_next=px->next;
  108. tpy_next=py->next;
  109. px->next=tpy_next;
  110. py->next=tpx_next;
  111. return 0;
  112. };
  113. };
  114. int main()
  115. {
  116. List LB;
  117. int n, a;
  118. cin >> n;
  119. for (int i = 0; i < n; i++)
  120. {
  121. cin >> a;
  122. LB.insert(i + 1, a);
  123. }
  124. LB.print_L();
  125. int t;
  126. cin >> t;
  127. for (int i = 0; i < t; i++)
  128. {
  129. int a = LB.change();
  130. if (a == -1)
  131. continue;
  132. LB.print_L();
  133. }
  134. return 0;
  135. }

这个代码使用了一个头结点和指针操作来实现单链表的插入和交换操作。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=9576

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?