请大家帮忙找出错误所在.

bensu 2003-08-23 12:57:05
#include<string.h>
#include<iostream.h>
struct name
{
char student_name[20];
name *link;
};

class student
{
public:
student();
~student();
int check();
int insert();
void show();
private:
name *head,*tail;
};
student::student()
{
head=tail=NULL;
}

student::~student()
{
name *temp=head;
while(temp->link!=NULL)
{
delete temp;
temp=temp->link;
}
}

int student::check()
{
char *a;
cout<<"please input the name that you want tocheck"<<endl;
cin>>a;
name *temp=head;
while(temp->link!=NULL)
{
if(!strcmp(temp->student_name,a))
{
cout<<"the name is found!";
return 1;
}
else
temp=temp->link;
}
if(temp->link==NULL)
{
cout<<"the name is not in the link";
return 0;
}
}

int student::insert()
{
name *temp=new name;
if(temp==NULL)
return 0;
cout<<"input the name you want to insert"<<endl;
cin>>temp->student_name;
if(head==NULL)
{
head=tail=temp;
delete temp;
}
else
{
tail->link=temp;
tail=temp;
delete temp;
}
return 1;
}

void student::show()
{
name *temp=head;
while(temp->link!=NULL)
{
cout<<temp->student_name<<endl;
temp=temp->link;
}

}

void main()
{
student s;
char *a;
int flag=0,order;
while(flag)
{
cout<<"1.insert"<<endl<<"2.check"<<endl<<"3.show all"<<endl<<"4.exit"<<endl;
cin>>order;
switch(order)
{
case'1':
s.insert();
break;
case'2':
s.check()
break;
case'3':
s.show();
break;
case'4':
flag=0;
}
}

}
//代码的作用是用链表储存名字,并可以查找出某个名字是否在表里也可以显示出所有的名字。
//有菜单选择。
可以通过编译,但是运行不了.
...全文
41 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
panther8888 2003-08-24
  • 打赏
  • 举报
回复
你的析构函数似乎有点问题,看看你的代码:
student::~student()
{
name *temp=head;
while(temp->link!=NULL)
{
delete temp;
temp=temp->link;
}
}
其中: delete temp;执行后temp==NULL,而你接着又temp=temp->link:这里肯定错了;
在int student::check()中你的指针a在分配内存前被使用,似乎也有问题。
这一行好像多余:if(temp->link==NULL),因为程序能执行到这里就说明没有找到,如果找到了就已经返回了,因为只有找到和没找到两种可能不是吗。
还有在int student::insert()中的两句delete temp;应该去掉,不然你调用这个函数之后,你的链子表就成了一个空表,即head==NULL或head->link==NULL.
粗略看了一下就只发现了这些,你自已改一下试试看。
bensu 2003-08-24
  • 打赏
  • 举报
回复
谢谢各位了
CslQy 2003-08-24
  • 打赏
  • 举报
回复
先说一下,搂主的编程风格很不好,有待提高。下面是修改过的代码,应该没有什么严重的错误了。
#include<string.h>
#include<iostream.h>
#define MAX_NAME_LENGTH 30
struct name
{
char student_name[20];
name *link;
};

class student
{
public:
student();
~student();
int check();
int insert();
void show();
private:
//name *head,*tail;
name *m_pHead;
name *m_pTail;
};
student::student()
{
//head=tail=NULL;
m_pHead = NULL;
m_pTail = NULL;
}

student::~student()
{
//name *temp=m_pHead; //问题出在这里,附值之前没有判断m_pHead是否为空
name *temp;
if( !m_pHead )
{
return;
}
//temp = m_pHead;
while(m_pHead != NULL)
{
//delete temp;
//temp=temp->link;
//这两句话也不对,temp已经被delete掉了,还怎么进行temp = temp->link呢?
temp = m_pHead->link;
delete m_pHead;
m_pHead = temp;
}
}

int student::check()
{
//char *a;
//cout<<"please input the name that you want tocheck"<<endl;
//cin>>a;
//以上三句话有问题,你定义了一个char*的变量,没有给他分配空间就赋值,肯定会出错的
char *a = new char[ MAX_NAME_LENGTH ];
cout<<"please input the name that you want tocheck"<<endl;
cin>>a;

name *temp=m_pHead;

while(temp!=NULL)
{
//if(!strcmp(temp->student_name,a))
if( !strcmp(temp->student_name,a) )
{
cout<<"the name is found!\n";
return 1;
}
//else //这个else没有必要
temp=temp->link;
}
cout<<"the name is not in the link\n";
return 0;

//上面的if判断也没有必要
return 0;
}

int student::insert()
{
name *temp=new name;
if(temp==NULL)
return 0;
cout<<"input the name you want to insert"<<endl;
cin>>temp->student_name;
//如果你想根据temp->Link是否为空判断是否到了链表末尾的话,记得要给Link赋值
temp->link = NULL;
if(m_pHead==NULL)
{
m_pHead=m_pTail=temp;
//delete temp;
}
else
{
//m_pTail->link=temp;
//m_pTail=temp;
m_pTail->link = temp;
m_pTail = temp;
//delete temp; 这两个delete不能有,因为是指针复制。
}
return 1;
}

void student::show()
{
name *temp=m_pHead;
while(temp != NULL)
{
cout<<temp->student_name<<endl;
temp=temp->link;
}

}

