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;

public:

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

};

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 result = LB.change();
  15. if (result == -1)
  16. continue;
  17. LB.print_L();
  18. }
  19. return 0;

} “`


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?