打印本文 关闭窗口 |
|
| 一般线性链表类的C++实现 | |
| 作者:佚名 文章来源:不详 点击数 更新时间:2008/1/9 11:02:17 文章录入:杜斌 责任编辑:杜斌 | |
|
|
|
|
头文件: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。 |
|
打印本文 关闭窗口 |