您的位置:首页 > 其它

单链表的链接

2018-01-02 15:45 162 查看
建立长度为n的单链表A和长度为m的单链表B。编程实现将B表链接在A表的尾端,形成一个单链表A。数据类型指定为字符型。Description第一行为A表的长度n; 
第二行为A表中的数据元素; 
第三行为B表的长度m; 
第四行为B表中的数据元素。Input输出为链接好后的A表中的所有数据元素。Output
123454A B C D 61 2 3 4 5 6
Sample Input
1A 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: