标签:push a ali copy abs 基于 head 模板 his abstract
数据结构List,叫列表,也叫线性表。栅栏fence的概念,也就是操作定位。
List的抽象模板类代码:
1 /* class List */ 2 template <class Elem> 3 class List 4 { 5 public: 6 //set the position of the fence 7 virtual bool setPos(int pos) = 0; 8 //insert an element 9 virtual bool insert(const Elem& e) = 0; 10 //remove an element 11 virtual bool remove(Elem& e) = 0; 12 };
Alistl类的模板类代码:
1 /* class AList */ 2 const int DefaultListSize = 100; 3 template <class Elem> 4 class AList:public List<Elem> 5 { 6 private: 7 Elem* listArray;//Array holding list 8 int maxSize; //Maximum size of list 9 int listSize; //Actual elem count 10 int fence; //Position of fence; 11 public: 12 AList(int size=DefaultListSize){ 13 listArray = new Elem[size]; 14 maxSize = fence = 0; 15 } 16 ~AList(){delete[] listArray;} 17 //重写虚函数要声明 18 virtual bool setPos(int pos); 19 virtual bool insert(const Elem& e); 20 virtual bool remove(Elem& e); 21 bool getValue(Elem& it)const; 22 }; 23 //set the Position of the fence 24 template <class Elem> 25 bool AList<Elem>::setPos(int pos){ 26 if((pos>=0)&&(pos<=listSize))//判断是否在合理的区间 27 fence = pos; 28 return (pos>=0)&&(pos<=listSize); 29 } 30 //get the first element after the fence 31 template <class Elem> 32 bool AList<Elem>::getValue(Elem& it)const{ 33 if(fence == listSize) return false;//判断是否重叠 34 else{ 35 it = listArray[fence]; 36 return true; 37 } 38 } 39 template <class Elem> 40 bool AList<Elem>::insert(const Elem& item){ 41 if(listSize == maxSize) return false;//判断是否有空间 42 //Shift Elems up to make room 43 for(int i=listSize; i>fence; i--) 44 listArray[i] = listArray[i-1]; 45 listArray[fence] = item; 46 listSize++;//Increment list size 47 return true; 48 } 49 template <class Elem> 50 bool AList<Elem>::remove(Elem& it){ 51 if(fence == listSize) return false; 52 it = listArray[fence];//Copy Elem 53 for(int i=fence; i<listSize; ++i) 54 listArray[i] = listArray[i+1]; 55 listSize--; //Decrement size 56 return true; 57 }
链表结点模板类代码:
1 //list node 2 template <class Elem> 3 class Node{ 4 public: 5 Elem element;//Value for this node 6 Node* next; //Pointer to next node 7 Node(const Elem& elemval,Node* nextval = NULL) 8 {element = elemval; next = nextval;} 9 };
LList模板类代码:
1 template <class Elem> 2 class LList:public List<Elem> 3 { 4 private: 5 Node<Elem>* head;//Point to list header 6 Node<Elem>* tail;//Pointer to last Elem 7 Node<Elem>* fence;//Last element on left 8 int length; //Length of list 9 public: 10 LList() 11 { 12 fence = head = tail = new Node<Elem>; 13 length = 0; 14 } 15 virtual bool getValue(Elem& it)const; 16 virtual bool insert(const Elem& item); 17 virtual bool remove(Elem& it); 18 virtual bool setPos(int pos); 19 }; 20 template <class Elem> 21 bool LList<Elem>::getValue(Elem& it)const{ 22 if(fence == tail) return false; 23 it = fence->next->element; 24 return true; 25 } 26 template <class Elem> 27 bool LList<Elem>::insert(const Elem& item){ 28 fence->next = new Node<Elem>(item,fence->next); 29 if(tail == fence) tail = fence->next; 30 length++; 31 return true; 32 } 33 template <class Elem> 34 bool LList<Elem>::remove(Elem& it){ 35 if(fence->next == NULL) return false; 36 it = fence->next->element;//Remember value_comp 37 Node<Elem>* ltemp = fence -> next; 38 fence->next = ltemp->next;//Remove 39 if(tail == ltemp) 40 tail = fence; //Reset tail 41 delete ltemp;//Reclaim space 42 length--; 43 return true; 44 } 45 template <class Elem> 46 bool LList<Elem>::setPos(int pos){ 47 if((pos < 0) || (pos > length)) 48 return false; 49 fence = head; 50 for(int i=0; i<pos; ++i) 51 fence = fence->next; 52 return true; 53 }
Stack抽象模板类代码:
1 //Stack abstract class 2 template <class Elem> 3 class Stack 4 { 5 public: 6 //Push an element onto the top of the stack 7 virtual bool push(const Elem& e) = 0; 8 //Remove the element at the top of the stack 9 virtual bool pop(Elem& e) = 0; 10 //Get a copy of the top element in the stack 11 virtual bool topValue(Elem& e) const = 0; 12 //Return the number of element in the stack 13 virtual int length() const = 0; 14 //Reinitialize the stack 15 virtual void clear() = 0; 16 };
基于数组的栈模板类代码:
1 //Array-based stack implementation 2 template <class Elem> 3 class AStack:public Stack<Elem> 4 { 5 private: 6 Elem* listArray;//Array holding stack elements 7 int size; //Maximum size of stack 8 int top; //The position to insert the new element 9 public: 10 AStack(int sz) 11 { 12 size = sz; 13 top = 0; 14 listArray = new Elem[sz]; 15 } 16 ~AStack(){delete[] listArray;} 17 virtual bool push(const Elem& item); 18 virtual bool pop(Elem& it); 19 }; 20 template <class Elem> 21 bool AStack<Elem>::push(const Elem& item){ 22 if(top == size) return false; //Stack is full 23 else{ 24 listArray[top++] = item; 25 return true; 26 } 27 } 28 //pop top element 29 template <class Elem> 30 bool AStack<Elem>::pop(Elem& it){ 31 if(top == 0) return false; 32 else{ 33 it = listArray[--top]; 34 return true; 35 } 36 }
队列抽象模板类代码:
1 template <class Elem> 2 class Queue 3 { 4 public: 5 virtual bool enqueue(const Elem& e) = 0; 6 virtual bool dequeue(Elem& e) = 0; 7 virtual bool frontValue(Elem& e) = 0; 8 virtual int length() const = 0; 9 virtual void clear() = 0; 10 }
基于链表的队列模板类代码:
标签:push a ali copy abs 基于 head 模板 his abstract
原文地址:https://www.cnblogs.com/GoldenEllipsis/p/12106546.html