要完整准确的 均以单链表作存储结构 试编写算法将A表和B表归并成一个按元素值递减有序的线性表C 【数据结构】假设有两个按元素值递增有序的线性表A和B 并要求利用原表的空间存放C。谁会么
2008-10-20 09:36
956 查看
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
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
相关文章推荐
- 假设有两个按元素值递增有序排列的线性表A和B,均以单链表作存储结构,请编写算法将A表和B表归并为一个按元素值递减 有序(即非递增有序,允许表中含有值相同的元素)排列的线性表C,并要求利用原装(即A表和
- 假设有两个按元素值递增有序排列的线性表A和B,均以单链表作存储结构,请编写算法将A表和B表归并为一个按元素值递减 有序(即非递增有序,允许表中含有值相同的元素)排列的线性表C,并要求利用原装(即A表和
- 假设有两个按元素值递增有序排列的线性表A和B,均以单链表作存储结构,请编写算法将A表和B表归并为一个按元素值递减 有序(即非递增有序,允许表中含有值相同的元素)排列的线性表C,并要求利用原装(即A表和
- 数据结构链表习题2.27,假设以两个元素依值递增有序排列的线性表A和B分别表示两个集合,现要求另辟空间构成一个顺序链表
- 假设以两个元素依值递增有序排列的线性表A和B分别表示两个集合,现要求另辟空间构成一个顺序链表C
- 写程序。设ha和hb分别是两个带头结点的非递减有序单链表的头指针,试设计算法,将这两个有序链表合并成一个非递增有序的单链表。要求使用原链表空间,
- 将两个递增的链表合并成一个递减的链表(相同的元素只留下一个),并要求利用原表结点
- 设A和B是两个单链表,其表中元素递增有序,使用一算法将A和B归并成一个案元素递减有序的单链表C(该程序中的功能包含了上几个博客中的部分功能)
- 将两个递增有序的单链表合并成一个递减有序的单链表,利用原结点空间
- 将两个递增有序的单链表合并成一个递减有序的单链表,利用原结点空间
- 已知线性表中元素以值递增有序排列,并以单链表作为存储结构,设计一个算法,删除表中值相同的多余元素
- 编写算法,对非递减顺序存储的线性表操作,将表中相等的多余元素删除,变为严格递增
- 已知两个链表 la和 lb,其元素值递增排序。编程将la和lb合并成一个递减有序(相同值元素只保留一个)的链表lc。(北方名校经典试题) 本题选做
- 假设利用两个线性表La和Lb分别表示两个集合A和B(即线性表中的数据元素即为集合中的成员),现要求一个新的 集合A=AUB。这就要求对线性表作如下操作:扩大线性表La,将存在于线性表Lb中而不存在于线
- 数据结构习题分析:已知由一个线性链表表示的线性表中含有三类字符的数据元素,是编写算法将该线性表分割为三个循环链表,其中每个。。。。。
- 归并两个递增序列链表为一个递减有序链表
- 已知两个链表A和B分别表示两个集合,其元素递增排列。请设计算法求出两个集合A和B的差集(即仅由在A中出现而不在B中出现的元素所构成的集合),并以同样的形式存储,同时返回该集合的元素个数。
- 将两个非递减的有序链表合并为一个非递增的有序链表
- 采用邻接表存储结构,编写一个判别无向图中任意给定的两个顶点之间是否存在一条长度为k的简单路径的算法。
- 数据结构——将两个有序链表并为一个有序链表算法