求助:链表的头结点出现-842150451问题

maifuming 2011-04-24 02:11:25
我知这是头结点没处理好的问题,可我刚学链表,还不会处理这个问题,求助下我该如何修改。。。哪个手可以帮下我哈,谢先啦。。。

// list.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;
template <class datatype>
class NODE
{
public:
datatype data;
NODE<datatype> *next;
};
template<class datatype>
class LIST
{
private:
NODE<datatype> *head;
public:
LIST(){
head=new NODE<datatype>;
head->next=NULL;
};
bool get_data(int i,datatype &x);
bool insert_data(int i,datatype data);
bool delete_data(int i);
void print_list();
int get_num(int x);
~LIST()
{
NODE<datatype> *p;
while(head)
{
p=head;
head=head->next;
delete p;
}
}
};
template<class datatype>
int LIST<datatype>::get_num(int x)
{
NODE<datatype> *current;
int j=1;
current=head;
while((current->data!=x) && j<=6)
{
j++;
current=current->next;
}
return j;
}
template<class datatype>
bool LIST<datatype>::delete_data(int i)
{
NODE<datatype> *current,*previous;
int j=1;
if(head==NULL)
{
cout<<"表已空,不能删除。\n";
return false;
};
if((i<1)||(i>6))
{
cout<<"删除位置不正确,不能删除!\n";
return false;
}
current=previous=head;
while(current&&j<i)
{
previous=current;
current=current->next;
j++;
};
if(head==current)
{
head=head->next;
delete current;
}
else
{
previous->next=current->next;
delete current;
}
return true;
}
template<class datatype>
bool LIST<datatype>::get_data(int i,datatype &x)
{
NODE<datatype> *current;
int j=1;
if((i<1)||(i>6))
{
cout<<"非法位置读取元素,不能读取!\n";
return false;
}
current=head;
while(current!=NULL&&j<i)
{
j++;
current=current->next;
}
x=current->data;
return true;
}
template<class datatype>
bool LIST<datatype>::insert_data(int i,datatype data)
{
NODE<datatype> *current,*previous,*newnode;
int j=1;
if((i>7) || (i<0))
{
cout<<"插入位置不正确,不能插入!\n";
return false;
}
newnode=new NODE<datatype>;
if(newnode==NULL)
{
cout<<"内在无空间,表已满,不能插入!\n";
return false;
}
newnode->data=data;
newnode->next=NULL;
previous=head;
current=head->next;
while(current!=NULL&&j<i)
{
previous=current;
current=current->next;
j++;
};
newnode->next=current;
previous->next=newnode;
return true;
}
template<class datatype>
void LIST<datatype>::print_list()
{
NODE<datatype> *current;
current=head;
while(current)
{
cout<<current->data<<" ";
current=current->next;
}
cout<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int i,n,x;
int arry[6]={21,23,25,27,29,31};
LIST<int>list;
for(i=1;i<=6;i++)
list.insert_data(i,arry[i-1]);
cout<<"该链表为:";
list.print_list();
cout<<endl;
cout<<"输入序号:";
cin>>n;
int i_data=100;
list.get_data(n,i_data);
cout<<"输出值为:"<<i_data<<endl;
cout<<"输入值:";
cin>>x;
cout<<"输出序号:"<<list.get_num(x)<<endl;
cout<<"插入结点:";
cin>>n>>x;
list.insert_data(n,x);
cout<<"输出链表:";
list.print_list();
cout<<endl;
cout<<"删除结点:";
cin>>n;
list.delete_data(n);
cout<<"输出链表:";
list.print_list();
cout<<endl;
return 0;
}
...全文
286 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
书虫 2011-04-24
  • 打赏
  • 举报
回复
newfarmerchi 2011-04-24
  • 打赏
  • 举报
回复
LIST(){
head=new NODE<datatype>;
head->next=NULL;
};
构造函数生成一个头结点head.
在insert_data(int i,datatype data)中
previous=head;
previous->next=newnode;
第一个生成的NODE是head->next;
所以在print_list()中
要current=head->next;
maifuming 2011-04-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 happymawolf 的回复:]

按下面的改,应该没问题!
C/C++ code


