Five linear form, C ++ code that (school online, SCUT)

Data Structure List, call list, also called the linear form. The concept fence fence, which is the operation of the positioning.

List of abstract template class code:

 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 class template class code:

 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 is      AList ( int size = DefaultListSize) {
 13 is          ListArray = new new Elem [size];
 14          the maxSize fence = = 0 ;
 15      }
 16      ~ AList () { Delete [] ListArray;}
 . 17      // override the virtual function to declare 
18 is      Virtual  BOOL SetPos ( int POS);
 . 19      Virtual  BOOL INSERT ( const Elem & E);
 20 is      Virtual  BOOL Remove (Elem & E);
 21 is      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> :: (Elem & IT) the getValue const {
 33 is      IF (== fence listsize) return  to false ; // determines whether the overlap 
34 is      the else {
 35          IT = ListArray [fence];
 36          return  to true ;
 37 [      }
 38 is  }
 39 Template < class Elem>
 40  BOOL AList <Elem> :: INSERT ( const Elem & Item) {
 41 is      iF (== listsize the maxSize) return  to false ; // determines whether there is space
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 }

LLI node template class code:

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 template class code:

 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 abstract template class code:

 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 };

Array-based stack template class code:

 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 }

Queue abstract template class code:

 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 }

Based on the list of queue template class code:

Guess you like

Origin www.cnblogs.com/GoldenEllipsis/p/12106546.html