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. int data;
  9. LNode* next;
  10. LNode() {};
  11. LNode(int e, LNode* n = NULL) :data(e), next(n) {};
  12. };
  13. // 单链表List类创建
  14. class List {
  15. protected:
  16. LNode* head;
  17. int _size;
  18. void init() {
  19. head = new LNode;
  20. head->next = NULL;
  21. _size = 0;
  22. }
  23. public:
  24. // 构造函数
  25. List(){init();}
  26. // 析构函数
  27. ~List() {
  28. LNode* p = head;
  29. while (head) {
  30. p = p->next;
  31. delete head;
  32. head = p;
  33. }
  34. }
  35. // 头节点
  36. List(int info) { // 使用头结点储存某些信息,不随链表插入改变位置
  37. init();
  38. head->data = info;
  39. }
  40. int& info() { return head->data; } // 返回链表头结点的信息
  41. void insert(int& e) {
  42. _size++;
  43. LNode* p = new LNode(e, head->next);
  44. head->next = p;
  45. }
  46. void insert(int index, int a) {
  47. if (index < 1 || index > _size + 1) {
  48. return; // 无效的插入位置,直接返回
  49. }
  50. _size++;
  51. LNode* prev = nullptr; // 插入位置前一个节点的指针(初始为空)
  52. LNode* curr = head; // 当前节点的指针
  53. for (int i = 0; i < index; i++) {
  54. prev = curr;
  55. curr = curr->next;
  56. }
  57. LNode* newNode = new LNode(a);
  58. newNode->next = curr;
  59. if (prev != nullptr) {
  60. prev->next = newNode;
  61. } else { // 如果是在头部插入,则更新头节点
  62. head->next = newNode;
  63. }
  64. }
  65. void print_L() {
  66. LNode* p = head->next;
  67. while (p != NULL) {
  68. cout << p->data << " ";
  69. p = p->next;
  70. }
  71. cout << endl;
  72. }
  73. int change() {
  74. int x, y;
  75. cin >> x >> y;
  76. if (x < 0 || x > _size || y < 0 || y > _size) {
  77. cout << "error" << endl;
  78. return -1;
  79. }
  80. if (x == y) { // 如果两个位置相同,不进行交换
  81. return 0;
  82. }
  83. LNode* prevX = nullptr; // 第一个要交换的位置的前一个节点
  84. LNode* currX = head; // 第一个要交换的位置的当前节点
  85. LNode* prevY = nullptr; // 第二个要交换的位置的前一个节点
  86. LNode* currY = head; // 第二个要交换的位置的当前节点
  87. for (int i = 0; i < x; i++) { // 找到第一个要交换的位置及其前一个节点
  88. prevX= currX;
  89. currX= currX->next ;
  90. }
  91. for (int i = 0; i < y; i++) { // 找到第二个要交换的位置及其前一个节点
  92. prevY= currY;
  93. currY= currY->next ;
  94. }
  95. if (prevX != nullptr) { // 更新第一个要交换的位置的前一个节点的指针
  96. prevX->next = currY;
  97. } else { // 如果是头部位置,则更新头节点指针
  98. head->next = currY;
  99. }
  100. if (prevY != nullptr) { // 更新第二个要交换的位置的前一个节点的指针
  101. prevY->next = currX;
  102. } else { // 如果是头部位置,则更新头节点指针
  103. head->next = currX;
  104. }
  105. LNode* tmp = currY->next; // 临时保存第二个要交换位置后面节点的指针
  106. currY->next = currX->next;
  107. currX->next = tmp;
  108. return 0;
  109. }
  110. };
  111. int main() {
  112. List LB;
  113. int n, a;
  114. cin >> n;
  115. for (int i = 0; i < n; i++) {
  116. cin >> a;
  117. LB.insert(i + 1, a);
  118. }
  119. LB.print_L();
  120. int t;
  121. cin >> t;
  122. for (int i = 0; i < t; i++) {
  123. int a = LB.change();
  124. if (a == -1)
  125. continue;
  126. LB.print_L();
  127. }
  128. }

这段代码使用了另一种插入和交换方式来实现,但保持了原始功能不变。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?