今天开始写数据结构了,感觉不错!!2012-05-19
线性表的链式存储
| Time Limit: 1000MS |
Memory Limit: 65535KB |
| Submissions: 191 |
Accepted: 85 |
Sample Input
5
1 2 3 4 5
6
11 22 33 44 55 66
Sample Output
1 2 3 4 5 11 22 33 44 55 66
# include<stdio.h>
# include<stdlib.h>
# include<malloc.h>
typedef struct node
{
int num;
struct node *next;
}SeqList;
SeqList *Insert(int n)//创建链表
{
int t=0;
SeqList *head,*p1,*p2;
p1=p2=(SeqList *)malloc(sizeof(SeqList));
head=NULL;
if(n==0)return head;//n=0,返回NULL
scanf("%d",&p1->num);
head=p1;//这里很重要,解决n=1的情况
while(t<n-1)
{
if(t!=0) p2->next=p1;
p2=p1;
p1=(SeqList *)malloc(sizeof(SeqList));
scanf("%d",&p1->num);
t++;
}
p2->next=p1;
p2=p1;
p2->next=NULL;
return head;
}
SeqList *Concatenate(SeqList *p1,SeqList *p2)//连接两链表
{
SeqList *head;
if(p1!=NULL)
{
head=p1;
while(p1->next!=NULL)
p1=p1->next;
p1->next=p2;
//free(p2);
}
else head=p2;
return head;
}
int main()
{
int n,m;
SeqList *p,*List1,*List2,*NewHead;
scanf("%d",&n);
List1=Insert(n);
scanf("%d",&m);
List2=Insert(m);
NewHead=Concatenate(List1,List2);
p=NewHead;
while(p->next!=NULL)
{
printf("%d ",p->num);
p=p->next;
}
printf("%d\n",p->num);
return 0;
}