线性链表的储存

建立长度为n的线性表的链表类LinList,长度n通过和n个数据元素通过cin输入。 要求: 1)调用Insert函数,建立长度为n的链表A, 2)建立长度为m的链表B,并编写函数实现单链表类LinList的对象B连接到单链表类LinList的对象A的尾部:Void Concatenate(LinList& A, LinList& B)。并输出链接后的链表的所有数据元素。

Description

输入共有四行,第一行是一个整数n,表示线性表A的长度; 第二行输入n个数据元素; 第三行是一个整数m,表示线性表B的长度; 第四行输入m个数据元素。

Input

输出A和B链接后的所有数据

Output
1
2
3
4
5
5
1 2 3 4 5
6
11 22 33 44 55 66
Sample Input
1
2
1 2 3 4 5 11 22 33 44 55 66

只要把两个链表的头尾连接起来就可以。输出格式控制。。。多想一下!!

#include<stdio.h>
#include<stdlib.h>
struct Date
{
int date;
struct Date *next;
};
#define len sizeof(struct Date)
int main()
{
struct Date *head1,*head2,*p,*p1;
head1=NULL;
head2=NULL;
int n1,n2,i;
scanf("%d",&n1);
for(i=0;i<n1;i++)
{
p=(struct Date *)malloc(len);
scanf("%d",&p->date);
if(head1==NULL)
head1=p;
else
p1->next=p;
p1=p;
}
p->next=NULL;
scanf("%d",&n2);
for(i=0;i<n2;i++)
{
p=(struct Date *)malloc(len);
scanf("%d",&p->date);
if(head2==NULL)
head2=p;
else
p1->next=p;
p1=p;
}
p->next=NULL;
p=head1;
int flag=0;
while(p!=NULL)
{
if(p==head1)
printf("%d",p->date);
else
printf(" %d",p->date);
p=p->next;
if(p==NULL && flag==0)
{
p=head2;
flag=1;
}
}
printf("\n");
free(p);
return 0;
}
Sample Output
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值