我这个链表建立哪里有问题?
这是题目:
设键盘输入n个英语单词,输入格式为n, w1, w2, …
我这个链表建立哪里有问题? 这是题目: 设键盘输入n个英语单词,输入格式为n, w1, w2, …,wn,其中n表示随后输入英语单词个数,试编一程序,建立一个单向链表,实现: 如果单词重复出现,则只在链表上保留一个。 只要一输入不同单词程序就会停止,全相同的不会 #includeusing namespace std; typedef struct LNode{ char word[20]; struct LNode *next; }LNode,*LinkList; bool InitList(LinkList &L) { L=(LNode*)malloc(sizeof(LNode)); if(L==NULL) return false; L->next=NULL; return true; } int Cheak(LinkList &L,char ch[20]){ LNode *q=L->next; while(q!=NULL&&strcmp(q->word,ch)) { q=q->next; } if(q!=NULL) return 0; else return 1; } ListInsert(LinkList &L,int n,int &length) { char ch[20]; LNode *s,*r=L; while(n){ scanf("%s",ch); s=(LNode*)malloc(sizeof(LNode)); if(Cheak(L,ch)) { strcpy(s->word,ch); r->next=s; r=s; length++; } n--; } r->next=NULL; } int main() { int length=0; LinkList L; InitList(L); int n,i; cin>>n; ListInsert(L,n,length); LNode *p; p=L->next; for(i=1;i<=length;i++) { printf("%s ",p->word); p=p->next; } }