C#实现栈
概述
先用C#实现一个栈的类,如下:
1 public class MyStack<T> : IEnumerable<T>, IDisposable 2 { 3 private int _top = 0; 4 private int _size = 0; 5 private T[] _stack = null; 6 7 public int Top { 8 get { 9 return _top; 10 } 11 } 12 13 public int Size { 14 get { 15 return _size; 16 } 17 } 18 19 public int Length { 20 get { 21 return _top; 22 } 23 } 24 25 public T this[int index] { 26 get { 27 return _stack[index]; 28 } 29 } 30 31 public MyStack(int size) { 32 _size = size; 33 _top = 0; 34 _stack = new T[size]; 35 } 36 37 public bool IsEmpty() { 38 return _top == 0; 39 } 40 41 public bool IsFull() { 42 return _top == _size; 43 } 44 45 public void Clear() { 46 _top = 0; 47 } 48 49 /// <summary> 50 /// 入栈 51 /// </summary> 52 /// <param name="node"></param> 53 /// <returns></returns> 54 public bool Push(T node) { 55 if (!IsFull()) { 56 _stack[_top] = node; 57 _top++; 58 return true; 59 } 60 return false; 61 } 62 63 /// <summary> 64 /// 出栈 65 /// </summary> 66 /// <returns></returns> 67 public T Pop() { 68 T node = default(T); 69 if (!IsEmpty()) { 70 _top--; 71 node = _stack[_top]; 72 } 73 return node; 74 } 75 76 public void Traverse() { 77 for(int i = 0; i < _top; i++) { 78 Console.WriteLine(_stack[i]); 79 } 80 } 81 82 public IEnumerator<T> GetEnumerator() { 83 for(int i = 0; i < _stack.Length; i++) { 84 if(_stack[i] != null) { 85 yield return _stack[i]; 86 } 87 } 88 } 89 90 IEnumerator IEnumerable.GetEnumerator() { 91 return GetEnumerator(); 92 } 93 94 public void Dispose() { 95 _stack = null; 96 } 97 }
上面要注意的是入栈时,是直接把值赋给栈顶,然后再将栈顶加1,出栈的时候,先将栈顶减一,再出栈,切记。
实例一:
用栈来实现进制转换,即十进制转化为2进制、8进制、16进制,代码如下:
1 /// <summary> 2 /// 十进制转换为其他进制 3 /// </summary> 4 /// <param name="number">要转换的数字</param> 5 /// <param name="target">目标进制</param> 6 /// <returns></returns> 7 public static string Process(int number, int target) { 8 string result = string.Empty; 9 string format = "0123456789ABCDEF"; 10 MyStack<int> stack = new MyStack<int>(30); 11 int mod = 0; 12 while(number != 0) { 13 mod = number % target; 14 stack.Push(mod); 15 number = number / target; 16 } 17 while (!stack.IsEmpty()) { 18 int pos = stack.Pop(); 19 result += format[pos]; 20 } 21 return result; 22 }
测试代码:
1 string slt = MoronicConvert.Process(98758, 16); 2 Console.WriteLine(slt);
运行结果:
实例二
用栈来检查一个字符串括号是否配对,代码如下:
1 /// <summary> 2 /// 检查字符串中括号是否成对匹配 3 /// </summary> 4 /// <param name="charter">待检查的字符串</param> 5 /// <returns></returns> 6 public static bool Check(string charter) { 7 bool result = false; 8 MyStack<char> stack = new MyStack<char>(30); 9 MyStack<char> needStack = new MyStack<char>(30); 10 char currentNeed = '0'; 11 for (int i = 0; i < charter.Length; i++) { 12 if(charter[i] != currentNeed) { 13 char t = charter[i]; 14 stack.Push(t); 15 switch (t) { 16 case '[': 17 if(currentNeed != '0') { 18 needStack.Push(currentNeed); 19 } 20 currentNeed = ']'; 21 break; 22 case '(': 23 if(currentNeed != '0') { 24 needStack.Push(currentNeed); 25 } 26 currentNeed = ')'; 27 break; 28 case '{': 29 if (currentNeed != '0') { 30 needStack.Push(currentNeed); 31 } 32 currentNeed = '}'; 33 break; 34 } 35 } 36 else { 37 stack.Pop(); 38 currentNeed = needStack.Pop(); 39 } 40 } 41 if (stack.IsEmpty()) { 42 result = true; 43 } 44 return result; 45 }
测试代码:
1 bool check = SpellCheck.Check("[[([)]]]"); 2 Console.WriteLine(check.ToString());
运行结果:
总结:
希望能帮到你,望推荐!