这个为什麼不能插入结点,请C++高手指点

来源:百度知道 编辑:UC知道 时间:2024/06/02 01:25:00
#include<iostream.h>
class CWnodeChain;
class CWnode
{
friend CWnodeChain;
private:
int data;
CWnode *link;
};
class CWnodeChain
{
public:
CWnodeChain(){first=0;}
~CWnodeChain();
bool IsEmpty(){return first==0;}
int Length();
int Find(int k);
int Search(int x);
CWnodeChain Insert(int nd);
CWnodeChain Delete(int nd);
void Output();
private:
// CWnode X;
CWnode *first;
};
CWnodeChain::~CWnodeChain()
{
CWnode *p;
while(first)
{
p=first->link;
delete first;
first=p;
}
}
int CWnodeChain::Length()
{
CWnode *p=first;
int len=0;
while(p)
{
//cout<<"len";
len++;
p=p->link;
}
return len;
}
int CWnodeChain::Find(int k)
{
int n=0;
CWnode *p=first;
while(p-

问题多多,其中一个是 指针p == NULL 不等价于 p == true。刚写了个程序,与你这个类似,可以看下。
/*************************** SortedSet.h ***********************************/
/* */
/* 包含类模板SortedSet的定义 */
/* */
/*************************** SortedSet.h ***********************************/

#ifndef _SortedSet_H__
#define _SortedSet_H__

#include <iostream>

template<typename EType>
class SortedSet
{

public:

//用以表示节点
struct Node
{
Node* next; //指向下一个节点的指针
Node* pre; //指向前一个节点的指针
EType data; //数据单元
Node(const EType& dat):data(dat), next(NULL), pre(NULL){}
};

Node* head; //
int size;
int MaxSize;

SortedSet(int maxsize = 100): head(NULL), size(0), MaxSize(maxsize){}

//用以显示结构中的算有元素
void printSet()
{
Node* pos = head;
while (pos != NULL)
{
std::cout << pos->data << " &quo