顺序栈

头文件:

 1 #ifndef STACK_H
 2 #define STACK_H
 3 #include <iostream>
 4 #include <fstream>
 5 #include <assert.h>
 6 using namespace std;
 7 
 8 const int stackIncreament=20;
 9 
10 template <class T>
11 class SeqStack
12 {
13 public:
14     //SeqStack(){}  //new
15     SeqStack(int sz);
16     //~SeqStack();
17     void Push(const T &x);
18     void Pop(const T &x);
19     bool getTop(T &x);
20     bool isEmpty()const;
21     bool isFull()const;
22     //int getSize();
23     //void makeEmpty();
24     //friend ostream & operator<<(ostream& os,SeqStack<T> &S);      // '<<'后面加上'<>'表明这个函数是个函数模板   ?????
25     friend ostream & operator<< <>(ostream& os,SeqStack<T> &S);       //这里还是要加上<>,才能编译通过
26 private:
27     T *element;
28     int top;
29     int maxSize;
30     void overflowProcess();
31 };
32 
33 
34 
35 #endif
View Code

函数实现:

 1 #include <iostream>
 2 #include <fstream>
 3 #include <assert.h>
 4 #include "Stack.h"
 5 using namespace std;
 6 
 7 template <class T>
 8 SeqStack<T>::SeqStack(int sz)
 9 {
10     maxSize=sz;
11     top=-1;
12     element=new T[maxSize];
13     assert(element!=NULL);
14 }
15 
16 template <class T>
17 void SeqStack<T>::overflowProcess()
18 {
19     T *newarray=new T[maxSize+stackIncreament];
20     if (newarray=NULL)
21     {
22         cout<<"存储分配错误"<<endl;
23         exit(1);
24     }
25     for(int i=0;i<=top;i++)
26     {
27         newarray[i]=element[i];
28     }
29     maxSize=maxSize+stackIncreament;
30     delete[] element;
31     element=newarray;
32 }
33 
34 template <class T>
35 void SeqStack<T>::Push(const T &x)
36 {
37     if (isFull()==true)
38     {
39         overflowProcess();
40     }
41     element[++top]=x;
42 }
43 
44 template <class T>
45 void SeqStack<T>::Pop(const T &x)
46 {
47     if(isEmpty()==true)
48     {
49         return false;
50     }
51     x=element[top--];                //先退栈,然后top-1
52     return true;
53 }
54 
55 template <class T>
56 bool SeqStack<T>::getTop(T &x)
57 {
58     if (isEmpty()==true)
59     {
60         return false;
61     }
62     x=element[top];
63     return true;
64 }
65 
66 template <class T>
67 bool SeqStack<T>::isEmpty()const
68 {
69     if (top==-1)
70     {
71         return true;
72     }
73     else
74         return false;
75 }
76 
77 template <class T>
78 bool SeqStack<T>::isFull()const
79 {
80     if (top==maxSize-1)
81     {
82         return true;
83     }
84     else 
85         return false;
86 }
87 
88 
89 template <class T>
90 ostream &operator<<(ostream &os,SeqStack<T> &s)
91 {
92     cout<<"top="<<s.top<<endl;
93     for(int i=0;i<=s.top;i++)
94     {
95         cout<<i<<"   :      "<<s.element[i]<<endl;
96     }
97     return os;
98 }
View Code

测试:

 1 #include "Stack.cpp"
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     SeqStack<int> a(10);
 8     a.Push(3);
 9     a.Push(4);
10     a.Push(5);
11 
12     bool i=a.isEmpty();
13     if(i)
14         cout<<"Y";
15     else
16         cout<<"N";
17     cout<<endl;
18 
19     cout<<a;
20     return 0;
21 }
View Code

这个是用数组实现栈,并没有很用心的全部测试到,原因在于这个写的并不是很有用,在链式栈中我会更全面的测试函数的。

原文地址:https://www.cnblogs.com/ruirui610/p/3399350.html