|
一般线性链表类的C++实现 |
|
|
|
www.nanhushi.com 佚名 不详 |
|
以下的C++类LinkList实现了线性链表的一般操作。可以直接在其他的程序中直接建立它的对象,其中线性表中的数据在此为整型,具体应用的时候可以适当的修改,并可以在此基础上继续封装特定的功能。
头文件:LinkList.h
typedef struct LNode { int data; struct LNode *next; }LNode, *pLinkList;
class LinkList { private: pLinkList m_pList; int m_listLength; public: LinkList(); ~LinkList(); bool InitList (); bool DestroyList (); bool ClearList(); bool IsEmpty (); int GetLength (); bool GetNode(int position, LNode** node); int LocateElem(int elem); bool SetNodeData(int position, int newData); bool GetNodeData(int position, int &data); bool InsertNode(int beforeWhich, int data); bool DeleteNode(int position); };
Cpp文件:LinkList.cpp
#include <iostream.h> #include "LinkList.h"
LinkList::LinkList() { m_pList = NULL; m_listLength = 0;
InitList(); }
LinkList::~LinkList() { if (!DestroyList()) { DestroyList(); } }
//初始化,分配一个头节点。 bool LinkList::InitList() { if (!(m_pList = new LNode)) { return false; } m_pList->next = NULL;
return true; }
//销毁链表。 bool LinkList::DestroyList() { if (!ClearList()) { return false; }
delete m_pList;
return true; }
//判断链表是否为空。若为空,返回true,否则返回false。
|
|
|
|
| 文章录入:杜斌 责任编辑:杜斌 |
|
上一篇文章: C++中确定基类有虚析构函数 下一篇文章: C++中用函数模板实现和优化抽象操作 |
| 【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |
| |
|