急!!!!!!!数据结构的问题.

来源:百度知道 编辑:UC知道 时间:2024/05/27 07:36:29
随便输入一行英文,计算其中各单词的个数.
并不知道其中有多少单词或具体是哪些单词.
最后输出各单词及其个数.
用数据结构实现.
下面是我写的代码.编译没错,但运行得不到结果.
我找了很久都没找到哪错了.希望各位大虾帮帮忙!
小弟感激不尽!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//typedef char datatype[8];
typedef struct node *pnode;
typedef struct node
{
char info[8];
int num;
pnode next;
};
typedef struct node * linklist;
typedef linklist * plinklist;
linklist createnulllist_link(void) /*创建空链表*/
{
linklist llist=(linklist)malloc(sizeof(struct node));
if (llist!=NULL)
llist->next=NULL;
else printf("Out of space!\n");
return (llist);
}

int insertlink(linklist llist,pnode q)
{
pnode p;
p=llist->next;
while (p!=NULL)
{
if (strcmp(q->info,p->info)==0)
{
q->num++;
return(1);
}
else
p=p->next;

原因就不说了,按如下修改:
1.将函数insertlink()修改为
int insertlink(linklist llist,pnode q)
{
pnode p;
p=llist->next;
while (p!=NULL && p->next != NULL)
{
if (strcmp(q->info,p->info)==0)
{
q->num++;
return(1);
}
else
p=p->next;
}
if (p==NULL)
{
p = llist;
}
p->next=q;
q->next=NULL;
q->num=1;
return(1);
}
2.在函数main()中,将
pnode q=(pnode)malloc(sizeof(struct node));
修改为
q=(pnode)malloc(sizeof(struct node));
3.这个是个警告,不影响运行,将
typedef struct node
{
char info[8];
int num;
pnode next;
};
修改为
node
{
char info[8];
int num;
pnode next;
};

4.还有, 在main()中,
if(q->info=='\0')
continue;
改为
if (q->info[0] == '\0')
{
i++;
continue;
}

最后,祝你成功。