ZBLOG

用双向链表实现任意插入新的双向链表

下面是一个完整的C++实现,用于在双向链表中插入另一个双向链表。这种情况下,我们将实现一个方法来将新链表插入到任意位置。

双向链表节点结构体

首先定义双向链表节点的结构体:

  1. #include <iostream>
  2. struct Node {
  3. int data;
  4. Node* prev;
  5. Node* next;
  6. Node(int val) : data(val), prev(nullptr), next(nullptr) {}
  7. };

双向链表类

接下来,我们定义一个双向链表类,包含基本操作和插入另一个链表的方法:

  1. class DoublyLinkedList {
  2. public:
  3. Node* head;
  4. DoublyLinkedList() : head(nullptr) {}
  5. // 向末尾插入元素
  6. void insertAtEnd(int value) {
  7. Node* newNode = new Node(value);
  8. if (head == nullptr) { // 空链表情况
  9. head = newNode;
  10. } else {
  11. Node* last = head;
  12. while (last->next != nullptr) {
  13. last = last->next;
  14. }
  15. last->next = newNode;
  16. newNode->prev = last;
  17. }
  18. }
  19. // 在指定位置插入新的链表
  20. void insertListAtPosition(DoublyLinkedList& newList, int position) {
  21. if (position == 0) { // 如果要在头部插入整个新链表
  22. if (newList.head != nullptr) {
  23. newList.head->next = head;
  24. if (head != nullptr) {
  25. head->prev = newList.head;
  26. }
  27. head = newList.head;
  28. }
  29. } else {
  30. Node* current = head;
  31. for (int i = 0; i < position - 1 && current != nullptr; ++i) {
  32. current = current->next;
  33. }
  34. if (current == nullptr) { // 超出范围,直接返回
  35. std::cerr << "Position out of bounds. Not inserting the list." << std::endl;
  36. return;
  37. }
  38. // 插入新的列表到当前位置之前
  39. if (newList.head != nullptr) {
  40. Node* tempNext = current->next;
  41. current->next = newList.head;
  42. newList.head->prev = current;
  43. if (tempNext != nullptr) {
  44. tempNext->prev = getTail(newList);
  45. getTail(newList)->next = tempNext;
  46. }
  47. }
  48. }
  49. // 清理新列表的head指针,以避免重复引用。
  50. newList.head = nullptr;
  51. }
  52. // 获取当前列表的尾部节点
  53. Node* getTail(DoublyLinkedList& list) {
  54. Node* tailNode = list.head;
  55. while(tailNode && tailNode->next != nullptr){
  56. tailNode=tailNode->next;
  57. }
  58. return tailNode;
  59. }
  60. void display() const {
  61. Node* temp = head;
  62. while (temp != nullptr) {
  63. std::cout << temp->data << " ";
  64. temp = temp->next;
  65. }
  66. std::cout << std::endl;
  67. }
  68. };

示例程序

最后,在main函数中演示如何使用这些类和方法:

  1. int main() {
  2. DoublyLinkedList dll1, dll2;
  3. dll1.insertAtEnd(10);
  4. dll1.insertAtEnd(20);
  5. std::cout << "Original List 1: ";
  6. dll1.display();
  7. dll2.insertAtEnd(30);
  8. dll2.insertAtEnd(40);
  9. std::cout << "Original List 2: ";
  10. dll2.display();
  11. // 在dll1的位置1处插入dll2
  12. dll1.insertListAtPosition(dll2, 1);
  13. std::cout << "After inserting List 2 into List 1 at position 1: ";
  14. dll1.display();
  15. return 0;
  16. }

程序说明

  • DoublyLinkedList:管理双向链表及其操作,包括添加元素、显示内容以及在特定位置插入另一条链接。
  • insertAtEnd方法:用于在当前列表末尾添加元素。
  • insertListAtPosition方法:接受另一个双向链表并将其插入到当前链表的特定位置。如果该位置为零,则将新列表添加到头部。
  • getTail方法:获取指定列表的最后一个节点。

通过这个示例,你可以方便地将任意两个双向链表合并。你也可以根据需要扩展功能,例如删除某个节点、查找等。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?