template <class datatype>
class NODE
{
public:
datatype data;
NODE<datatype> *next;
};
template<class datatype>
class LIST
{
private:
NODE<datatype> *head;
pub……
[/Quote]

呵呵。。。OK啦。。。谢谢了。。。
书虫 2011-04-24
  • 打赏
  • 举报
回复
按下面的改,应该没问题!


template <class datatype>
class NODE
{
public:
datatype data;
NODE<datatype> *next;
};
template<class datatype>
class LIST
{
private:
NODE<datatype> *head;
public:
LIST(){
// head=new NODE<datatype>; // 将head赋空值
// head->next=NULL;
head = NULL;
};
bool get_data(int i,datatype &x);
bool insert_data(int i,datatype data);
bool delete_data(int i);
void print_list();
int get_num(int x);
~LIST()
{
NODE<datatype> *p;
while(head)
{
p=head;
head=head->next;
delete p;
}
}
};
template<class datatype>
int LIST<datatype>::get_num(int x)
{
NODE<datatype> *current;
int j=1;
current=head;
while((current->data!=x) && j<=6)
{
j++;
current=current->next;
}
return j;
}
template<class datatype>
bool LIST<datatype>::delete_data(int i)
{
NODE<datatype> *current,*previous;
int j=1;
if(head==NULL)
{
cout<<"表已空,不能删除。\n";
return false;
};
if((i<1)||(i>6))
{
cout<<"删除位置不正确,不能删除!\n";
return false;
}
current=previous=head;
while(current&&j<i)
{
previous=current;
current=current->next;
j++;
};
if(head==current)
{
head=head->next;
delete current;
}
else
{
previous->next=current->next;
delete current;
}
return true;
}
template<class datatype>
bool LIST<datatype>::get_data(int i,datatype &x)
{
NODE<datatype> *current;
int j=1;
if((i<1)||(i>6))
{
cout<<"非法位置读取元素,不能读取!\n";
return false;
}
current=head;
while(current!=NULL&&j<i)
{
j++;
current=current->next;
}
x=current->data;
return true;
}
template<class datatype>
bool LIST<datatype>::insert_data(int i,datatype data)
{
NODE<datatype> *current,*previous,*newnode;
int j=1;
if((i>7) || (i<0))
{
cout<<"插入位置不正确,不能插入!\n";
return false;
}
newnode=new NODE<datatype>;
if(newnode==NULL)
{
cout<<"内在无空间,表已满,不能插入!\n";
return false;
}
newnode->data=data;
newnode->next=NULL;
// previous=head; // 下面的逻辑有点问题,按照这样改
if (head == NULL)
{
head=newnode;
return true;
}
current=head/*->next*/;
while(current!=NULL&&j<i)
{
previous=current;
current=current->next;
j++;
}/*;*/
newnode->next=current;
previous->next=newnode;
return true;
}
template<class datatype>
void LIST<datatype>::print_list()
{
NODE<datatype> *current;
current=head;
while(current)
{
cout<<current->data<<" ";
current=current->next;
}
cout<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int i,n,x;
int arry[6]={21,23,25,27,29,31};
LIST<int>list;
for(i=1;i<=6;i++)
list.insert_data(i,arry[i-1]);
cout<<"该链表为:";
list.print_list();
cout<<endl;
cout<<"输入序号:";
cin>>n;
int i_data=100;
list.get_data(n,i_data);
cout<<"输出值为:"<<i_data<<endl;
cout<<"输入值:";
cin>>x;
cout<<"输出序号:"<<list.get_num(x)<<endl;
cout<<"插入结点:";
cin>>n>>x;
list.insert_data(n,x);
cout<<"输出链表:";
list.print_list();
cout<<endl;
cout<<"删除结点:";
cin>>n;
list.delete_data(n);
cout<<"输出链表:";
list.print_list();
cout<<endl;
return 0;
}
maifuming 2011-04-24
  • 打赏
  • 举报
回复
#2楼:我知是头结点没初始化,可在哪修改好呢?。。。我还是不会。。。弄这个问题我弄了两天啦。好气人呀。。。

...........
class LIST
{
private:
NODE<datatype> *head;
public:
LIST(){
head=new NODE<datatype>;
head->data=NULL;
head->next=NULL;
};
bool get_data(int i,datatype &x);
bool insert_data(int i,datatype data);
bool delete_data(int i);
void print_list();
int get_num(int x);
~LIST()
{
NODE<datatype> *p;
while(head)
{
p=head;
head=head->next;
delete p;
}
}
};
.............
这样初始化的话那第一个结点的元素就是0了。。。可怎消去这个头结点呢?。。。
KID_coder 2011-04-24
  • 打赏
  • 举报
回复
出现负数大概是头结点没初始化或初始化不成功~
maifuming 2011-04-24
  • 打赏
  • 举报
回复
注:我那主函数的输入输出格式是应我所做的题目要求的,所以输入输出格式有点那个了呵呵。。。

64,654

社区成员

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

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