单链表的链接(0954)swust-oj
Description
建立長度為n的單鏈表A和長度為m的單鏈表B,n>0,m>0。編程實現(xiàn)將B表鏈接在A表的尾端,形成一個單鏈表A。數(shù)據(jù)類型指定為字符型。
Input
第一行為A表的長度n; 第二行為A表中的數(shù)據(jù)元素 第三行為B表的長度m; 第四行為B表中的數(shù)據(jù)元素。
Output
輸出為鏈接好后的A表中的所有數(shù)據(jù)元素。
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];//定義字符數(shù)組
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);//讀入數(shù)據(jù)
r->next=s;
r=s;
}
r->next=NULL;
}
void output(Node*L)//輸出函數(shù)
{
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)//連接函數(shù)*a和*b是前后兩次輸入的
{
Node *q;
q=a->next;//q先指向第一次輸入的鏈表的頭結(jié)點
while(q->next)
{
q=q->next; //先輸入的
}
q->next=b->next; //當?shù)谝淮屋斎氲亩急闅v完之后,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;
}
總結(jié)
以上是生活随笔為你收集整理的单链表的链接(0954)swust-oj的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: i春秋DMZ大型靶场实验(四)Hash基
- 下一篇: 模板—tarjan求割边