八大数据结构-栈

  1. 使用栈计算后缀表达式
     1   public static void CalucatePostFix(string expression)
     2         {
     3             string[] opers = new string[] { "+", "-", "*", "/" };
     4             Stack<string> stack = new Stack<string>();
     5             string[] strs = expression.Split(' ');
     6             string result = string.Empty;
     7             foreach (string s in strs)
     8             {
     9                 if (opers.Contains(s))
    10                 {
    11                     double oper1 = Convert.ToDouble(stack.Pop());
    12                     double oper2 = Convert.ToDouble(stack.Pop());
    13                     double opresult = double.MinValue;
    14                     switch (s)
    15                     {
    16                         case "+":
    17                             opresult = oper2 + oper1;
    18                             break;
    19                         case "-":
    20                             opresult = oper2 - oper1;
    21                             break;
    22                         case "*":
    23                             opresult = oper2 * oper1;
    24                             break;
    25                         case "/":
    26                             opresult = oper2 / oper1;
    27                             break;
    28                         default:
    29                             break;
    30                     }
    31                     stack.Push(Convert.ToString(opresult));
    32                 }
    33                 else
    34                 {
    35                     stack.Push(s);
    36                 }
    37             }
    38             result = stack.Pop();
    39         }
    View Code
  2. 对栈中元素进行排序
     1     public static Stack<int> SortedStack(Stack<int> stack)
     2         {
     3             Stack<int> sortStack = new Stack<int>();
     4             while (stack.Count != 0)
     5             {
     6                 int value = stack.Pop();
     7                 if (sortStack.Count == 0 || value >= sortStack.Peek())
     8                 {
     9                     sortStack.Push(value);
    10                 }
    11                 else
    12                 {
    13                     while (sortStack.Count != 0 && value < sortStack.Peek())
    14                     {
    15                         stack.Push(sortStack.Pop());
    16                     }
    17                     sortStack.Push(value);
    18                 }
    19             }
    20             return sortStack;
    21         }
    View Code
  3. 判断表达式是否括号平衡
     1  public static bool BlanceParentheses(string exp)
     2         {
     3             bool Result = true;
     4             char[] leftparentheses = new char[] { '(', '[', '{' };
     5             char[] rightparentheses = new char[] { ')', ']', '}' };
     6             Stack<char> stack = new Stack<char>();
     7             foreach (char c in exp.ToCharArray())
     8             {
     9                 if (leftparentheses.Contains(c))
    10                 {
    11                     stack.Push(c);
    12                 }
    13                 if (rightparentheses.Contains(c))
    14                 {
    15                     char value = stack.Pop();
    16                     switch (c)
    17                     {
    18                         case ')':
    19                             {
    20                                 if (value != '(') Result = false;
    21                             }
    22                             break;
    23                         case ']':
    24                             {
    25                                 if (value != '[') Result = false;
    26                             }
    27                             break;
    28                         case '}':
    29                             {
    30                                 if (value != '{') Result = false;
    31                             }
    32                             break;
    33                     }
    34                 }
    35             }
    36             if (stack.Count != 0)
    37             {
    38                 Result = false;
    39             }
    40             return Result;
    41         }
    View Code

     

posted @ 2019-09-26 09:05  sky&&dan  阅读(83)  评论(0)    收藏  举报