STL-stack和顺序栈实现括号匹配

2018-11-11-14:28:31

1.顺序栈

下面是我用数组实现的顺序栈,包含的函数有出入栈,查看栈顶元素,栈的大小,栈是否空等函数,当栈空间不够用时,对应的数组会自动增长。

 

  1 /*********************************************************
  2     顺序栈实现括号匹配。
  3     main函数操作:
  4         1.在这里main函数内部主要以解决括号匹配问题。
  5         2.本例中包含"()""[]"{}"三种括号。
  6         3.输入一个表达式,如果括号匹配则输出True,否则输出False。
  7 **********************************************************/
  8 #include <cstdio>
  9 #include <cstring>
 10 #include <cstdlib>
 11 #include <iostream>
 12 using namespace std;
 13 typedef char SElemtype;
 14 #define INITSTACKSIZE 100
 15 #define STACKINCRESMENT 40
 16 #define OverFlow -1
 17 typedef struct {
 18     SElemtype*Top;//指向栈顶
 19     SElemtype*Base;//指向栈底
 20     int Stacksize;//以sizeof(SElemtype)为单位
 21 }Stack;
 22 
 23 bool InitStack(Stack&S);
 24 bool StackEmpty(Stack S);
 25 int StackLength(Stack S);
 26 bool GetTop(Stack S,SElemtype&Elem);
 27 bool Push(Stack&S,SElemtype Elem);
 28 bool Pop(Stack&S,SElemtype&Elem);
 29 bool PrintStack(Stack S);
 30 //main函数内所有数据均为测试数据,读者可根据自己测试方式自行调换
 31 
 32 int main()
 33 {
 34     Stack S;
 35     InitStack(S);
 36     char c;
 37     string str;
 38     cin>>str;
 39     for(int i=0;i<str.length();i++){
 40         if(str[i]=='('||str[i]=='['||str[i]=='{'){
 41             Push(S,str[i]);
 42             continue;
 43         }
 44         if(str[i]==')'){
 45             GetTop(S,c);
 46             if(c=='(')  Pop(S,c);
 47             if(c=='['||c=='{')  break;
 48         }
 49         if(str[i]==']'){
 50             GetTop(S,c);
 51             if(c=='[')  Pop(S,c);
 52             if(c=='('||c=='{')  break;
 53         }
 54         if(str[i]=='}'){
 55             GetTop(S,c);
 56             if(c=='{')  Pop(S,c);
 57             if(c=='('||c=='[')  break;
 58         }
 59     }
 60     if(StackEmpty(S))
 61        cout<<"True"<<endl;
 62     else
 63         cout<<"False"<<endl;
 64     return 0;
 65 }
 66 bool InitStack(Stack&S){
 67     S.Base=(SElemtype*)malloc(INITSTACKSIZE*sizeof(SElemtype));
 68     if(!S.Base)
 69         exit(OverFlow);//程序异常终止
 70     S.Top=S.Base;
 71     S.Stacksize=INITSTACKSIZE;
 72     return true;
 73 }
 74 
 75 bool StackEmpty(Stack S){
 76     if(S.Top==S.Base)
 77         return true;
 78     else
 79         return false;
 80 }
 81 
 82 int StackLength(Stack S){
 83     int Length=0;
 84     SElemtype*index;
 85     for(index=S.Top;index!=S.Base;index--)
 86         Length++;
 87     return Length;
 88 }
 89 
 90 bool GetTop(Stack S,SElemtype&Elem){
 91     if(S.Top==S.Base)
 92        return false;
 93     else{
 94         Elem=*(S.Top-1);
 95         return true;
 96     }
 97 }
 98 
 99 bool Push(Stack&S,SElemtype Elem){
100     if(S.Top-S.Base>=S.Stacksize){
101         S.Base=(SElemtype*)realloc(S.Base,(S.Stacksize+STACKINCRESMENT)*sizeof(SElemtype));
102         if(!S.Base)
103             exit(OverFlow);
104         S.Top=S.Base+S.Stacksize;
105         S.Stacksize+=STACKINCRESMENT;
106     }
107     *S.Top++=Elem;
108     return true;
109 }
110 
111 bool Pop(Stack&S,SElemtype&Elem){
112     if(S.Base==S.Top)
113         return false;
114     else
115         Elem=*--S.Top;
116     return true;
117 }
118 
119 /****************************************
120     Author:CRUEL_KING
121     Time:2018/11/10
122     Program name:顺序栈实现括号匹配.cpp
123 ****************************************/

 2.STL之Stack

  stack是一种容器适配器(STL的容器分为顺序容器和关联容器,容器适配器,是对这两类容器进行包装得到的具有更强的约束力的容器),被设计来用于操作先进后出(FILO)结构的情景,在这种情况下, 元素的插入和删除都只能在容器的尾部进行。stack通过容器适配器来实现,是一种将特定的容器类作为其最底层的容器的类,它提供了一些特定的成员函数来访问自己的元素,元素只能在这个特定容器的后面,也就是栈的顶部,进行出栈和入栈操作。 

  构造:template <class T, class Container = deque<T> > class stack;

  如上,这对尖括号中有两个参数,第一个是T,表示栈中存放的数据的类型,比如int,double,或者结构体之类。

  第二个参数指明底层实现的容器类型,也就是指明这个栈的内部实现方式,比如vector,deque,list。如果不指明它,默认使用deque(双端队列)。

  栈的成员函数和基本操作:

    1.定义栈

