【数据结构_链表_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;
}



版权声明:本文为IronCarrot原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。