用C++模板实现线性表的链式存储的基本算法(数据结构C++版 北京科海)
2012-11-07 11:06
856 查看
/* 文件名称:LinkList.h 文件内容:线性链表的定义 */ #ifndef LINKLIST_H #define LINKLIST_H #include <iostream> using namespace std; template<class T> class LinkList; //LNode类模板定义 template<class T> class LNode { friend class LinkList<T>; public: LNode():next(NULL) { } LNode(const T& item):data(item),next(NULL) { } LNode(const T& item, LNode<T>* Next) { data = item; next = Next; } ~LNode() { } public: LNode<T>* Next(); void InsertAfter(LNode<T> *p); LNode<T>* GetNode(const T& item, LNode<T>* Next); LNode<T>* RemoveAfter(); private: T data; LNode<T> *next; }; /*************************************************************/ //LinlList类模板定义 template<class T> class LinkList { public: LinkList() { tail = head = new LNode<T>(0); } LinkList(const T& val) { tail = head = new LNode<T>(val); } ~LinkList() { Empty(); delete head; } public: void Empty(); int LeadLength() const; int NoLeadLength(LinkList L); LNode<T>* FindByVal(T val); LNode<T>* FindByIndex(int i); void AddToHead(T val); void AddToTail(T val); int Insert(T val, int i); T* Remove(int i); T* Get(int i); void Print(); private: LNode<T>* head; LNode<T>* tail; }; #endif /*************************************************************/ /*LNode类的操作实现程序*/ template<class T> LNode<T>* LNode<T>::Next() { return next; } template<class T> void LNode<T>::InsertAfter(LNode<T> *p) { p->next = next; next = p; } template<class T> LNode<T>* LNode<T>::GetNode(const T& item, LNode<T>* Next) { LNode<T>* newNode = new LNode<T>(item); newNode->next = Next; return newNode; } template<class T> LNode<T>* LNode<T>::RemoveAfter() { LNode<T>* tempNodePtr = next; if (next == NULL) { return NULL; } next = tempNodePtr->next; return next; } /*************************************************************/ /*LinkList类的操作实现程序*/ //清空链表 template<class T> void LinkList<T>::Empty() { LNode<T>* temp; while (head->next != NULL) { temp = head->next; head->next = temp->next; delete temp; } tail = head; } //带头结点的单链表的表长 template<class T> int LinkList<T>::LeadLength() const { int nlen = 0; LNode<T>* temp = head->next; while (temp) { temp = temp->next; nlen++; } return nlen; } //不带头结点的单链表的表长 template<class T> int LinkList<T>::NoLeadLength(LinkList L) { LNode* p = L; if(p == NULL) return 0; int j = 1; while (p->next) { p = p->next; j++; } return j; } //数据定位查找 template<class T> LNode<T>* LinkList<T>::FindByVal(T val) { LNode<T>* temp = head->next; while (temp && temp->data!=val) { temp = temp->next; } return temp; } //按序号查找 template<class T> LNode<T>* LinkList<T>::FindByIndex(int i) { if(i<-1) return NULL; if(i==-1) return head; LNode<T>* temp = head; int j = 0; while(temp && (j<i)) { temp = temp->next; j++; } return temp; } /* 后插和前插(实更关心数据元素之间的逻辑关系,那么对于前插可以按后插操作,然后再数据交换) */ //在链表的头部插入结点建立单链表 template<class T> void LinkList<T>::AddToHead(T val) { LNode<T> *newNode = new LNode<T>(val); newNode->next = head->next; head->next = newNode; LNode<T> *p = head; while (p->next != NULL) { p = p->next; } tail = p; } //在链表的尾部插入结点建立单链表 template<class T> void LinkList<T>::AddToTail(T val) { LNode<T> *newNode = new LNode<T>(val); tail->next = newNode; tail = newNode; } //插入运算 template<class T> int LinkList<T>::Insert(T val, int i) { LNode<T>* temp = FindByIndex(i-1); if (temp==NULL) { return 0; } LNode<T>* newNode = new LNode<T>(val, temp->next); if (temp->next==NULL) { tail = newNode; } temp->next = newNode; return 1; } //删除结点 template<class T> T* LinkList<T>::Remove(int i) { LNode<T> *q; LNode<T> *p = FindByIndex(i-1); if (p==NULL || p->next == NULL) { return NULL; } q = p->next; p->next = q->next; T *val = new T(q->data); if (q == tail) { tail = p; } delete q; return val; } //得到第i个结点 template<class T> T* LinkList<T>::Get(int i) { LNode<T>* p = FindByIndex(i); if (p==NULL || p==head) { return NULL; }else { return &(p->data); } } template<class T> void LinkList<T>::Print() { LNode<T>* temp = head->next; cout << "共有:" << LeadLength() << "个节点" << endl; while (temp) { cout << temp->data << " "; temp = temp->next; } cout << endl; } //测试 #include "LinkList.h" int main() { LinkList<int> ls; ls.AddToHead(2); ls.AddToHead(4); ls.AddToHead(8); ls.Print(); cout << "--------------------------------" << endl; ls.AddToTail(11); ls.AddToTail(18); ls.AddToTail(20); ls.Print(); cout << "-------------------------------" << endl; ls.Insert(100, 4); ls.Insert(101,5); ls.Print(); cout << "-------------------------------" << endl; int *data = ls.Remove(1); cout << "删除index=1的数据:" << *data << endl; ls.Print(); int *temp = ls.Get(3); cout << "index=3的数据:" << *temp << endl; cout << "-------------------------------" << endl; LNode<int> *addr_val = ls.FindByVal(100); cout << "value=100的地址:" << addr_val << endl; LNode<int> *addr_index = ls.FindByIndex(2); cout << "index=2的地址:" << addr_index << endl; ls.Print(); ls.Empty(); ls.Print(); system("pause"); return 0; }
相关文章推荐
- C++类模板 实现双向循环链表的基本算法 《数据结构》(C++版 北京科海)中摘抄
- 数据学习之线性表的链式存储C++实现
- C++类模板实现线性表单链式存储
- 线性表链式存储C++实现
- 队列(链式存储)C++模板实现
- 栈(链式存储) C++模板实现
- C语言——链式存储实现栈的基本运算算法
- 线性表链式存储结构下基本操作的实现(初始化、赋值、取值、插入、删除、归并等)
- 线性表的链式存储实现c++
- 线性结构的顺序存储和链式存储的实现代码(一)
- 线性表的链式存储结构之单链表结点类的实现_Java
- c++模板类实现线性表顺序存储
- C++基本算法收集及代码实现
- 线性表的链式存储实现c语言
- 线性表链式存储实现
- [C++]数据结构:线性表的公式化描述和链式描述的结构特点与基本操作
- 线性表的链式存储和实现
- C++实现顺序结构线性表的基本操作
- 利用C++的基本算法实现十个数排序
- 线性表的链式表示和实现----实用算法