首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

大侠帮忙见见,链表输出函数咋不能调用

2012-09-23 
大侠帮忙看看,链表输出函数咋不能调用?#include stdafx.h#include iostream.h#include stdlib.hstru

大侠帮忙看看,链表输出函数咋不能调用?
#include "stdafx.h"
#include "iostream.h"
#include "stdlib.h"

struct Node
{
int m_data;
Node* m_pnext; //C中唯一一处先使用,后定义的应用
};

class List
{
public:
Node* head;
public:
List()
{
head=new Node;
if(head==NULL)
{
  exit(1);
}
head->m_pnext=NULL;
}
~List(){delete head; head=NULL;}
public:
Node* Insert(int data);
bool Print();
Node* Combine(Node* FirstHead, Node* SecondHead);
Node* Delete();
};

Node* List::Insert(int data)
{
Node* pmove=head;
Node* pthis=new Node;
pthis->m_data=data;
pthis->m_pnext=NULL;
if(head==NULL)
{
return head;
}
while(pmove->m_pnext!=NULL)
{
pmove=pmove->m_pnext;
}
pmove->m_pnext=pthis;
return head;
}

bool List::Print()
{
Node* pmove=head->m_pnext;
cout << "head->";
while(pmove) 
{
cout << pmove->m_data << "->";
pmove=pmove->m_pnext;
}
cout << "over" << endl << endl;
return true;
}

Node* List::Delete()
{
Node* pmove=head->m_pnext;
Node* pdelete=NULL;
if(head==NULL || head->m_pnext==NULL)
{
return head;
}
while(pmove)
{
pdelete=pmove;
pmove=pmove->m_pnext;
head->m_pnext=pmove;
delete pdelete;
pdelete=NULL;
}
   
return head;
}

bool Print_Global(Node* newhead) //全局输出函数
{
Node* head=newhead;
Node* pmove=head->m_pnext;
cout << "head->";
while(pmove) 
{
cout << pmove->m_data << "->";
pmove=pmove->m_pnext;
}
cout << "over" << endl << endl;
return true;
}  

int main(int argc, char* argv[])
{
List Onelist;
cout << "The First list is: " << endl;
for(int i=0; i<10; i++)
{
Onelist.Insert(i);
}
  Onelist.Print();
Node* First=new Node;
First->m_pnext=Onelist.head->m_pnext;
Onelist.Delete();
Onelist.Print();
Print_Global(First);//????????

   
cout << "The Second list is: " << endl;
for(int j=0; j<10; j++)
{
Onelist.Insert(j);
}
  Onelist.Print();
Node* Second=new Node;
Second->m_pnext=Onelist.head->m_pnext;
Onelist.Delete();
Onelist.Print();
  Print_Global(First);//????????
return 0;
}

[解决办法]
看了一下,你的头指针保存有错误,代码就不贴出来啦,自习检查一下。顺便提示一下析构函数删除链表的方法有错误
[解决办法]
main函数中,
Onelist.Delete();
Onelist.Print();
First还在指向Onelist,并要遍历它,Onelist却被delete,肯定有问题。

热点排行