急求高手,解决数据结构的作业!!先谢过了
程序比较短,就没写注释了,学过的应该都能看得懂吧 第一个函数是初始化 第二个函数是用单链表存放一个句子中每个字符出现的次数和位置 第三个是输出
#include <iostream>
using namespace std;
#define MAXSIZE 32767
struct InfoList
{char data;
int weight;
int location[80];
struct InfoList *next;
}IL;
char sen[MAXSIZE];
void InitInfoList(InfoList *&IL)
{
IL=(InfoList *)malloc(sizeof(InfoList));
IL-> next=NULL;
}
void CreateInfoList(char sen[],InfoList *&IL)
{int i,j;
InfoList *p=IL-> next,*s;
i=0;
while(sen[i]!= '\n ')
{
if(sen[i]==p-> data)
{
j++;
p-> weight++;
p-> location[j]=i;
}
else if(p==NULL)
{
j=0;
s=(InfoList*)malloc(sizeof(InfoList));
s-> data=sen[i];
s-> weight=1;
s-> location[j]=i;
p-> next=s;
}
else p=p-> next;
i++;
}
void DispList(InfoList *IL)
{
InfoList *p=IL-> next;
while(p!=NULL)
{cout < <p-> data < < " " < <p-> weight < < " " < <p-> location[] < <endl;
p=p-> next;
}
cout < <endl;
}
int main()
{
InitInfoList(IL);
CreateInfoList(sen[],IL);
DispList(IL);
return 0;
}
运行结果是--------------------Configuration: info - Win32 Debug--------------------
Compiling...
info.cpp
E:\sourcefile\sourcefile\info.cpp(47) : error C2601: 'DispList ' : local function definitions are illegal
E:\sourcefile\sourcefile\info.cpp(57) : error C2601: 'main ' : local function definitions are illegal
E:\sourcefile\sourcefile\info.cpp(63) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
info.obj - 3 error(s), 0 warning(s)
[解决办法]
链表指针头没有定义
void CreateInfoList(char sen[],InfoList *&IL)少了一个}
cout < <p-> data < < " " < <p-> weight < < " " < <p-> location[] < <endl;
p-> location[]
没有看懂这个是要干什么,有这么写的吗?
[解决办法]
好似c++和c语言都用到了。既然c++是面向对象的,那为什么还要用结构体呢?