Problem E: 建立链表(线性表)
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 323 Solved: 207
Description
(线性表)设键盘输入n个英语单词,输入格式为n, w1, w2, …,wn,其中n表示随后输入英语单词个数,试编一程序,建立一个单向链表,实现:
如果单词重复出现,则只在链表上保留一个。
Input
4
now come now please
Output
now come please
Sample Input
3
go come keep
Sample Output
go come keep
HINT
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef char ElemType;
typedef struct Node
{
ElemType data[100];
struct Node *next;
}SqList;
void CreateList(SqList *&L)
{
L=(SqList *)malloc(sizeof(SqList));
L->next=NULL;
}
void zhuanhuan(SqList *&L)
{
SqList *p=L->next,*q,*pre,*r;
while(p!=NULL)
{
q=p->next;pre=p;
while(q!=NULL)
{
if(strcmp(p->data,q->data)==0)
{
r=q;
pre->next=q->next;
q=q->next;
free(r);
}
else
{
pre=q;
q=q->next;
}
}
p=p->next;
}
}
void print(SqList *L)
{
SqList *p=L->next;
while(p!=NULL)
{
printf("%s ",p->data);
p=p->next;
}
}
int main()
{
SqList *p;
int n,i;
char a[100][100];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",a[i]);
CreateList(p);
SqList *r=p,*s;
for(i=0;i<n;i++)
{
s=(SqList *)malloc(sizeof(SqList));
strcpy(s->data,a[i]);
r->next=s;
r=s;
}
r->next=NULL;
zhuanhuan(p);
print(p);
}