博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
循环链表
阅读量:4701 次
发布时间:2019-06-09

本文共 1400 字,大约阅读时间需要 4 分钟。

#include<iostream>

#include<malloc.h>
#include<string.h>
#include<stdio.h>
using namespace std;
#define maxsize 100
typedef struct node
{
int data;
struct node *next;
}lnode,*linklist;

linklist link(linklist head1,linklist head2)

{
lnode *p,*q;
p=head1->next; q=head2->next;
while(p->next!=head1)
{
p=p->next;
}
while(q->next!=head2)
{
q=q->next;
}
p->next=head2->next;
q->next=head1;
return head1;
}
linklist creat(int n)
{
linklist head=(linklist)malloc(sizeof(lnode));

lnode *p,*q;

int e;
q=head;
for(int i=0;i<n;i++)
{
p=(linklist)malloc(sizeof(lnode));
cin>>e;
p->data=e;
q->next=p;//注意 别写成head 那样的话就变成了一对多关系
p->next=NULL;
q=p;
}
if(q!=NULL)
{
q->next=head;
}
return head;
// while(i<n)
// {
// cout<<"请输入第%d个元素。"<<i<<endl;
// cin>>e;
// if(i==1)
// {
// head=(linklist)malloc(sizeof(lnode));
// head->data=e;
// head->next=NULL;
// q=head;
// }
// }
}

void dis(lnode * head)

{
lnode *p,*q;
p=head;
if(p==NULL)
{
cout<<"循环链表为空!";
}
else
{
while(p->next!=head)
{ p=p->next;
cout<<p->data<<" ";
}
}
}
int main()
{
linklist h1,h2;
int n1,n2;
cout<<" 创建h1循环单链表:"<<endl;
cout<<" 请输入元素个数:"<<endl;
cin>>n1;
h1=creat(n1);
cout<<" 创建h2循环单链表:"<<endl;
cout<<" 请输入元素个数:"<<endl;
cin>>n2;
h2=creat(n2);
cout<<"输出循环单链表h1!"<<endl;
dis(h1); cout<<endl;
cout<<"输出循环单链表h2!"<<endl;
dis(h2); cout<<endl;
h1=link(h1,h2) ;
cout<<"输出合并后的循环单链表h1+h2!"<<endl;
dis(h1);
}

 

转载于:https://www.cnblogs.com/mykonons/p/6591902.html

你可能感兴趣的文章
关于PCB开窗
查看>>
【蓝桥杯单片机07】彻底理解51单片机的中断系统
查看>>
款待奶牛
查看>>
APIO 2018选圆圈
查看>>
RabbitMQ,Redis
查看>>
C语言 可变参数的用法
查看>>
数据结构(树套树):ZJOI 2013 K大数查询
查看>>
ruby on rails安装(win7x64)
查看>>
kotlin成长之路
查看>>
建造者模式
查看>>
hdu1114-Piggy-Bank
查看>>
Ionic开发-如何在ion-content形成上下结构 上面固定下层可滚动
查看>>
从头创建一个Git项目
查看>>
cocos2d-X 学习笔记-CCAction(动作)示例
查看>>
spring-boot 速成(12) - 如何注入多个redis StringRedisTemplate
查看>>
pygame 笔记-4 代码封装&发射子弹
查看>>
[sql]mysql指引(整理中...)-对db的分类
查看>>
JS实例之图片滑动效果,实例图片滑动进场
查看>>
Ldap介绍
查看>>
shiro退出登陆清空缓存实现
查看>>