Stack&Queue基本模板

View Code
  1 #include<iostream>
2 #include<assert.h>
3 using namespace std;
4
5 //const int maxSize=50;
6 const int stackIncreament=20;
7
8
9
10 template<class T> class Stack
11 {
12 public:
13 Stack(){}; //由于没写{},一直报错。undefined reference to Stack<int>::stack 注意模板的分离编译。
14 virtual void push(const T& x) =0;
15 virtual bool pop(T& x) =0;
16 virtual bool getTop(T& x) const =0;//设置成const函数是因为可以被const对象调用,即不改变const成员。
17 virtual bool isEmpty() const =0;
18 virtual bool isFull() const =0;
19 virtual int getSize() const =0;
20 };
21
22 template<class T> class SeqStack: public Stack<T>
23 {
24 private:
25 T* elements;
26 int top;
27 int maxSize;
28 void overflowProcess()
29 {
30 T *newArray=new T[maxSize+stackIncreament];
31 if(newArray==NULL)
32 {
33 cerr<<"增加容量时存储分配失败!"<<endl;
34 return;
35 }
36 for(int i=0;i<=top;i++)
37 newArray[i]=elements[i];
38 maxSize=maxSize+stackIncreament;
39 delete []elements;
40 elements=newArray;
41 }
42 public:
43 SeqStack(int sz=50):top(-1),maxSize(sz)
44 {
45 elements=new T[maxSize];
46 assert(elements!=NULL);
47 }
48 void push(const T& x)
49 {
50 if(isFull())
51 overflowProcess();
52 elements[++top]=x;
53 }
54 bool pop(T& x)
55 {
56 if(isEmpty())
57 return false;
58 x=elements[top--];
59 return true;
60 }
61
62
63 bool getTop(T& x) const
64 {
65 if(isEmpty())
66 return false;
67 x=elements[top];
68 return true;
69 }
70
71 bool isEmpty() const
72 {
73 if(top==-1)
74 return true;
75 return false;
76 }
77 bool isFull() const
78 {
79 if(top==maxSize-1)
80 return true;
81 return false;
82 }
83 int getSize() const
84 {
85 return maxSize;
86 }
87 template<class Type> friend ostream& operator<<(ostream& os,SeqStack<Type>& s); //我日啊,这一句改了好久好久啊。。。
88
89 };
90
91 template<class T> ostream& operator<<(ostream& os,SeqStack<T>& s)
92 {
93 os<<"top ="<<s.top<<endl;
94 for(int i=0;i<=s.top;i++)
95 os<<i<<":"<<s.elements[i]<<endl;
96 return os;
97 }
98
99
100 template<class T> class LinkNode
101 {
102 public:
103 LinkNode<T>* link;
104 T data;
105
106 LinkNode(LinkNode<T>* ptr=NULL)
107 {
108 link=ptr;
109 }
110 LinkNode(const T& val,LinkNode<T>* ptr=NULL)
111 {
112 data=val;
113 link=ptr;
114 }
115 };
116
117
118
119 template<class T> class LinkedStack:public Stack<T>
120 {
121 private:
122 LinkNode<T>* top;
123
124 public:
125 LinkedStack():top(NULL)
126 {
127 }
128 ~LinkedStack()
129 {
130 makeEmpty();
131 }
132 void makeEmpty()
133 {
134 LinkNode<T> *p;
135 while(top!=NULL)
136 {
137 p=top;
138 top=top->link;
139 delete p;
140 }
141 }
142 void push(const T& x)
143 {
144 top=new LinkNode<T>(x,top);
145 assert(top!=NULL);
146 }
147 bool pop(T& x)
148 {
149 if(isEmpty()==true)
150 return false;
151 LinkNode<T> *p=top;
152 top=top->link;
153 x=p->data;
154 delete p;
155 return true;
156 }
157 bool isEmpty() const
158 {
159 return (top==NULL)?true:false;
160 }
161
162 bool isFull() const
163 {
164 return false;
165 }
166 bool getTop(T& x) const
167 {
168 if(isEmpty()==true)
169 return false;
170 x=top->data;
171 return true;
172 }
173
174
175 int getSize() const
176 {
177 LinkNode<T>* p=top;
178 int k=0;
179 while(p!=NULL)
180 {
181 p=p->link;
182 k++;
183 }
184 return k;
185 }
186 template<class Type> friend ostream& operator<< (ostream& os,LinkedStack<Type>& s);
187 };
188
189 template<class T> ostream& operator<<(ostream& os,LinkedStack<T>& s)
190 {
191 os<<"栈中元素个数为:"<<s.getSize()<<endl;
192 LinkNode<T> *p=s.top;
193 int i=0;
194 while(p!=NULL)
195 {
196 os<<++i<<":"<<p->data<<endl;
197 p=p->link;
198 }
199 return os;
200 }
201
202 int main()
203 {
204 SeqStack<int> s;
205 int x;
206 s.push(4);
207 s.push(3);
208 s.push(2);
209 s.push(1);
210 s.push(1);
211 s.pop(x);
212 cout<<s;
213
214 SeqStack<string> s2;
215 s2.push("dfsf");
216 cout<<s2;
217
218 LinkedStack<string> ls;
219 ls.push("diyige");
220 ls.push("dierge");
221 ls.push("disange");
222 ls.push("disige");
223 ls.push("diwuge");
224 cout<<ls;
225 return 0;
226 }

