一个关于链表的问题,请大家帮忙。

Lemon-sai 2002-02-15 05:15:38
class ListNode
{
public:
~ListNode ();
void insert (char);
void deletel (char);
int isEmpty ();
void printList ();
private:
static ListNode *startPtr; //想问的是这句
ListNode *currentPtr;
ListNode *previousPtr;
ListNode *nextPtr;
char data;
};

这是我自己写的一个链表的头文件,在实现中也没有问题,
只要用,ListNode list,之后,它的操作都好,现在想问,
我写的
static ListNode *startPtr;这里是用来记录链表首地址的
在只定义一个链表时很好用,又节省了空间。但是要定义两个以上的
链表是,就不行了,但是如果去掉static有比较浪费空间,有什么好
办法吗?
...全文
61 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lifanxi 2002-02-16
  • 打赏
  • 举报
回复
同意zheng_can!建两个类比较好,一个LinkList,一个ListNode,我自己一直这样用,效果不错。
要不就用STL。
hz129 2002-02-16
  • 打赏
  • 举报
回复
这是我学模板时写的一个练习的一部分,当时没看过effective C++,设计中还有很多不合适的部分,仅供参考吧

template <class T>
class CNode {
public:
// 构造函数
// 新建CNode对象,next指针设为空
CNode();
// 新建CNode对象,用data初始化结点,next指针设为空
CNode(const T &data);
// 拷贝构造CNode对象,next指针设为空
CNode(const CNode<T> &rhs);
~CNode();

// 赋值操作
// data: 结点数据
CNode<T>& operator=(const T &data);
// 赋值操作
// node: 结点
CNode<T>& operator=(const CNode<T> &node);
// 比较操作,对象地址相同则认为相同
bool operator==(const CNode<T> &node) const { return this==&node; }

// 取出结点数据(const)
const T& GetData() const { return data; }
// 设置结点数据为data
void SetData(const T& data) { *this = data; }

CNode<T> *next;
private:
T data;
};


template <class T>
class CList {
public:
// head指向一个空的头结点
CList();
// 构造具有一个结点的链表
CList(const T &data);
// 用一个结点及其后继结点拷贝构造链表
CList(const CNode<T> &node);
// 拷贝构造链表
CList(const CList<T> &list);
// 从一个data数组构造链表
CList(const T listArray[], int length);
// 析构,删除所有CNode结点
~CList();

// 返回第index个结点的引用
// index : 0 based
//CNode<T>& operator[](int index);
CNode<T>& operator[](int index) const;

// 赋值操作
CList<T>& operator=(CList<T> &list);
// 比较两个CList对象,如果对象地址相同则认为相同
const bool operator==(const CList<T> &list) const;

// 返回一个数组长度
// arr: 用于存放数组的头指针
// length: 最多可存放的数据个数
const int ToArray(T *const arr, int length) const;

// 向list末尾添加一个结点
// data: 结点数据
void Append(T &data);
// 向list末尾添加一个结点
// node: 要添加的结点
void Append(CNode<T> &node);
// 向list中插入结点
// data: 结点数据
void Insert(T &data, int index=0);
// 向list中插入结点
// node: 要添加的结点
// index: 插入位置,0 based
// index < 0: 在表头插入
// index >= nodecount: 在表尾插入
void Insert(CNode<T> &node, int index=0);
// 从list中删除一个结点
// index: 0 based
// index < 0 或 index >= nodecount: 不删除任何结点,返回
void Remove(int index);
// 删除所有结点,保留head所指向的空头结点
void RemoveAll();

// 返回list中结点个数
const int GetLength() const { return nodecount; }

private:
CNode<T> * head;
CNode<T> * tail;
int nodecount;
};
zheng_can 2002-02-15
  • 打赏
  • 举报
回复
其实你可以直接使用 STL::list
如果一定要自己写的话,可以参照下面的:
template < typename ElemType >
class MyList {
public:
MyList();
~MyList();
bool Insert( const ElemType& );
bool Delete( const ElemType& );
bool IsEmpty() const;
void Print();
...
private:
int _length;
ElemType* _startPtr;
ElemType* _currentPtr;
ElemType* _previousPtr;
ElemType* _nextPtr;
...
};
// 信手打来,可能有错,望多包涵:)
zheng_can 2002-02-15
  • 打赏
  • 举报
回复
哦,现在我理解你的操作了
你应该写两个类,别嫌多!
一个是结点类,专门负责管理各个节点
一个是链表类,由它负责链表的相关信息的管理、操作
不要试图去写那种具有很广泛功能的类,那样不好,也不易纠错
Lemon-sai 2002-02-15
  • 打赏
  • 举报
回复
但对于一个链表,我需要一个首地址的指针啊。
magicblue 2002-02-15
  • 打赏
  • 举报
回复
实现部分改进一下吧,增加节点后就删除那个作用的pointer(可以是临时的)
Lemon-sai 2002-02-15
  • 打赏
  • 举报
回复
因为如果去掉static每增加一个结点,就会产生一个startPtr,
不去掉的话,不管产生多少结点,用的只有一个startPtr
zheng_can 2002-02-15
  • 打赏
  • 举报
回复
去掉static为什么会浪费空间???
Lemon-sai 2002-02-15
  • 打赏
  • 举报
回复
但这样封装的效果就没有了
dev_uoboy 2002-02-15
  • 打赏
  • 举报
回复
有,全局变量:)

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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