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...

以下是使用另一种方法实现相同功能的代码,其中单链表结点结构和 List 类的定义保持不变:

  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. while (head)
  30. {
  31. LNode* p = head->next;
  32. delete head;
  33. head = p;
  34. }
  35. };
  36. //在指定位置插入元素
  37. void insert(int index, int a)
  38. {
  39. if (index < 1 || index > _size + 1)
  40. {
  41. return; // 无效的插入位置,直接返回
  42. }
  43. LNode* p = head;
  44. // 移动 p 到要插入位置的前一个节点
  45. for (int i = 1; i < index; i++)
  46. {
  47. p = p->next;
  48. }
  49. LNode* tmp = new LNode(a);
  50. tmp->next = p->next;
  51. p->next = tmp;
  52. _size++;
  53. print_L(); // 打印链表元素值(调试用)
  54. return; // 插入成功
  55. }
  56. void print_L()
  57. {
  58. LNode* p = head->next;
  59. while (p != NULL)
  60. {
  61. cout << p->data << " ";
  62. p = p->next;
  63. }
  64. cout << endl;
  65. }
  66. int change()
  67. {
  68. int x, y;
  69. cin >> x >> y;
  70. if (x<0 || x>_size || y<0 || y>_size)
  71. {
  72. cout << "error" << endl;
  73. return -1;
  74. }
  75. if (x == y) // 如果交换位置相同,直接返回
  76. return 0;
  77. LNode* px_prev = head; // 第一个要交换的结点的前一个结点
  78. for (int i = 0; i < x - 1; i++)
  79. {
  80. px_prev = px_prev->next;
  81. }
  82. LNode* py_prev = head; // 第二个要交换的结点的前一个结点
  83. for (int j = 0; j < y - 1; j++)
  84. {
  85. py_prev = py_prev->next;
  86. }
  87. LNode* px = px_prev->next; // 第一个要交换的结点
  88. LNode* py = py_prev->next; // 第二个要交换的结点
  89. LNode* tmp_next_px = px->next; // 记录第一个要交换结点后续的指针
  90. px_prev->next = py; // 将第二个要交换的结点链接到第一个要交换结点之前(px之前)
  91. if(px == py_prev) { // 如果两个要交换的结点相邻
  92. px->next = py->next; // 将第二个要交换结点的后续指针链接到第一个要交换结点之后(py之后)
  93. } else {
  94. px->next = py->next; // 将第二个要交换结点的后续指针链接到第一个要交换结点之后
  95. py_prev->next = px; // 将第一个要交换的结点链接到第二个要交换结点之前(py之前)
  96. }
  97. py->next = tmp_next_px; // 将记录的px后续指针链接到py之后
  98. print_L(); // 打印链表元素值(调试用)
  99. return 0;
  100. };
  101. };
  102. int main()
  103. {
  104. List LB;
  105. int n, a;
  106. cin >> n;
  107. for (int i = 0; i < n; i++)
  108. {
  109. cin >> a;
  110. LB.insert(i + 1, a);
  111. }
  112. int t;
  113. cin >> t;
  114. for (int i = 0; i < t; i++)
  115. {
  116. int a = LB.change();
  117. if (a == -1)
  118. continue;
  119. }
  120. return 0;
  121. }

这段代码通过在指定位置插入元素来构建单链表,实现了两个节点位置交换的功能。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?