void main()
{
student s;
int flag=1,order;
while(flag)
{
cout<<"1.insert"<<endl<<"2.check"<<endl<<"3.show all"<<endl<<"4.exit"<<endl;
cin>>order;
switch(order)
{
// case'1':
// s.insert();
// break;
// case'2':
// s.check()
// break;
// case'3':
// s.show();
// break;
// case'4':
// flag=0;
//如果你Switch的是int的"order"的话,应该在case中用int的类型判断
case 1:
{
s.insert();
break;
}
case 2:
{
s.check();
break;
}
case 3:
{
s.show();
break;
}
case 4:
{
flag = 0;
break;
}

}
}

}
Healer 2003-08-23
  • 打赏
  • 举报
回复
[可以通过编译,但是运行不了.]就是内存问题了:)
是哪的指针错了. 制空了.

Beover1984 2003-08-23
  • 打赏
  • 举报
回复
为什么不用类呢?
紫郢剑侠 2003-08-23
  • 打赏
  • 举报
回复
研究中。。。
zzyyxx 2003-08-23
  • 打赏
  • 举报
回复
楼上的楼上那位 好像你写的也不行吧
zzyyxx 2003-08-23
  • 打赏
  • 举报
回复
你最后的函数里都少了个语句结束符“;”,我咋一眼暂时只找出了这个 不好意思哈哈。
正在找。。。。。。
fancy123 2003-08-23
  • 打赏
  • 举报
回复
改好了。

#include "iostream.h"
#include "string.h"

struct name
{
char student_name[20];
name *link;
};

class student
{
public:
student();
~student();
int check();
int insert();
void show();
private:
name *head,*tail;
};
student::student()
{
head=tail=NULL;
};

student::~student()
{
name *temp=head;
while(temp->link!=NULL)
{
name *tt=temp;
temp=temp->link;
delete tt;
}
};

int student::check()
{
char a[20];
cout<<"please input the name that you want tocheck"<<endl;
cin>>a;
name *temp=head;


while(temp!=tail)
{
if(!strcmp(temp->student_name,a))
{
cout<<"the name is found!"<<endl;
return 1;
}
else
temp=temp->link;
}

if (temp!=NULL)
if(!strcmp(temp->student_name,a))
{
cout<<"the name is found!"<<endl;
return 1;
};

cout<<"the name no found!";
return 1;

};

int student::insert()
{
name *temp=new name;

if(temp==NULL)
{
return 0;
cout<<"Temp is null!";
}
cout<<"input the name you want to insert"<<endl;
cin>>temp->student_name;
temp->link = NULL;
if(head==NULL)
{
head=tail=temp;
}
else
{
tail->link=temp;
tail=temp;

}
return 1;
};

void student::show()
{
name *temp=head;

while(temp!=tail)
{
cout<<temp->student_name<<endl;
temp=temp->link;
}

if (temp!=NULL)
cout<<temp->student_name<<endl;
};


void main()
{
student s;

int flag=1,order;
while(flag)
{
cout<<"1.insert"<<endl<<"2.check"<<endl<<"3.show all"<<endl<<"4.exit"<<endl;
cin>>order;
switch(order)
{
case 1:
s.insert();
break;
case 2 :
s.check();
break;
case 3:
s.show();
break;
case 4:
flag=0;
break;
}
}

}

问题所在:1.仔细看看指针。明白new and delete.
2.明白char 与 int 的不同:char '1' 与 int 1 不相等。(in switch)
3.如果用结构,而不是用类。必须注意自己初始化完全,因为没有构造函数可自动调用。


bensu 2003-08-23
  • 打赏
  • 举报
回复
GhostAdz(鬼斧) 不好意思,那个char *a是多余的

sevecol(看什么看...) 我照你说的改了一下还是不行,编译器提示“access violation”
sevecol 2003-08-23
  • 打赏
  • 举报
回复
大概的改了一下

struct name
{
char student_name[20];
name *link;
};

class student
{
public:
student();
~student();
int check();
int insert();
void show();
private:
name *head,*tail;
};
student::student()
{
head=tail=NULL;
};

student::~student()
{
name *temp=head;
while(temp->link!=NULL)
{
name *tt=temp;
temp=temp->link;
delete tt;
}
};

int student::check()
{
char a[20];
cout<<"please input the name that you want tocheck"<<endl;
cin>>a;
name *temp=head;


while(temp!=tail)
{
if(!strcmp(temp->student_name,a))
{
cout<<"the name is found!"<<endl;
return 1;
}
else
temp=temp->link;
}

if (temp!=NULL)
if(!strcmp(temp->student_name,a))
{
cout<<"the name is found!"<<endl;
return 1;
};

cout<<"the name no found!";

};

int student::insert()
{
name *temp=new name;
if(temp==NULL)
return 0;
cout<<"input the name you want to insert"<<endl;
cin>>temp->student_name;
if(head==NULL)
{
head=tail=temp;
}
else
{
tail->link=temp;
tail=temp;
}
return 1;
};

void student::show()
{
name *temp=head;

while(temp!=tail)
{
cout<<temp->student_name<<endl;
temp=temp->link;
}

if (temp!=NULL)
cout<<temp->student_name<<endl;
};


////
student s;
char *a;
int flag=1,order;
while(flag)
{
cout<<"1.insert"<<endl<<"2.check"<<endl<<"3.show all"<<endl<<"4.exit"<<endl;
cin>>order;
switch(order)
{
case 1:
s.insert();
break;
case 2:
s.check();
break;
case 3:
s.show();
break;
case 4:
flag=0;
}
}
GhostAdz 2003-08-23
  • 打赏
  • 举报
回复
main里的char *a; 要来干嘛

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