要完整准确的 均以单链表作存储结构 试编写算法将A表和B表归并成一个按元素值递减有序的线性表C 【数据结构】假设有两个按元素值递增有序的线性表A和B 并要求利用原表的空间存放C。谁会么

p : q; if(t == p) p = p->next; else q = q->next; tail->next = t; tail = t; } } //将剩余的直接接过去即可; if(p == 0) tail->next = q; else tail->next = p;}int main(){ Node* List1 = 0 *tail; p = List1 *q Node*& List3){ //这里做法类似归并排序 Node* p Node*&List2 *temp; p = List; //第一个节点 q = p->next; //第二个节点 temp = q->next; //第三个节点 List->next = 0; while(temp) { q->next = p; p = q; q = temp; temp = temp->next; } List = q; q->next = p;}void Merge(Node*& List1 *q 实际上就是将递增序列变为递减序列{ Node* p p->data); } }}void Converse(Node*& List) //将链表中指针逆向 List = q; else { tail->next = q; tail = tail->next; } }}void Sort(Node*& List){ for(Node* p = List; p sort ascending. end by -1: /n"); scanf("%d" &x); if (x 代码如下:#include <iostream>using namespace std;const int N = 8;struct Node{ int data; Node* next; Node(int d = 0 我用C++实现了一下想法 你一看因该能明白 将两个链合并; 思路比较简单 这样就使原来两链表由递增序列变成了递减序列;第二步在根据合并排序的思想 则禁止加入列表*/ printf("/n Error input =NULL){ q=h2; h2=h2->next; q->next=h->next; h->next=q; } return h;}main(){ int select=0; LinkL *ha LinkL *h2){ /*本程序合并两个升序链表 =NULL){ q=h1; h1=h1->next; q->next=h->next; h->next=q; } while(h2 =NULL){ if (h1->d <= h2->d) {q=h1; h1=h1->next;} else {q=h2; h2=h2->next;} q->next=h->next; h->next=q; } while(h1 =NULL && h2 /n"); exit(0);} printf("/n The result is: /n"); printL(h); getch();}百分百正确 hb); else { printf("/n Error Select hb); else if (select==2) h=mergeList2(ha &select); if (select==1) h=mergeList1(ha *h; ha=creatSortList(); hb=creatSortList(); printf("/n this program will Merge two List: /n"); printf(" 1: the resule is ascending List: /n"); printf(" 2: the resule is descending List: /n"); gets(); printf(" Please input your select: "); scanf("%d" *hb *q; h=h1; h1=h1->next; h2=h2->next; h->next=NULL; while (h1 =NULL){ if (h1->d <= h2->d)/*选出小的并标记为q terminate =NULL && h2 =-1) { if (x1>x) { /*如果违反有序 选中的列表指针后移*/ {q=h1; h1=h1->next;} else {q=h2; h2=h2->next;} tail->next=q; tail=q; } if (h1==NULL) tail->next=h2; else tail->next=h1; return h;}LinkL * mergeList2(LinkL *h1 *tail LinkL *h2){ /*本程序合并两个升序链表 结果为 降序 链表 ===头插法*/ LinkL *h *q; h=h1; tail=h; h1=h1->next; h2=h2->next; while (h1 =NULL){ printf(" %d->" &x); continue; } Q=malloc(sizeof(LinkL)); Q->d=x; Q->next=NULL; tail->next=Q; tail=Q; x1=x; scanf("%d" &x); while (x = 0; q = q->next) { if(q->data < p->data) swap(q->data next(n){}};void Print(Node* p){ do { cout << p->data << " "; p = p->next ; }while(p); cout << endl;}void Create(Node*& List){ int i; Node* tail = 0; for(i = 0; i < N; i++) { Node* q = new Node; q->data = (rand()%100); if(tail == 0) tail = q Node* n = 0):data(d) 下面大体介绍一下基本思想:由于是递增的单链表:0->0->0->0 这种结构不能逆向反问;所以直接做操作是很不好实现的;所以我第一步是将两链表的指针反转 结果为升序链表 ===尾插法*/ LinkL *h p->d); p=p->next;} printf("^/n");}LinkL * mergeList1(LinkL *h1 &x); } return h;}printL(LinkL *h){ LinkL *p; p=h->next; while (p = 0; p = p->next) { for(Node* q = p; q =-1) { /* 创建第一个结点 */ Q=malloc(sizeof(LinkL)); Q->d=x; Q->next=NULL; tail->next=Q; tail=Q; x1=x; } scanf("%d" end by -1: /n"); h= (LinkL *) malloc(sizeof(LinkL)); h->next=NULL; tail=h; scanf("%d" *tail; /* 请输入有序表中的各元素 */ printf("/n input some Nums by ascend *Q x1; LinkL *h List3); cout << "合并结果如下:" << endl; Print(List3); system("pause"); return 0;} ||| /*假定此题的单链表带有头结点 p : q; if(List3 == p) p = p->next; else q = q->next; List3->next = 0; tail = List3; } else { Node* t = p->data > q->data 本程序原地合并两个有序表 */#include <stdlib.h>#include <conio.h>typedef struct L{ int d; struct L *next;}LinkL;LinkL *creatSortList(){/* 本函数创建一个升序的链表 */ int x List2 *List3 = 0; Create(List1); Create(List2); Sort(List1); Sort(List2); cout << "两个递增序列如下:" << endl; Print(List1); Print(List2); cout << "反转后的序列如下: " << endl; Converse(List1); Converse(List2); Print(List1); Print(List2); Merge(List1 *List2 = 0 q = List2; while(p && q) { if(List3 == 0) { List3 = p->data > q->data
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值