【数据结构_链表_List_0954】单链表的链接

建立长度为n的单链表A和长度为m的单链表B。编程实现将B表链接在A表的尾端,形成一个单链表A。数据类型指定为字符型。

第一行为A表的长度n; 
第二行为A表中的数据元素; 
第三行为B表的长度m; 
第四行为B表中的数据元素。

输出为链接好后的A表中的所有数据元素。

------------------------------------------------------------

4
A B C D
6
1 2 3 4 5 6
------------------------------------------------------------

A B C D 1 2 3 4 5 6



#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node
{
	char str;//我不管,我就要这样写,OJ你咬我啊!
	struct node *next;
};
int main()
{
	int length1,length2;
	struct node *head1,*head2,*p,*q,*r,*s,*temp,*t;
	head1=(struct node *)malloc(sizeof(struct node));
	head2=(struct node *)malloc(sizeof(struct node));
	while(cin>>length1)
	{
		p=head1;
		int i,j;
		for(i=0;i<length1;i++)
		{
			q=(struct node *)malloc(sizeof(struct node));
			cin>>q->str;
			p->next=q;
			p=q;
		}
		p->next=NULL;
		s=head2;
		for(i=0;i<length2;i++)
		{
			r=(struct node *)malloc(sizeof(struct node));
			cin>>r->str;
			s->next=r;
			s=r;
		}
		s->next=NULL;
		temp=head1;
		temp=temp->next;
		while(temp!=NULL)
		{
			cout<<temp->str<<" ";
			temp=temp->next;
		}
		temp=head2;
		temp=temp->next;
		while(temp!=NULL)
		{
			cout<<temp->str<<" ";
			temp=temp->next;
		}
	}
	return 0;
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值