建立长度为n的单链表A和长度为m的单链表B。编程实现将B表链接在A表的尾端,形成一个单链表A。数据类型指定为字符型。
Description
第一行为A表的长度n;
第二行为A表中的数据元素;
第三行为B表的长度m;
第四行为B表中的数据元素。
第二行为A表中的数据元素;
第三行为B表的长度m;
第四行为B表中的数据元素。
Input
输出为链接好后的A表中的所有数据元素。
Output
1
2
3
4
5
|
4
A B C D
6
1 2 3 4 5 6
|
Sample Input
1
|
A B C D 1 2 3 4 5 6
|
#include <iostream>
#include <malloc.h>
using namespace std;
typedef struct node{
char data;
struct node *next;
}LinkList;
void createLinkList(LinkList *&l,int n){
LinkList *end,*body;
l=(LinkList*)malloc(sizeof(LinkList));
end=l;
char ch;
for(int i=0;i<n;i++){
cin>>ch;
body=(LinkList*)malloc(sizeof(LinkList));
body->data=ch;
end->next=body;
end=body;
}
end->next=NULL;
}
int main(){
int n,m;
cin>>n;
LinkList *l1;
createLinkList(l1, n);
cin>>m;
LinkList *l2;
createLinkList(l2, m);
LinkList *head=l1->next;
while(head->next!=NULL){
head=head->next;
}
head->next=l2->next;
LinkList *head1=l1->next;
while(head1->next!=NULL){
cout<<head1->data<<' ';
head1=head1->next;
}
cout<<head1->data<<' ';
return 0;
}