C++实现模板(template)链表类 Posted on 2018-06-09 | In C&C++ , C++学习 , 文章页 | | 链表相信都很熟悉了就不过多的介绍,如果需要自行翻阅数据结构。这里面使用到了之前结点类 创建LinkedList.h头文件 代码: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302#ifndef LINKEDLIST_CLASS#define LINKEDLIST_CLASS#include <iostream>#include <cstdlib>#include "Node.h"using namespace std;template <class T>class LinkedList{ private: Node<T> \*front, \*rear; Node<T> \*prevPtr, \*currPtr; int size; int position; Node<T> \*GetNode(const T& item,Node<T> \*ptrNext=NULL); void FreeNode(Node<T> *p); void CopyList(const LinkedList<T>& L); public: LinkedList(void); LinkedList(const LinkedList<T>& L); ~LinkedList(void); LinkedList<T>& operator= (const LinkedList<T>& L); int ListSize(void) const; int ListEmpty(void) const; void Reset(int pos = 0); void Next(void); int EndOfList(void) const; int CurrentPosition(void) const; void InsertFront(const T& item); void InsertRear(const T& item); void InsertAt(const T& item); void InsertAfter(const T& item); T DeleteFront(void); void DeleteAt(void); T& Data(void); void ClearList(void);};template <class T>Node<T> *LinkedList<T>::GetNode(const T& item, Node<T>* ptrNext){ Node<T> *p; p = new Node<T>(item,ptrNext); if (p == NULL) { cout << "Memory allocation failure!\\n"; exit(1); } return p;}template <class T>void LinkedList<T>::FreeNode(Node<T> *p){ delete p;}template <class T>void LinkedList<T>::CopyList(const LinkedList<T>& L){ Node<T> *p = L.front; int pos; while (p != NULL) { InsertRear(p->data); p = p->NextNode(); } if (position == -1) return; prevPtr = NULL; currPtr = front; for (pos = 0; pos != position; pos++) { prevPtr = currPtr; currPtr = currPtr->NextNode(); }}template <class T>LinkedList<T>::LinkedList(void): front(NULL), rear(NULL), prevPtr(NULL),currPtr(NULL), size(0), position(-1){}template <class T>LinkedList<T>::LinkedList(const LinkedList<T>& L){ front = rear = NULL; prevPtr = currPtr = NULL; size = 0; position = -1; CopyList(L);}template <class T>void LinkedList<T>::ClearList(void){ Node<T> \*currPosition, \*nextPosition; currPosition = front; while(currPosition != NULL) { nextPosition = currPosition->NextNode(); FreeNode(currPosition); currPosition = nextPosition; } front = rear = NULL; prevPtr = currPtr = NULL; size = 0; position = -1;}template <class T>LinkedList<T>::~LinkedList(void){ ClearList();}template <class T>LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& L){ if (this == &L) return *this; ClearList(); CopyList(L); return *this;}template <class T>int LinkedList<T>::ListSize(void) const{ return size;}template <class T>int LinkedList<T>::ListEmpty(void) const{ return size==0;}template <class T>void LinkedList<T>::Next(void){ if (currPtr != NULL) { prevPtr = currPtr; currPtr = currPtr->NextNode(); position++; }}template <class T>int LinkedList<T>::EndOfList(void) const{ return currPtr == NULL;}template <class T>int LinkedList<T>::CurrentPosition(void) const{ return position;}template <class T>void LinkedList<T>::Reset(int pos){ int startPos; if (front == NULL) return; if (pos < 0 || pos > size-1) { cerr << "Reset: Invalid list position: " << pos << endl; return; } if(pos == 0) { prevPtr = NULL; currPtr = front; position = 0; } else { currPtr = front->NextNode(); prevPtr = front; startPos = 1; for(position=startPos; position != pos; position++) { prevPtr = currPtr; currPtr = currPtr->NextNode(); } }}template <class T>T& LinkedList<T>::Data(void){ if (size == 0 || currPtr == NULL) { cerr << "Data: invalid reference!" << endl; exit(1); } return currPtr->data;}template <class T>void LinkedList<T>::InsertFront(const T& item){ if (front != NULL) Reset(); InsertAt(item);}template <class T>void LinkedList<T>::InsertRear(const T& item){ Node<T> *newNode; prevPtr = rear; newNode = GetNode(item); if (rear == NULL) front = rear = newNode; else { rear->InsertAfter(newNode); rear = newNode; } currPtr = rear; position = size; size++;}template <class T>void LinkedList<T>::InsertAt(const T& item){ Node<T> *newNode; if (prevPtr == NULL) { newNode = GetNode(item,front); front = newNode; } else { newNode = GetNode(item); prevPtr->InsertAfter(newNode); } if (prevPtr == rear) { rear = newNode; position = size; } currPtr = newNode; size++;}template <class T>void LinkedList<T>::InsertAfter(const T& item){ Node<T> *p; p = GetNode(item); if (front == NULL) { front = currPtr = rear = p; position = 0; } else { if (currPtr == NULL) currPtr = prevPtr; currPtr->InsertAfter(p); if (currPtr == rear) { rear = p; position = size; } else position++; prevPtr = currPtr; currPtr = p; } size++;}template <class T>T LinkedList<T>::DeleteFront(void){ T item; Reset(); if (front == NULL) { cerr << "Invalid deletion!" << endl; exit(1); } item = currPtr->data; DeleteAt(); return item;}template <class T>void LinkedList<T>::DeleteAt(void){ Node<T> *p; if (currPtr == NULL) { cerr << "Invalid deletion!" << endl; exit(1); } if (prevPtr == NULL) { p = front; front = front->NextNode(); } else p = prevPtr->DeleteAfter(); if (p == rear) { rear = prevPtr; position--; } currPtr = p->NextNode(); FreeNode(p); size--;}#endif 使用: 代码: 1234567891011121314151617181920212223242526272829303132333435363738#include<iostream>#include "LinkedList.h"using namespace std;int main(){ LinkedList<int> A, B; int a=0; cout<<"输入4个数用于创建链表A:"; for(int i=0;i<4;i++) { cin>>a; A.InsertRear(a); } cout<<"输入3个数用于创建链表A:"; for(int i=0;i<3;i++) { cin>>a; B.InsertRear(a); } A.Reset(); B.Reset(); cout << "将B中的元素复制到链表A的尾部……" << endl; B.Reset(); while(!B.EndOfList()) { A.InsertRear(B.Data()); B.Next(); } A.Reset(); cout << "合并之后,链表A的元素为:" ; while(!A.EndOfList()) { cout << A.Data() << " "; A.Next(); } cout << endl;} 结果: Hobby lead creation, technology change world. Post author: StriveZs Post link: 1346.html Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.