单链表的链接(0954)swust-oj

Description

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

Input

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

Output

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

Sample Input
4
A B C D
6
1 2 3 4 5 6
Sample Output

 A B C D 1 2 3 4 5 6


代码:

#include<stdio.h>

#include<malloc.h>
typedef struct node
{
char data[50];//定义字符数组
node * next;
}Node;
void Create(Node*&L,int n)//不一定需要有a[],,,,尾插法建立链表
{
Node* s,*r;
int i;
L=(Node*)malloc(sizeof(node));
r=L;
for(i=0;i<n;i++)
{
s=(Node*)malloc(sizeof(node));
scanf("%s",s->data);//读入数据
r->next=s;
r=s;
}
r->next=NULL;
}
void output(Node*L)//输出函数
{
Node*read;
read=L->next;
while(read->next)
{
printf("%s ",read->data);
read=read->next;
}
printf("%s",read->data);
}
void combine(Node*a,Node*b)//连接函数*a和*b是前后两次输入的
{
Node *q;
q=a->next;//q先指向第一次输入的链表的头结点
while(q->next)
{
q=q->next;     //先输入的
}
q->next=b->next;   //当第一次输入的都遍历完之后,q指针就指向第二次输入的链表
}

int main()
{
int i;
int n;
Node*a,*b;
scanf("%d",&n);
Create(a,n);
int m;
scanf("%d",&m);
Create(b,m);
combine(a,b);
output(a);
free(b);
return 0;
}

 

posted @ 2015-04-06 19:14  殇林  阅读(1222)  评论(0编辑  收藏  举报