【练习】归并和冒泡两种方法c++将两个无序链表合并为一个升序的有序链表
生活随笔
收集整理的這篇文章主要介紹了
【练习】归并和冒泡两种方法c++将两个无序链表合并为一个升序的有序链表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
定義:
struct node {int data;node* next;};新建有頭指針的鏈表:
struct node *head; head = NULL;//頭指針初始為空 struct node *p; //動態申請一個空間,用來存放一個結點,并用臨時指針p指向這個結點 p=(struct node *)malloc(sizeof(struct node)); scanf("%d",&a); p->data=a;//將數據存儲到當前結點的data域中 p->next=NULL;//設置當前結點的后繼指針指向空,也就是當前結點的下一個結點為空完整版本:
#include <stdio.h> #include <stdlib.h> //這里創建一個結構體用來表示鏈表的結點類型 struct node { int data; struct node *next; }; int main() { struct node *head,*p,*q,*t; int i,n,a; scanf("%d",&n); head = NULL;//頭指針初始為空 for(i=1;i<=n;i++)//循環讀入n個數 { scanf("%d",&a); //動態申請一個空間,用來存放一個結點,并用臨時指針p指向這個結點 p=(struct node *)malloc(sizeof(struct node)); p->data=a;//將數據存儲到當前結點的data域中 p->next=NULL;//設置當前結點的后繼指針指向空,也就是當前結點的下一個結點為空 if(head==NULL) head=p;//如果這是第一個創建的結點,則將頭指針指向這個結點 else q->next=p;//如果不是第一個創建的結點,則將上一個結點的后繼指針指向當前結點 q=p;//指針q也指向當前結點 } //輸出鏈表中的所有數 t=head; while(t!=NULL) { printf("%d ",t->data); t=t->next;//繼續下一個結點 } getchar();getchar(); return 0; }合并采用先分別升序后再進行合并兩個有序鏈表,分別升序采用雙指針和遞歸調用。先看用歸并排序:
用歸并排序:
node* merge(node* p,node*p2) {//這里是合并的排序node* src = new node;src->next = nullptr;//src->data = 0;node* now = src;node* a1 = p;node* a2 = p2;while (a1 != NULL && a2 != NULL) {if (a1->data >= a2->data) {node* tem = a2->next;src->next = a2;a2 = tem;}else {node* tem = a1->next;src->next = a1;a1 = tem;}src = src->next;//合并兩個有序鏈表 src->next=nullptr}if (a1 != NULL) src->next = a1;if (a2 != NULL)src->next = a2;return now->next;} node* merge_sort(node* p) {//這是將一個鏈表進行升序if (p == nullptr || p->next == nullptr) return p;node* fast = p;node* slow = new node;slow->data = 0;slow->next = p;while (fast != NULL && fast->next != NULL) {fast = fast->next->next;slow = slow->next;}node* rhead = slow->next;slow->next = NULL;node* lh = merge_sort(p);node* rh = merge_sort(rhead);node* after = merge(lh, rhead);return after; } int main() {srand(0);node* m = new node;m->data = rand()%5;//cout << m->data;node* n = new node;n->data = rand()%7;m->next = n;node* nn = new node;nn->data = rand()%8;n->next = nn;nn->next = nullptr;node* copy_m = m;cout << "第一個鏈表:";while (copy_m) {cout << copy_m->data;copy_m = copy_m->next;}cout << endl;node* m2 = new node;m2->data =rand()%18;node* n2 = new node; n2->data = rand() % 13;m2->next = n2;node* nn2 = new node;nn2->data = rand()%15;n2->next = nn2;nn2->next = nullptr;node* copy_n = m2;cout << "第二個鏈表:";while (copy_n) {cout << copy_n->data;copy_n = copy_n->next;}cout << endl;node*one = merge_sort(m);node*two= merge_sort(m2);node* new_m = merge(one,two);node* nice = new_m;cout << endl;cout << "合并兩個有序鏈表后的結果:";while (nice) {cout << nice->data << "->";nice = nice->next;} }結果:
用冒泡排序:
node* merge_sort(node* p) {//這是將一個鏈表進行升序if (p == nullptr || p->next == nullptr) return p;node* mana = p;node* pp = mana;for (int i = 0; i < len(p) - 1; i++) {mana = p;for (int j = 0; j < len(p) - i - 1; j++) {node* t1 = mana;node* t2 = mana->next;if (t1->data > t2->data) {int tem = t1->data;t1->data = t2->data;t2->data = tem;}mana = mana->next;}}return pp; } int main() {srand(0);node* m = new node;m->data = rand()%12;//cout << m->data;node* n = new node;n->data = rand()%17;m->next = n;node* nn = new node;nn->data = rand()%18;n->next = nn;node* nnn = new node;nnn->data = rand() % 23;nnn->next = NULL;nn->next = nnn;node* copy_m = m;cout << "第一個鏈表:";while (copy_m) {cout << copy_m->data<<"-";copy_m = copy_m->next;}cout << endl;node* m2 = new node;m2->data =rand()%18;node* n2 = new node; n2->data = rand() % 13;m2->next = n2;node* nn2 = new node;nn2->data = rand()%15;n2->next = nn2;nn2->next = nullptr;node* copy_n = m2;cout << "第二個鏈表:";while (copy_n) {cout << copy_n->data << "-";copy_n = copy_n->next;}cout << endl;node*one = merge_sort(m);node*two= merge_sort(m2);node* new_m = merge(one,two);node* nice = new_m;cout << endl;cout << "合并兩個有序鏈表后的結果:";while (nice) {cout << nice->data << "->";nice = nice->next;} }總結
以上是生活随笔為你收集整理的【练习】归并和冒泡两种方法c++将两个无序链表合并为一个升序的有序链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【笔记】python os的使用 文件批
- 下一篇: 【练习】c++删除链表倒数第K个结点和