#include <iostream>using namespace std;struct node{ int data; node *next;};node *create_list(){ node *head = NULL, *p = NULL, *s = NULL; int len, val; head = (node *)malloc(sizeof(node)); if (NULL == head) { cout << "分配內存失敗,程序終止!" << endl; exit(-1);//exit用于在程序運行的過程中隨時結束程序,參數-1表示非正常退出 } p = head; p->next = NULL; cout << "len="; cin >> len; for (int i = 0;i<len;++i) { cout << "輸入第" << i + 1 << "個節點的值:"; cin >> val; s = (node *)malloc(sizeof(node)); if (NULL == s) { cout << "分配內存失敗,程序終止!" << endl; exit(-1); } s->data = val; p->next = s;//p指針起到連接節點的作用,s指針是生產指針 s->next = NULL; p = s; } head = head->next;//指向首節點 p->next = NULL; return head;}int list_length(node *head){ node *p = head; int n = 0; while (p != NULL) { p = p->next; n++; } return n;}void PRint_list(node *head){ node *p = head;//p為遍歷指針 while (NULL != p) { cout << p->data; p = p->next; } cout << endl; return;}node *reverse_list(node *head){ node *p1, *p2, *p3; if (NULL == head || NULL == head->next) return head; p3 = head, p2 = p3->next; while (p2) { p1 = p2->next;//p1先移向p2的下一個節點,先遣指針,以方便后面判斷p2是否為NULL p2->next = p3;//指針調轉 p3 = p2;//p2,p3同時向前移動,p3最終返回逆序后鏈表的首節點的地址 p2 = p1; } head->next = NULL; head = p3;//p1最終指向最后一個節點,也就是逆置之后的鏈表首節點 return head;}node *sort_list(node *head){ node *p = head; int len = list_length(head); int temp; if (NULL == p || NULL == p->next) return head; for (int i = 0;i<len - 1;++i) { for (int j = 0;j<len - 1 - i;++j) { if (NULL != p->next)//安全檢查,到達鏈表末尾則此趟就不進行比較 { if (p->data>p->next->data) { temp = p->data; p->data = p->next->data; p->next->data = temp; } p = p->next; } } p = head;//一趟比較完后再從頭開始比較 } return head;}node *insert_list(node *head, int val){ node *p1 = head; node *p2 = NULL; node *s = NULL; s = (node *)malloc(sizeof(node)); s->data = val; while (s->data>p1->data&&p1->next != NULL) {//除了頭節點和首節點后面還有節點,且插入值大于首節點值及后面的節點值,就雙指針向后移動 p2 = p1; p1 = p1->next; } if (s->data <= p1->data) {//雙指針后移,直到插入值小于或等于后面的節點值 if (head == p1) {//除頭節點,只有首節點,且插入值小于首節點值,插入到首節點之前 s->next = p1; head = s; } else {//插入到中間節點 p2->next = s; s->next = p1; } } else {//插入到尾節點之后 p1->next = s; s->next = NULL; } return head;}node *del_list(node *head, int val){ node *p1 = head; node *p2 = NULL; p1 = head; while (val != p1->data&&p1->next != NULL) { p2 = p1; p1 = p1->next; } if (val == p1->data) { if (p1 == head) { head = p1->next;//只有頭節點,則刪除它 free(p1); p1 = NULL; } else { p2->next = p1->next;//刪除p1所指向的節點 free(p1); p1 = NULL;//防止野指針 } } else//如果不是出現要刪除的整數使while循環停止,那么就是就是出現了p1->next==NULL,到達鏈表末尾,就說明沒有出現該值 cout << endl << val << "could not been found" << endl; return head;}node *combine_list(node *head1, node *head2){ if (NULL == head1)return head2; if (NULL == head2)return head1; node *head = NULL; if (head1->data<head2->data) { head = head1; head->next = combine_list(head1->next, head2);//遞歸實現 } else { head = head2; head->next = combine_list(head1, head2->next); } return head;}int main(){ node *head = NULL; head = create_list(); cout << "生成1:"; print_list(head); node *head1 = NULL; head1 = create_list(); cout << "生成2:"; print_list(head1); node *head2 = NULL; head2 = combine_list(head, head1); cout << "合并:"; print_list(head2); head = sort_list(head); cout << "排序:"; print_list(head); head = insert_list(head, 2); cout << "插入:"; print_list(head); head = del_list(head, 2); cout << "刪除:"; print_list(head); head = reverse_list(head); cout << "逆置:"; print_list(head); return 0;}
新聞熱點
疑難解答