实验数据结构(保存了两日内书面实验报告) 列表中的整合

本文涉及的产品
传统型负载均衡 CLB,每月750个小时 15LCU
网络型负载均衡 NLB,每月750个小时 15LCU
应用型负载均衡 ALB,每月750个小时 15LCU
简介:

huangjing

列表中的整合,要求O(la*lb)复杂性。实际插入是什么。需要注意的是,在特殊情况下的列表的开始和结束的假设

代码

#include<cstdio>
#include<cstring>
#include<cstdlib>

typedef struct node
{
	int data;
	struct node *next;
}Node,*listnode;

int lena,lenb;

void creatlist(listnode &head,int flag)
{
	int x=1;
	listnode p,xx;
    head->next=NULL;
	xx=head;
	while(x!=0)
	{
		p=(listnode)malloc(sizeof(struct node));
        scanf("%d",&x);
		if(x==0)  break;
		if(flag)
			lena++;
		else
			lenb++;
		p->data=x;
		p->next=NULL;
		xx->next=p;
		xx=p;
	}
}//创建链表

void forlist(listnode &head)
{
    listnode p;
	p=head->next;
	while(p!=NULL)
	{
        printf("%d ",p->data);
		p=p->next;
	}
}//遍历链表

void Insert(listnode &la,int val)
{
	listnode p,last,cur;
	p=la->next;
	last=p;
	cur=(listnode)malloc(sizeof(node));
    cur->data=val;
    if(p->data>val)
    {
       cur->next=p;
       la->next=cur;
       return;
    }
	while(p->data<val)
	{
		last=p;
		p=p->next;
		if(p==NULL)  break;
	}
    cur->next=p;
    last->next=cur;
}

void unionlist(listnode &la,listnode &lb)
{
	int flag;
	listnode xx,yy;
	yy=lb->next;
    for(int i=1;i<=lenb;i++)
	{
		flag=0;
        int key=yy->data;
		yy=yy->next;
		xx=la->next;
		for(int j=1;j<=lena;j++)
		{
			if(xx->data==key)
			 {
				 flag=1;
				 break;
			 }
			 else
				 xx=xx->next;
		}
		if(!flag)
		{
			Insert(la,key);
			lena++;
		}
	}
}//合并链表

int main()
{
	lena=lenb=0;
    listnode heada,headb;
	heada=(listnode)malloc(sizeof(struct node));
    headb=(listnode)malloc(sizeof(struct node));
	creatlist(heada,1);
	creatlist(headb,0);
	printf("链表la长度 lb长度:%d %d\n",lena,lenb);
	printf("链表ha为");
	forlist(heada);
	printf("\n");
	printf("链表hb为");
	forlist(headb);
	printf("\n");
    unionlist(heada,headb);
	printf("合并后的链表:\n");
    forlist(heada);
    printf("\n");
	forlist(headb);
	return 0;
}


/*
3 4 6 18  0
2 3 5 6 7 19 20 0
*/


版权声明:本文博客原创文章,博客,未经同意,不得转载。





本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4709251.html,如需转载请自行联系原作者


相关实践学习
SLB负载均衡实践
本场景通过使用阿里云负载均衡 SLB 以及对负载均衡 SLB 后端服务器 ECS 的权重进行修改,快速解决服务器响应速度慢的问题
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
相关文章
|
4月前
|
存储 算法 数据安全/隐私保护
【Python学习篇】Python实验小练习——高级数据结构(五)
【Python学习篇】Python实验小练习——高级数据结构(五)
57 1
|
4月前
|
存储 算法 数据挖掘
数据结构实验||约瑟夫环
数据结构实验||约瑟夫环
|
4天前
|
存储 索引 Python
Python编程的常用数据结构—列表
Python编程的常用数据结构—列表
|
4天前
|
存储 索引 Python
Python编程的常用数据结构—列表 原创
Python编程的常用数据结构—列表 原创
|
3月前
|
存储 缓存 Python
Python中的列表(List)和元组(Tuple)是两种重要的数据结构
【7月更文挑战第12天】Python中的列表(List)和元组(Tuple)是两种重要的数据结构
39 1
|
4月前
|
存储 Python
Python中使用列表和字典来存储和处理复杂的数据结构
Python中使用列表和字典来存储和处理复杂的数据结构
|
4月前
|
存储 索引 Python
Python零基础入门-5 数据结构(列表和元组
Python零基础入门-5 数据结构(列表和元组
|
5月前
|
数据处理 Python
深入理解Python的数据结构:列表与元组
深入理解Python的数据结构:列表与元组
42 1
|
5月前
|
存储 算法
数据结构实验(四)二叉树的基本操作
数据结构实验(四)二叉树的基本操作
|
5月前
|
索引
R语言数据结构-----列表
R语言数据结构-----列表
27 3