栈和队列应用之括号匹配

      括号匹配是计算机程序设计经常遇到的问题。为了简化问题,假设表达式只允许有2种括号:(),【】。 匹配格式为:(()【】)和【()【()】【】】视为正确匹配。具体实现部分见下图代码:衷心希望网友提出改进意见,大家共同进步。

 1 SeqStack < char >  list = new  SeqStack < char > ( 10 );
 2            
 3              // 栈的使用:括号匹配
 4 ExpandedBlockStart.gifContractedBlock.gif              char [] str  =   new   char [] {'[',']','(',')'} ;
 5              int  len  =  str.Length;
 6              for  ( int  i  =   0 ; i  <  len; i ++ )
 7 ExpandedBlockStart.gifContractedBlock.gif            
 8               //如果栈为空,将括号入栈
 9                if (list.IsEmpty())
10ExpandedSubBlockStart.gifContractedSubBlock.gif                {
11                    list.Push(str[i]);
12                }

13                else   //如果括号与栈顶的括号匹配,将栈顶括号出栈
14ExpandedSubBlockStart.gifContractedSubBlock.gif                {
15                    if (((list.GetTop() == '('&& (str[i] == ')')) || ((list.GetTop() == '['&& (str[i] == ']')))
16ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
17                       
18                        Console.WriteLine(list.Pop());
19                    }

20                    else  //如果括号与栈顶的括号不匹配,将括号入栈
21ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
22                        list.Push(str[i]);
23                    }

24             
25                }

26        
27            }

28              // 如果括号序列为空,栈为空则括号匹配,否则不匹配
29              if  (list.IsEmpty())
30 ExpandedBlockStart.gifContractedBlock.gif             {
31                Console.WriteLine("匹配!");
32            }

33              else
34 ExpandedBlockStart.gifContractedBlock.gif             {
35                Console.WriteLine("不匹配!");
36            }

37  

 

完整代码如下:

 

  1 using  System;
  2 using  System.Collections.Generic;
  3 using  System.Text;
  4
  5 namespace  栈和队列
  6 ExpandedBlockStart.gifContractedBlock.gif {
  7    class Program
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    {
  9        public interface IStack<T>
 10ExpandedSubBlockStart.gifContractedSubBlock.gif        
 11           //求栈的长度
 12             int GetLength();
 13            //判断是否为空
 14            bool IsEmpty(); 
 15            //判断是否已满
 16            bool IsFull();
 17            //清空操作
 18            void Clear();
 19            //入栈
 20            void Push(T item);
 21            //出栈
 22            T Pop();
 23            //取栈顶元素
 24            T GetTop();
 25           
 26            
 27
 28        }

 29        public class SeqStack<T> : IStack<T>
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        
 31           //栈的容量
 32            private int maxsize;
 33           //存储栈中的数据
 34            private T[] data;
 35           //指定栈的栈顶
 36            private int top;
 37
 38            //索引器
 39            public T this[int index]
 40ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 41                get
 42ExpandedSubBlockStart.gifContractedSubBlock.gif                
 43                   return data[index];
 44                }

 45                set
 46ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 47                    data[index] = value;
 48                }

 49            }

 50
 51            //容量属性
 52            public int Maxsize
 53ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 54                get
 55ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 56                    return maxsize;
 57                }

 58                set
 59ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 60                    maxsize = value;
 61                }

 62            }

 63
 64            //栈顶属性
 65            public int Top
 66ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 67                get
 68ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 69                    return top;
 70                }

 71            }

 72
 73            //构造器
 74            public SeqStack(int size)
 75ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 76                data = new T[size];
 77                maxsize = size;
 78                top = -1;
 79            }

 80
 81            //求栈的长度
 82            public int GetLength()
 83ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 84                return top + 1;
 85            }

 86
 87            //清空栈
 88            public void Clear()
 89ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 90                top = -1;
 91            }

 92
 93            //判断是否为空
 94            public bool IsEmpty()
 95ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 96                if (top == -1)
 97ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 98                    return true;
 99                }

100                else
101ExpandedSubBlockStart.gifContractedSubBlock.gif                {
102                    return false;
103                }

104            }

105
106            //判断是否以满
107            public bool IsFull()
108ExpandedSubBlockStart.gifContractedSubBlock.gif            {
109                if (top == maxsize - 1)
110ExpandedSubBlockStart.gifContractedSubBlock.gif                {
111                    return true;
112                }

113                else
114ExpandedSubBlockStart.gifContractedSubBlock.gif                {
115                    return false;
116                }

117            }

118
119            //入栈
120            public void Push(T item)
121ExpandedSubBlockStart.gifContractedSubBlock.gif            {
122                if (IsFull())
123ExpandedSubBlockStart.gifContractedSubBlock.gif                {
124                    Console.WriteLine("栈满啦,要清空啦!");
125                    return;
126                }

127                data[++top] = item;
128                
129            }

130            //出栈
131            public T Pop()
132ExpandedSubBlockStart.gifContractedSubBlock.gif            
133               T tmp=default(T);
134               if (IsEmpty())
135ExpandedSubBlockStart.gifContractedSubBlock.gif               {
136                   Console.WriteLine("栈已空!");
137                   return tmp;
138               }

139               tmp = data[top];
140               --top;
141               return tmp;
142            }

143            //获取栈顶数据元素
144            public T GetTop()
145ExpandedSubBlockStart.gifContractedSubBlock.gif            {
146                if (IsEmpty())
147ExpandedSubBlockStart.gifContractedSubBlock.gif                {
148                    Console.WriteLine("表已空!");
149                    return default(T);
150                }

151                return data[top];
152            }

153            
154            
155
156        }

157
158        static void Main(string[] args)
159ExpandedSubBlockStart.gifContractedSubBlock.gif        {
160           
161            SeqStack<char> list=new SeqStack<char>(10);
162           
163            //栈的使用:括号匹配
164ExpandedSubBlockStart.gifContractedSubBlock.gif            char[] str = new char[]{'[',']','(',')'};
165            int len = str.Length;
166            for (int i = 0; i < len; i++)
167ExpandedSubBlockStart.gifContractedSubBlock.gif            
168               //如果栈为空,将括号入栈
169                if (list.IsEmpty())
170ExpandedSubBlockStart.gifContractedSubBlock.gif                {
171                    list.Push(str[i]);
172                }

173                else   //如果括号与栈顶的括号匹配,将栈顶括号出栈
174ExpandedSubBlockStart.gifContractedSubBlock.gif                {
175                    if (((list.GetTop() == '('&& (str[i] == ')')) || ((list.GetTop() == '['&& (str[i] == ']')))
176ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
177                       
178                        Console.WriteLine(list.Pop());
179                    }

180                    else  //如果括号与栈顶的括号不匹配,将括号入栈
181ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
182                        list.Push(str[i]);
183                    }

184             
185                }

186        
187            }

188            //如果括号序列为空,栈为空则括号匹配,否则不匹配
189            if (list.IsEmpty())
190ExpandedSubBlockStart.gifContractedSubBlock.gif            {
191                Console.WriteLine("匹配!");
192            }

193            else
194ExpandedSubBlockStart.gifContractedSubBlock.gif            {
195                Console.WriteLine("不匹配!");
196            }

197 
198          }

199
200        }

201    }

202
203

转载于:https://www.cnblogs.com/zhangq0309/archive/2009/03/10/1407696.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值