1 stack<data_type>name;//如:stack<int>z;定义一个数据类型为data_type的栈name。

 

    2.复制栈

1 stack<data_type>name(name2);//如:stack<int>z(z2);将栈name2中的元素复制到一个新栈name中。

    3.入栈

1 name.push(variable);//如:z.push(a);将a压入栈z的顶端。

    4.出栈(栈不为空的情况下)

1 name.pop();//如:z.pop();弹出一个z栈的栈顶元素。

    5.查看栈顶元素(栈不为空的情况下)

1 variable=name.top();//如:k=z.top();将z栈的栈顶元素赋值给一个变量k。

    6.栈的元素个数

1 variable=name.size();//如:k=z.size();将z栈的元素个数赋值给一个变量k。

 

    7.检验栈是否为空

1 name.empty();//如:z.empty();检验栈是否为空,空返回为1,非空返回为0;

 

  例题详解(括号匹配)

 1 /*********************************************************
 2     STL_Stack栈实现括号匹配。
 3     main函数操作:
 4         1.在这里main函数内部主要以解决括号匹配问题。
 5         2.本例中包含"()""[]"{}"三种括号。
 6         3.输入一个表达式,如果括号匹配则输出True,否则输出False。
 7 **********************************************************/
 8 #include <cstdio>
 9 #include <cstring>
10 #include <stack>
11 #include <iostream>
12 using namespace std;
13 
14 int main()
15 {
16     stack <char>S;
17     char c;
18     string str;
19     cin>>str;
20     for(int i=0;i<str.length();i++){
21         if(str[i]=='('||str[i]=='['||str[i]=='{'){
22             S.push(str[i]);
23             continue;
24         }
25         if(str[i]==')'){
26             c=S.top();
27             if(c=='(')  S.pop();
28             if(c=='['||c=='{')  break;
29         }
30         if(str[i]==']'){
31             c=S.top();
32             if(c=='[')  S.pop();
33             if(c=='('||c=='{')  break;
34         }
35         if(str[i]=='}'){
36             c=S.top();
37             if(c=='{')  S.pop();
38             if(c=='('||c=='[')  break;
39         }
40     }
41     if(S.empty())
42        cout<<"True"<<endl;
43     else
44         cout<<"False"<<endl;
45     return 0;
46 }
47 
48 
49 /****************************************
50     Author:CRUEL_KING
51     Time:2018/11/11
52     Program name:STL栈实现括号匹配.cpp
53 ****************************************/

 

posted @ 2018-11-10 21:39  Cruel_King  阅读(922)  评论(0编辑  收藏  举报