單向鏈表的代碼實現:node
#include <bits/stdc++.h> using namespace std; /** * 單向鏈表的實現 */ struct Node { int val; Node* next; }; Node* head; int sz; void init() { head = new Node; head->next = NULL; sz = 0; } void add(int pos, int val) { // 用於向單向鏈表的 pos 位置插入一個元素 if (pos > sz) { puts("illegal"); return; } sz ++; Node* tmp = head; for (int i = 0; i < pos; i ++) tmp = tmp->next; Node* new_node = new Node; new_node->val = val; new_node->next = tmp->next; tmp->next = new_node; } void del(int pos) { // 用於刪除單向鏈表的 pos 位置的元素 if (pos >= sz) { puts("illegal"); return; } sz --; Node* tmp = head; for (int i = 0; i < pos; i ++) tmp = tmp->next; tmp->next = tmp->next->next; } void output() { // 輸出單向鏈表中的全部元素 Node* tmp = head; for (int i = 0; i < sz; i ++) { if (i) cout << " "; tmp = tmp->next; cout << tmp->val; } cout << endl; } string s; int pos, val; int main() { init(); while (cin >> s) { if (s == "add") { cin >> pos >> val; add(pos, val); } else if (s == "del") { cin >> pos; del(pos); } else if (s == "output") { output(); } else if (s == "size") { cout << sz << endl; } } return 0; }
雙向鏈表的代碼實現:c++
#include <bits/stdc++.h> using namespace std; /** * 雙向鏈表的實現 */ struct DNode { int val; DNode* pre; DNode* next; }; DNode* head; int sz; void init() { head = new DNode; head->pre = head->next = NULL; sz = 0; } void add(int pos, int val) { // 用於向單向鏈表的 pos 位置插入一個元素 if (pos > sz) { puts("illegal"); return; } sz ++; DNode* tmp = head; for (int i = 0; i < pos; i ++) tmp = tmp->next; DNode* new_node = new DNode; new_node->val = val; new_node->next = tmp->next; new_node->pre = tmp; if (tmp->next != NULL) tmp->next->pre = new_node; tmp->next = new_node; } void del(int pos) { // 用於刪除單向鏈表的 pos 位置的元素 if (pos >= sz) { puts("illegal"); return; } sz --; DNode* tmp = head; for (int i = 0; i < pos; i ++) tmp = tmp->next; if (tmp->next->next != NULL) tmp->next->next->pre = tmp; tmp->next = tmp->next->next; } void d_solve(int val) { // 這個函數的做用是找到雙向鏈表中第一個值爲val的元素,輸出它左右兩個元素的值 for (DNode* tmp = head->next; tmp != NULL; tmp = tmp->next) { if (tmp->val == val) { if (tmp->pre == head) cout << "no"; else cout << tmp->pre->val; if (tmp->next == NULL) cout << " no" << endl; else cout << " " << tmp->next->val << endl; return; } } puts("none"); } void output() { // 輸出單向鏈表中的全部元素 DNode* tmp = head; for (int i = 0; i < sz; i ++) { if (i) cout << " "; tmp = tmp->next; cout << tmp->val; } cout << endl; } string s; int pos, val; int main() { init(); while (cin >> s) { if (s == "add") { cin >> pos >> val; add(pos, val); } else if (s == "del") { cin >> pos; del(pos); } else if (s == "output") { output(); } else if (s == "size") { cout << sz << endl; } else if (s == "find") { cin >> val; d_solve(val); } } return 0; }
循環鏈表的代碼實現:算法
#include <bits/stdc++.h> using namespace std; /** * 循環鏈表的實現 */ struct Node { int val; Node* next; }; Node* head; int sz; void init() { head = new Node; head->next = head; sz = 0; } void add(int pos, int val) { // 用於向循環鏈表的 pos 位置插入一個元素 if (pos > sz) { puts("illegal"); return; } sz ++; Node* tmp = head; for (int i = 0; i < pos; i ++) tmp = tmp->next; Node* new_node = new Node; new_node->val = val; new_node->next = tmp->next; tmp->next = new_node; } void del(int pos) { // 用於刪除循環鏈表的 pos 位置的元素 if (pos >= sz) { puts("illegal"); return; } sz --; Node* tmp = head; for (int i = 0; i < pos; i ++) tmp = tmp->next; tmp->next = tmp->next->next; } void output() { // 輸出循環鏈表中的全部元素 Node* tmp = head; for (int i = 0; i < sz; i ++) { if (i) cout << " "; tmp = tmp->next; cout << tmp->val; } cout << endl; } void output_num(int num) { // 輸出循環鏈表中的前 n 個元素,由於是循環鏈表,因此若是鏈表中只有兩個元素:2,1;可是num==5的狀況下,會輸出2 1 2 1 2。 if (sz == 0) { cout << "none" << endl; return; } Node* tmp = head; for (int i = 0; i < num; i ++) { tmp = tmp->next; if (tmp == head) { i --; continue; } if (i) cout << " "; cout << tmp->val; } cout << endl; } string s; int pos, val; int main() { init(); while (cin >> s) { if (s == "add") { cin >> pos >> val; add(pos, val); } else if (s == "del") { cin >> pos; del(pos); } else if (s == "output") { output(); } else if (s == "output_num") { int num; cin >> num; output_num(num); } else if (s == "size") { cout << sz << endl; } } return 0; }