c++单向链表类的问题,初学者一枚,求大家指教

落日的背后 2019-05-09 12:05:06
用devc++写的一个测试代码,寻找指定结点时,判断最后一个链表一直会出错,求大神指点。
下面是代码:

#include<iostream>
using namespace std;
#include<string.h>

class person{
private:
char name[20];
public:
person(char *str = "zhangsan"){
strcpy(name, str);
}
person(const person& obj)
{
strcpy(name, obj.name);
}
person& operator = (const person& obj)
{
strcpy(name, obj.name);
}
void setname(char *str)
{
strcpy(name, str);
}
char* getname()
{
return name;
}
};

struct Node
{
person member;
Node *next;
};

class LinkI{
private:
Node *p_head; //专用头结点指针
int SizeI;
public:
LinkI();
~LinkI();
void Insert_Node(person& obj); //尾部插入
int FindName_Node(char *targetname);
person GetData_Node(int pos);
int GetSizeI();
};

LinkI::LinkI():p_head(NULL),SizeI(0)
{
p_head = new Node;
p_head->next = NULL;
}

LinkI::~LinkI()
{
Node *temp = NULL;
Node *p = p_head->next;
while (p != NULL)
{
temp = p->next;
delete p;
p = temp;
}
delete temp;
temp = NULL;
delete p_head;
p = NULL;
SizeI = 0;
}

void LinkI::Insert_Node(person& obj) //链表尾部插入
{
Node *temp = new Node;
temp->member = obj;
temp->next = NULL;
Node * p = NULL;
p = p_head;
while (p->next != NULL)
{
p = p->next;
}

p->next = temp;
SizeI++;

temp = NULL;
p = NULL;
}

int LinkI::FindName_Node(char *targetname)
{
int pos = 1;
Node *temp = NULL;
temp = p_head->next;
while(temp != NULL)
{
if(strcmp((temp->member).getname(), targetname) == 0) //调试时这里判断会多一次循环……
{
return pos;
}
else
{
temp =temp->next;
pos += 1;
}
}
temp = NULL;
cout<<"未找到信息,可能姓名输入有误。"<<endl;
return -1;
}

person LinkI::GetData_Node(int pos)
{
int count = 0;
person data;
Node *temp = NULL;
temp = p_head;
while(count < pos)
{
temp = temp->next;
count++;
}
data = temp->member;
delete temp;
return data;
}

int LinkI::GetSizeI()
{
return SizeI;
}

int main()
{
LinkI list;
person p1;
person p2("wangwu");
person p3("zhaoliu");
list.Insert_Node(p1);
list.Insert_Node(p2);
list.Insert_Node(p3);
person p4;
p4 = list.GetData_Node(3);
cout<<list.FindName_Node("zhangsan")<<endl;
cout<<list.FindName_Node("wangwu")<<endl;
cout<<list.FindName_Node("zhaoliu")<<endl;
cout<<list.GetSizeI()<<endl;
cout<<p4.getname()<<endl;
}
...全文
112 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2019-05-09
  • 打赏
  • 举报
回复
数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633
636f6c696e 2019-05-09
  • 打赏
  • 举报
回复
[ 问题出在GetData_Node最后莫名其妙去delete指针 还有你的编码习惯不知道是哪里学的,除非是显式释放对象,其余场景在函数尾部把局部变量置空完全没有意义
person LinkI::GetData_Node(int pos)
{
int count = 0;
person data;
Node *temp = NULL;
temp = p_head;
while(count < pos)
{
temp = temp->next;
count++;
}
data = temp->member;
//delete temp;
return data;
}
落日的背后 2019-05-09
  • 打赏
  • 举报
回复
引用 2 楼 赵4老师的回复:
数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633
谢谢!我会加强学习的!
落日的背后 2019-05-09
  • 打赏
  • 举报
回复
引用 1 楼 636f6c696e的回复:
[ 问题出在GetData_Node最后莫名其妙去delete指针 还有你的编码习惯不知道是哪里学的,除非是显式释放对象,其余场景在函数尾部把局部变量置空完全没有意义
person LinkI::GetData_Node(int pos)
{
int count = 0;
person data;
Node *temp = NULL;
temp = p_head;
while(count < pos)
{
temp = temp->next;
count++;
}
data = temp->member;
//delete temp;
return data;
}
非常感谢!也谢谢你指出了我的不足!每次我都怕指针悬空所以才会在函数末尾都指空,现在弄懂了,再次感谢!

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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