程序写的不好,要改善。。

View Code
  1 #include<iostream>
2 #include<assert.h>
3 using namespace std;
4
5 template<class Type> class Queue
6 {
7 public:
8 Queue(){};
9 ~Queue(){};
10 virtual void enQueue(const Type& x)=0;
11 virtual bool deQueue(Type& x)=0;
12 virtual bool getFront(Type& x)=0;
13 virtual bool isEmpty() const=0;
14 virtual bool isFull() const=0;
15 virtual int getSize() const=0;
16 };
17
18 template<class Type> class SeqQueue:public Queue<Type>
19 {
20 protected:
21 int rear,front;
22 Type* elements;
23 int maxSize;
24 public:
25 SeqQueue(int sz=10):front(0),rear(0),maxSize(sz)
26 {
27 elements=new Type[maxSize];
28 assert(elements!=NULL);
29 }
30 ~SeqQueue(){delete []elements;}
31 void enQueue(const Type& x)
32 {
33 if(isFull())
34 return ;
35 elements[rear]=x;
36 rear=(rear+1)%maxSize;
37 }
38
39 bool deQueue(Type &x)
40 {
41 if(isEmpty())
42 return false;
43 x=elements[front];
44 front=(front+1)%maxSize;
45 return true;
46 }
47
48 bool getFront(Type &x)
49 {
50 if(isEmpty())
51 return false;
52 x=elements[front];
53 return true;
54 }
55
56 bool isFull() const
57 {
58 return ((rear+1)%maxSize==front)?true:false;
59 }
60
61 bool isEmpty() const
62 {
63 return (rear==front)?true:false;
64 }
65
66 int getSize() const
67 {
68 return (rear-front+maxSize)%maxSize;
69 }
70
71 void makeEmpty() const { front=rear=0; }
72
73 template<class T> friend ostream& operator<<(ostream& os,SeqQueue<T>& s);
74 };
75
76 template<class T> ostream& operator<<(ostream& os,SeqQueue<T>& q)
77 {
78 os<<"输出队列的内容(从队头到队尾共"<<q.getSize()<<"个元素):"<<endl;
79 for(int i=q.front;i!=q.rear;i=(i+1)%q.maxSize)
80 os<<q.elements[i]<<"";
81 os<<endl;
82 return os;
83 }
84
85 template<class Type> class LinkNode
86 {
87 public:
88 Type data;
89 LinkNode<Type>* link;
90
91 LinkNode(LinkNode<Type>* ptr=NULL)
92 {
93 link=ptr;
94 }
95 LinkNode(const Type& val,LinkNode<Type>* ptr=NULL)
96 {
97 data=val;
98 link=ptr;
99 }
100 };
101
102 template<class Type> class LinkedQueue:public Queue<Type>
103 {
104 protected:
105 LinkNode<Type> *front,*rear;
106 public:
107 LinkedQueue():rear(NULL),front(NULL){}
108 ~LinkedQueue(){ makeEmpty();}
109 void enQueue(const Type& x)
110 {
111 if(front==NULL)
112 front=rear=new LinkNode<Type>(x);
113 else
114 {
115 rear->link=new LinkNode<Type>(x);
116 rear=rear->link;
117 }
118 }
119
120 bool deQueue(Type& x)
121 {
122 if(isEmpty())
123 return false;
124 LinkNode<Type> *p=front;
125 x=front->data;
126 front=front->link;
127 delete p;
128 return true;
129 }
130
131 bool getFront(Type& x)
132 {
133 if(isEmpty())
134 return false;
135 x=front->data;
136 return true;
137 }
138
139 int getSize() const
140 {
141 LinkNode<Type> *p=front;
142 int k=0;
143 while(p!=NULL)
144 {
145 p=p->link;
146 k++;
147 }
148 return k;
149 }
150 bool isEmpty() const
151 {
152 if(front==NULL)
153 return true;
154 return false;
155 }
156 bool isFull() const
157 {
158 return false;
159 }
160 void makeEmpty()
161 {
162 LinkNode<Type> *p;
163 while(front!=NULL)
164 {
165 p=front;
166 front=front->link;
167 delete p;
168 }
169 }
170 template<class T> friend ostream& operator<<(ostream& os,LinkedQueue<T>& s);
171
172 };
173
174 template<class T> ostream& operator<<(ostream& os,LinkedQueue<T>& q)
175 {
176 os<<"输出队列的内容(从队头到队尾共"<<q.getSize()<<"个元素):"<<endl;
177 LinkNode<T> *p=q.front;
178 while(p!=NULL)
179 {
180 os<<p->data<<"";
181 p=p->link;
182 }
183 os<<endl;
184 return os;
185 }
186
187 int main()
188 {
189 SeqQueue<int> q1;
190 q1.enQueue(10);
191 q1.enQueue(20);
192 q1.enQueue(30);
193 int x;
194 q1.deQueue(x);
195 cout<<q1;
196
197
198 LinkedQueue<string> q2;
199 q2.enQueue("diyige");
200 q2.enQueue("dierge");
201 q2.enQueue("disange");
202 q2.enQueue("disige");
203 string s;
204 q2.enQueue(s);
205 q2.enQueue("diliuge");
206 q2.deQueue(s);
207 cout<<q2;
208
209 return 0;
210 }

顺序栈:
用两个数据成员,T* elements和int top,就像平时数组模拟一样。不过这里面的elements在构造函数的时候要动态分配。

链式栈:
用一个数据成员,LinkNode<Type>* top就可以了。
构造函数的时候,直接top初始化为NULL就可以了,为什么不像链表实现那样分配一个new LinkNode<Type>给它呢,因为栈里面的top是动态变化的,总是表示新加入的节点。所以push操作只需要一句简单的top=new LinkNode<Type>(x,top);就可以了。


顺序队列:
维持三个数据成员,T* elements,int rear,int front。
构造函数时动态分配elements,rear和front都初始化为0。rear总是指向队尾端得下一个位置。入队时对rear位置初始化,rear再加1(注意可能是循环的)。

链式队列:
维持两个数据成员:LinkNode<Type> *front,*rear。
构造函数时都初始化为NULL,刚开始加入时front和rear指向同一位置,但是之后rear->link=new LinkNode<Type>(x),rear=rear->link;即rear总是指向队尾元素。

原文地址:https://www.cnblogs.com/YipWingTim/p/2247744.html