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 List { protect...

以下是使用另一种方法实现该代码,使用双指针法进行插入和交换操作:

”`cpp #ifndef NULL #define NULL 0 #endif #include using namespace std;

//定义单链表结点结构 struct LNode {

  1. int data;
  2. LNode* next;
  3. LNode() {};
  4. LNode(int e, LNode* n = NULL) :data(e), next(n) {};

};

//单链表list类创建 class List { protected:

  1. LNode* head;
  2. int _size;
  3. void init() {
  4. head = new LNode;
  5. head->next = NULL;
  6. _size = 0;
  7. }

public:

  1. //构造函数
  2. List(){init();}
  3. //析构函数
  4. ~List() {
  5. LNode* p = head;
  6. while (head)
  7. {
  8. p = p->next;
  9. delete head;
  10. head = p;
  11. }
  12. };
  13. //头节点
  14. List(int info) { //使用头结点储存某些信息,不随链表插入改变位置
  15. init();
  16. head->data = info;
  17. }
  18. int& info() { return head->data; } //返回链表头结点的信息
  19. //在指定位置插入元素(双指针法)
  20. void insert(int index, int a)
  21. {
  22. if (index < 1 || index > _size + 1)
  23. {
  24. return; // 无效的插入位置,直接返回
  25. }
  26. LNode* prev = nullptr; // 前一个节点指针
  27. LNode* curr = head; // 当前节点指针
  28. LNode* tmp = new LNode(a); // 新插入的节点
  29. // 找到要插入位置的前一个节点和当前节点
  30. for (int i = 1; i < index && curr; i++)
  31. {
  32. prev = curr;
  33. curr = curr->next;
  34. }
  35. tmp->next = curr; // 新插入节点指向当前节点
  36. if (prev)
  37. prev->next = tmp; // 前一个节点指向新插入节点
  38. else
  39. head->next = tmp; // 头结点指向新插入节点
  40. _size++;
  41. }
  42. void print_L()
  43. {
  44. LNode* p = head->next;
  45. while (p != NULL)
  46. {
  47. cout << p->data << " ";
  48. p = p->next;
  49. }
  50. cout << endl;
  51. }
  52. int change()
  53. {
  54. int x, y;
  55. cin >> x >> y;
  56. if (x < 0 || x > _size || y < 0 || y > _size)
  57. {
  58. cout << "error" << endl;
  59. return -1;
  60. }
  61. LNode *curr_x, *prev_x, *curr_y, *prev_y;
  62. curr_x = prev_x = head;
  63. for (int i = 1; i <= x && curr_x != nullptr; i++)
  64. {
  65. prev_x=curr_x ;
  66. curr_x=curr_x ->next ;
  67. }
  68. curr_y = prev_y= head;
  69. for (int j = 1; j <= y && curr_y != nullptr ; j++)
  70. {
  71. prev_y=curr_y ;
  72. curr_y=curr_y ->next ;
  73. }
  74. if (curr_x == nullptr || curr_y == nullptr )
  75. {
  76. cout << "error" << endl;
  77. return -1;
  78. }
  79. if(curr_x!=head)
  80. prev_x->next = curr_y;
  81. else
  82. head->next=curr_y;
  83. prev_y->next = curr_x;
  84. LNode *tmp = curr_x->next;
  85. curr_x->next = curr_y->next;
  86. curr_y->next = tmp;
  87. return 0;
  88. };

};

int main() {

  1. List LB;
  2. int n, a;
  3. cin >> n;
  4. for (int i = 0; i < n; i++)
  5. {
  6. cin >> a;
  7. LB.insert(i + 1, a);
  8. }
  9. LB.print_L();
  10. int t;
  11. cin >> t;
  12. for (int i = 0; i < t; i++)
  13. {
  14. int a = LB.change();
  15. if (a == -1)
  16. continue;
  17. LB.print_L();
  18. }

} “`


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?