zoukankan      html  css  js  c++  java
  • 假设有两个按元素值递增有序排列的线性表A和B,均以单链表作存储结构,请编写算法将A表和B表归并为一个按元素值递减 有序(即非递增有序,允许表中含有值相同的元素)排列的线性表C,并要求利用原装(即A表和

    假设有两个按元素值递增有序排列的线性表A和B,均以单链表作存储结构,请编写算法将A表和B表归并为一个按元素值递增 有序(即非递增有序,允许表中含有值相同的元素)排列的线性表C,并要求利用原装(即A表和B表)的结点空间构造C表。

    采用的方法:

    尾插法

    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <ctime>
    using namespace std;
    
    typedef char ElemType;
    
    typedef struct Node{
    	ElemType data;
    	struct Node *next;
    }Node, *LinkList;
    
    LinkList CreateList()
    {
    	LinkList L;
    	ElemType c;
    	L = (LinkList)malloc(sizeof(Node));
    	L->next = NULL;
    	Node *p , *tail;
    	tail = L;
    	c = getchar();
    	while(c != '#')
    	{
    		p = (Node *)malloc(sizeof(Node));
    		p->data = c;
    		tail->next = p;
    		tail = p;
    		c = getchar();
    	}
    	tail->next = NULL;
    	return L;
    }
    
    void ShowList(LinkList L)
    {
    	Node *p;
    	p = L->next;
    	while(p != NULL)
    	{
    		cout << p->data << " ";
    		p = p->next;
    	}
    	cout << endl;
    }
    
    LinkList MergeList(LinkList LA, LinkList LB)
    {
    	LinkList LC;
    	Node *pa, *pb, *r;
    	pa = LA->next;
    	pb = LB->next;
    	LC = LA;
    	LC->next = NULL;
    	r = LC;
    	while(pa != NULL && pb != NULL)
    	{
    		if(pa->data <= pb->data)
    		{
    			r->next = pa;
    			r = pa;
    			pa = pa->next;
    		}
    		else
    		{
    			r->next = pb;
    			r = pb;
    			pb = pb->next;
    		}
    		if(pa)
    		{
    			r->next = pa;
    		}
    		else
    		{
    			r->next = pb;	
    		}
    	}
    	return LC;
    }
    
    int main()
    {
    	LinkList LA;
    	LA = CreateList();
    	getchar();
    	LinkList LB;
    	LB = CreateList();
    
    	cout << "LA:" << endl;
    	ShowList(LA);
    
    	cout << "LB:" << endl;
    	ShowList(LB);
    
    	LinkList LC;
    	LC = MergeList(LA, LB);
    	cout << "MergeList:" << endl;
    	ShowList(LC);
    
    	return 0;
    }
    





    假设有两个按元素值递增有序排列的线性表A和B,均以单链表作存储结构,请编写算法将A表和B表归并为一个按元素值递减 有序(即非递增有序,允许表中含有值相同的元素)排列的线性表C,并要求利用原装(即A表和B表)的结点空间构造C表。

    采用的方法:

    即先进行线性表的逆置,在进行合并

    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <ctime>
    using namespace std;
    
    typedef char ElemType;
    
    typedef struct Node{
    	ElemType data;
    	struct Node *next;
    }Node, *LinkList;
    
    LinkList CreateList()
    {
    	LinkList L;
    	ElemType c;
    	L = (LinkList)malloc(sizeof(Node));
    	L->next = NULL;
    	Node *p , *tail;
    	tail = L;
    	c = getchar();
    	while(c != '#')
    	{
    		p = (Node *)malloc(sizeof(Node));
    		p->data = c;
    		tail->next = p;
    		tail = p;
    		c = getchar();
    	}
    	tail->next = NULL;
    	return L;
    }
    
    void ShowList(LinkList L)
    {
    	Node *p;
    	p = L->next;
    	while(p != NULL)
    	{
    		cout << p->data << " ";
    		p = p->next;
    	}
    	cout << endl;
    }
    
    void ReverseList(LinkList L)
    {
    	Node *p, *q;
    	p = L->next;
    	L->next = NULL;
    	while(p != NULL)
    	{
    		q = p->next;
    		p->next = L->next;
    		L->next = p;
    		p = q;
    	}
    
    }
    
    LinkList MergeList1(LinkList LA, LinkList LB)
    {
    	LinkList LC;
    	Node *pa, *pb, *r;
    	pa = LA->next;
    	pb = LB->next;
    	LC = LA;
    	LC->next = NULL;
    	r = LC;
    	while(pa != NULL && pb != NULL)
    	{
    		if(pa->data <= pb->data)
    		{
    			r->next = pa;
    			r = pa;
    			pa = pa->next;
    		}
    		else
    		{
    			r->next = pb;
    			r = pb;
    			pb = pb->next;
    		}
    		if(pa)
    		{
    			r->next = pa;
    		}
    		else
    		{
    			r->next = pb;	
    		}
    	}
    	return LC;
    }
    
    LinkList MergeList2(LinkList LA, LinkList LB)
    {
    	ReverseList(LA);
    	ReverseList(LB);
    	LinkList LC;
    	Node *pa, *pb, *r;
    	pa = LA->next;
    	pb = LB->next;
    	LC = LA;
    	LC->next = NULL;
    	r = LC;
    	while(pa != NULL && pb != NULL)
    	{
    		if(pa->data <= pb->data)
    		{
    			r->next = pb;
    			r = pb;
    			pb = pb->next;
    		}
    		else
    		{
    			r->next = pa;
    			r = pa;
    			pa = pa->next;
    		}
    		if(pa)
    		{
    			r->next = pa;
    		}
    		else
    		{
    			r->next = pb;	
    		}
    	}
    	return LC;
    }
    
    int main()
    {
    	LinkList LA;
    	LA = CreateList();
    	getchar();
    	LinkList LB;
    	LB = CreateList();
    
    	cout << "LA:" << endl;
    	ShowList(LA);
    
    	cout << "LB:" << endl;
    	ShowList(LB);
    
    	LinkList LC;
    	LC = MergeList2(LA, LB);
    	cout << "MergeList2:" << endl;
    	ShowList(LC);
    
    	return 0;
    }
    



  • 相关阅读:
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    cookie相关内容:用法,特点,常用功能以及与session的异同
    JSP (一)
    Node.js npm 环境配置
    新老版本vue-cli的安装及创建项目等方式的比较
    npm 代理设置及更换为国内下载源
    for...of的使用
    打印机使用方法
    给OC项目添加icon
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835156.html
Copyright © 2011-2022 走看看