實現一個單向鏈表的:建立、插入、刪除、排序(冒泡)、逆向、搜索中間節點 node
#include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; typedef struct student { int data; struct student *next; } node; //建立鏈表 node *create() { //1. 定義變量 node *head = NULL; node *p = NULL; node *pnew = NULL; int x = 0; int cycle = 1; //2. 新建頭節點 head = (node*)malloc(sizeof(node)); p = head; //3. 添加新節點 while (cycle) { printf("input data:"); scanf("%d", &x); if (x != 0) { pnew = (node*)malloc(sizeof(node)); pnew->data = x; p->next = pnew; p = pnew; } else { cycle = 0; } } //4. 釋放頭節點 p->next = NULL; p = head; head = head->next; free(p); p = NULL; //5. 返回鏈表 return head; } //計算鏈表長度 int length(node *head) { //1. 定義變量 node *p = NULL; int n = 0; //2. 遍歷累加 p = head; while (p != NULL) { p = p->next; n++; } printf("%d\n", n); //3. 返回計數 return n; } //顯示 void show(node *head) { //1. 定義變量 node *p = NULL; //2. 遍歷打印 p = head; while(p != NULL) { printf("data:%d ", p->data); p = p->next; } printf("\n"); } //插入節點(升序) node *insert (node *head, int num) { //1. 定義變量 node *p0 = NULL; node *p1 = NULL; node *p2 = NULL; //2. 新建節點 p0 = (node*)malloc(sizeof(node)); p0->data = num; //3. 定位插入位置(升序) p1 = head; while (p0->data > p1->data && p1->next != NULL) { p2 = p1; p1 = p1->next; } //4. 插入節點 if (p0->data > p1->data) //末尾 { p1->next = p0; p0->next = NULL; } else { if (head == p1) //頭 { p0->next = p1; head = p0; } else //中間 { p2->next = p0; p0->next = p1; } } //5. 返回頭 return head; } //刪除鏈表中指定節點 node *del(node *head, int num) { //1. 定義變量 node *p1 = NULL; node *p2 = NULL; //2. 定位刪除位置 p1 = head; while (num != p1->data && p1->next != NULL) { p2 = p1; p1 = p1->next; } //3. 刪除節點 if (num != p1->data) { printf("not found data to delete\n"); } else { if(p1 == head) { head = p1->next; free(p1); p1 = NULL; } else { p2->next = p1->next; free(p1); p1 = NULL; } } //4. 返回頭 return head; } //鏈表升序排序(冒泡算法) node *sort(node *head) { //1. 定義變量 node *p = NULL; int n = 0; int temp = 0; if (head == NULL || head->next == NULL) { return head; } //2. 獲取鏈表長度 n = length(head); //3. 排序 for (int j=1; j<n; ++j) //遍歷全部節點 { p = head; for(int i=0; i<n-j; ++i) //遍歷未排序好的節點 { if (p->data > p->next->data) { temp = p->data; p->data = p->next->data; p->next->data = temp; } p = p->next; } } //4. 返回頭 return head; } //鏈表逆置 node *reverse(node *head) { //1. 定義變量 node *p1 = NULL; node *p2 = NULL; node *p3 = NULL; if (head == NULL || head->next == NULL) { return head; } //2. 逆置 p1 = head; p2 = p1->next; while(p2 != NULL) { p3 = p2->next; p2->next = p1; p1 = p2; p2 = p3; } //3. 調換頭尾節點 head->next = NULL; //轉置完後頭節點成爲尾節點 head = p1; //轉置完後尾節點成爲頭節點 //4. 返回頭 return head; } //搜索鏈表中間節點 //算法:以步長2和1單位同時遍歷鏈表,步長2到末尾,步長1到中間 void searchmid(node *head, node *mid) { //1. 定義變量 node *p1 = NULL; node *p2 = NULL; //2. 定位中間節點 p1 = head; p2 = head; while (p2->next != NULL && p2->next->next != NULL) { p1 = p1->next; mid = p1; p2 = p2->next->next; } printf("mid:%d\n", mid->data); } int main() { node *head = create(); int len = length(head); show(head); head = insert(head, 2); show(head); head = del(head, 2); show(head); head = sort(head); show(head); head = reverse(head); show(head); node *mid; searchmid(head, mid); }