麻烦帮找一下出错原因

来源:百度知道 编辑:UC知道 时间:2024/05/28 10:14:08
//面向对象思路写链表.程序在头插里有问题,不报错,运行的时候有问题,麻烦帮指点一下.谢谢.
#include <iostream>
using namespace std;
/*结点类*/
class Nope
{
public:
Nope(int n,Nope *nxt):num(n),next(nxt){};
void putnum(int n){num=n;}
void putnext(Nope *nxt){next=nxt;}
int getnum() const{return num;}
Nope *getnext() const{return next;}
private:
int num;
Nope *next;
};

/*链表类*/
class Link
{
public:
void hinsert(Nope *note);
void show();
private:
static Nope *head;
};
Nope *Link::head=NULL;
void Link::hinsert(Nope *note)
{
note->putnext(head);
head=note;
}
void Link::show()
{
Nope *p;
if(head==NULL)
cout<<"空链表!"<<endl;
else
{
while(1)
{
cout<<p->getnum();
cout.width(5);
if(p->getnext==NULL)
break;
else
p=p->getnext();

这个是我做过的一个未成形的链表。。。
很久前的东西了,没有优化过的。。。你参考参考吧。。至于你的,不好找,节点不必非封装。面向对象不一样要Private的。

#include <iostream>
#include <cassert>

using namespace std;

template < class T >
class CSList
{
public:
CSList(); // 创建一个空链表
CSList( unsigned n ); // 创建一个大小为n的单链表
CSList( unsigned n, const T& t ); // 创建一个大小为n,元素为t的复制的单链表
CSList( const CSList& ); // 拷贝构造函数
CSList& operator = ( const CSList& ); // 重载=
~CSList();
public:
typedef struct Data
{
T value;
Data* next;
}Data;
protected:
Data* m_pHead;
unsigned m_uCount;
public:
void push_back( const T& );
void pop_back();
void push_front( const T& );
void pop_front();
void printList(); // 打印所有成员,部分拥有

bool empty();
unsigned size();
void swap( CSList& );
void clear();
void insert( Data* pos, const T& );
void erase( Data*