面试题3:自己实现单链表

  1 #include <cassert>
  2 #include <iostream>
  3 
  4 using namespace std;
  5 
  6 struct SingleListNode {
  7     int val;
  8     SingleListNode *next;
  9     SingleListNode(int x) :
 10             val(x), next(nullptr) {
 11     }
 12     SingleListNode(int x, SingleListNode* pn) {
 13         val = x;
 14         next = pn;
 15     }
 16 };
 17 
 18 class SingleList {
 19 private:
 20     SingleListNode *m_head;
 21     int m_length;
 22 public:
 23     SingleList() {
 24         m_head = nullptr;
 25         m_length = 0;
 26     }
 27     ~SingleList() {
 28         while (m_head) {
 29             SingleListNode *p = m_head->next;
 30             delete m_head;
 31             m_head = p;
 32         }
 33     }
 34     //插入
 35     void insert(int index, const int Ele) {
 36         assert(index > 0 && index < m_length + 2);
 37         if (index == 1) {
 38             m_head = new SingleListNode(Ele, m_head);
 39         } else {
 40             SingleListNode *p = m_head;
 41             while (index > 2) {
 42                 p = p->next;
 43                 index--;
 44             }
 45             p->next = new SingleListNode(Ele, p->next);
 46         }
 47         m_length++;
 48     }
 49 
 50     //删除操作:
 51     //-1:error 0:correct
 52     int erase(int index) {
 53         if (index < 1 || index > m_length) {
 54             return -1;
 55         }
 56         SingleListNode *p = m_head;
 57         if (index == 1) {
 58             m_head = m_head->next;
 59             delete p;
 60         } else {
 61             while (index > 2) {
 62                 index--;
 63                 p = p->next;
 64             }
 65             SingleListNode* tmp = p->next;
 66             p->next = p->next->next;
 67             delete tmp;
 68         }
 69         m_length--;
 70         return 0;
 71     }
 72     int getlength() {
 73         return m_length;
 74     }
 75 
 76     void print() {
 77         SingleListNode* p = m_head;
 78         while (p) {
 79             cout << p->val << "->";
 80             p = p->next;
 81         }
 82         cout << endl;
 83     }
 84 
 85     //插入排序
 86     void insertSort() {
 87         SingleListNode* fake = new SingleListNode(-1, m_head); //建一个假的节点
 88         if (m_head->next == nullptr) return;
 89         SingleListNode* cur = m_head->next;
 90         SingleListNode* cur_next = cur->next;
 91         SingleListNode* cur_pre = m_head;
 92         while (cur) {
 93             SingleListNode *p = fake;
 94             while (p->next != cur) {
 95                 if (cur->val < p->next->val) {
 96                     SingleListNode* tmp = p->next;
 97                     p->next = cur;
 98                     cur->next = tmp;
 99                     cur_pre->next = cur_next;
100                     cur = cur_next;
101                     if (cur)
102                         cur_next = cur->next;
103                     break;
104                 } else {
105                     p = p->next;
106                 }
107             }
108             if (p->next == cur) {
109                 cur_pre = cur;
110                 cur = cur_next;
111                 if (cur)
112                     cur_next = cur->next;
113             }
114         }
115         m_head = fake->next;
116     }
117 
118     //归并排序
119     void mergeSort() {
120         m_head = mergeSort(m_head);
121     }
122 
123     //链表翻转
124     void reverse() {
125         if (!m_head || !m_head->next)
126             return;
127         SingleListNode* cur = m_head->next;
128         SingleListNode* cur_next;
129         m_head->next = nullptr;
130         while (cur) {
131             cur_next = cur->next;
132             cur->next = m_head;
133             m_head = cur;
134             cur = cur_next;
135         }
136     }
137 private:
138     SingleListNode* mergeSort(SingleListNode* head) {
139         if (head == nullptr || head->next == nullptr)
140             return head; //less 2 nodes
141         SingleListNode* slow = head;
142         SingleListNode* fast = head;
143         SingleListNode* pre = head;
144         while (fast && fast->next) {
145             pre = slow;
146             slow = slow->next;
147             fast = fast->next->next;
148         }
149         pre->next = nullptr;
150         cout << slow->val << endl;
151         return merge(mergeSort(head), mergeSort(slow));
152     }
153 
154     SingleListNode* merge(SingleListNode* l1, SingleListNode* l2) {
155         SingleListNode* dummy = new SingleListNode(-1);
156         SingleListNode* cur = dummy;
157         while (l1 && l2) {
158             if (l1->val < l2->val) {
159                 cur->next = l1;
160                 l1 = l1->next;
161             } else {
162                 cur->next = l2;
163                 l2 = l2->next;
164             }
165             cur = cur->next;
166         }
167         if (l1)
168             cur->next = l1;
169         if (l2)
170             cur->next = l2;
171         return dummy->next;
172     }
173 
174 };
175 
176 void insertSort(int a[], int n) {
177     int temp;
178     for (int i = 1; i < n; i++) {
179         temp = a[i];
180         int j = i - 1;
181         while (j > -1 && temp < a[j]) {
182             a[j + 1] = a[j];
183             j--;
184         }
185         a[j + 1] = temp;
186     }
187 }
188 
189 int main() {
190     SingleList l = SingleList();
191     l.insert(1, 2);
192     l.insert(1, 3);
193     l.insert(1, 1);
194     l.insert(2, 0);
195     l.print();
196     l.mergeSort();
197 //    l.insertSort();
198 //    l.mergeSort();
199 //    l.reverse();
200     l.print();
201     return 0;
202 }
原文地址:https://www.cnblogs.com/wxquare/p/6441202.html