链表练习:单向循环链表变双向

已知有一个单向循环链表,其每个结点中含三个域:prior,data 和 next,其中 data 域为数据域,next 为指向后继结点的指针域,prior 也为指针域,但它的值为空 (NULL) ,试编写算法将此单向循环链表改为双向循环链表,即使 prior 成为指向前驱结点的指针域。

输入格式

输入共有三行,第一行为该单向循环链表的长度 n(1≤n≤50);第二行为该单向循环链表的各个元素 aii​​(1≤ai ​​≤1000),它们各不相同且都为数字;第三行为一个数字 m,表示链表中的一个元素值,要求输出时以该元素为起点反向输出整个双向链表。

输出格式

输出为一行,即完成双向链表后以反向顺序输出该链表,每两个整数之间一个空格,最后一个整数后面没有空格。

  1 #include <iostream>
  2 using namespace std;
  3 
  4 class Node {
  5 public:
  6     Node * pre, * next;
  7     int data;
  8     Node(const int &_data) {
  9         data = _data;
 10         next = nullptr;
 11         pre = nullptr;
 12     }
 13 };
 14 
 15 class LinkedList {
 16 private:
 17     Node * head;
 18 public:
 19     LinkedList() {
 20         head = nullptr;
 21     }
 22     ~LinkedList() {
 23         if (head == nullptr) {
 24             return;
 25         }
 26         Node * current_node = head -> next;
 27         head -> next = nullptr;
 28         current_node -> pre = nullptr;
 29         while (current_node != nullptr) {
 30             Node * delete_node = current_node;
 31             current_node = current_node -> next;
 32             delete delete_node;
 33         }
 34     }
 35     void insert(Node *node, int index) {
 36         if (head == nullptr) {
 37             if (index != 0) {
 38                 return;
 39             }
 40             head = node;
 41             head -> next = head;
 42             return;
 43         }
 44         if (index == 0) {
 45             node -> next = head -> next;
 46             head -> next = node;
 47             return;
 48         }
 49         Node * current_node = head -> next;
 50         int count = 0;
 51         while (current_node  != head && count < index - 1) {
 52             current_node = current_node->next;
 53             count++;
 54         }
 55         if (count == index - 1) {
 56             node->next = current_node->next;
 57             current_node->next = node;
 58         }
 59         if (node == head -> next) {
 60             head = node;
 61         }
 62     }
 63     void dualOutput(const int &value) {
 64         if (head == nullptr) {
 65             return;
 66         }
 67         Node * current_node = head;
 68         while (current_node -> data != value) {
 69             current_node = current_node -> next;
 70         }
 71         if (current_node -> data == value) {
 72             Node * end_node = current_node -> next;
 73             while (current_node -> pre != end_node) {
 74                 cout << current_node -> data << " ";
 75                 current_node = current_node -> pre;
 76             }
 77             cout << current_node -> data << " ";
 78             cout << end_node -> data << endl;
 79         }
 80     }
 81     void dualDirection() {
 82         if (head == nullptr) {
 83             return;
 84         }
 85         Node * current_node = head -> next;
 86         while (current_node != head) {
 87             current_node -> next -> pre = current_node;
 88             current_node = current_node -> next;
 89         }
 90         head -> next -> pre = head;
 91     }
 92     void output() {
 93         if (head == nullptr) {
 94             return;
 95         }
 96         Node * current_node = head -> next;
 97         cout << current_node -> data;
 98         while (current_node -> next != head -> next) {
 99             current_node = current_node -> next;
100             cout << " " << current_node -> data;
101         }
102         cout << endl;
103     }
104     void reverseOutput() {
105         if (head == nullptr) {
106             return;
107         }
108         Node * current_node = head;
109         cout << current_node -> data;
110         while (current_node -> pre != head) {
111             current_node = current_node -> pre;
112             cout << " " << current_node -> data;
113         }
114         cout << endl;
115     }
116 };
117 
118 int main() {
119     LinkedList linkedlist;
120     int n;
121     cin >> n;
122     for (int i = 0; i < n; ++i) {
123         int num;
124         cin >> num;
125         auto * node = new Node(num);
126         linkedlist.insert(node, i);
127         //linkedlist.output();
128     }
129 
130     int key;
131     cin >> key;
132     linkedlist.dualDirection();
133     //linkedlist.reverseOutput();
134     linkedlist.dualOutput(key);
135 
136     return 0;
137 }

 

转载于:https://www.cnblogs.com/xudongwei/p/7500662.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值