1. 程式人生 > >鏈表練習:單向循環鏈表變雙向

鏈表練習:單向循環鏈表變雙向

== iostream 指向 put on() 相同 起點 dex void

已知有一個單向循環鏈表,其每個結點中含三個域: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 }

鏈表練習:單向循環鏈表變雙向