概述
先用C#实现一个栈的类,如下:
public class MyStack<T> : IEnumerable<T>, IDisposable
{
private int _top = 0;
private int _size = 0;
private T[] _stack = null;
public int Top {
get {
return _top;
}
}
public int Size {
get {
return _size;
}
}
public int Length {
get {
return _top;
}
}
public T this[int index] {
get {
return _stack[index];
}
}
public MyStack(int size) {
_size = size;
_top = 0;
_stack = new T[size];
}
public bool IsEmpty() {
return _top == 0;
}
public bool IsFull() {
return _top == _size;
}
public void Clear() {
_top = 0;
}
/// <summary>
/// 入栈
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public bool Push(T node) {
if (!IsFull()) {
_stack[_top] = node;
_top++;
return true;
}
return false;
}
/// <summary>
/// 出栈
/// </summary>
/// <returns></returns>
public T Pop() {
T node = default(T);
if (!IsEmpty()) {
_top--;
node = _stack[_top];
}
return node;
}
public void Traverse() {
for(int i = 0; i < _top; i++) {
Console.WriteLine(_stack[i]);
}
}
public IEnumerator<T> GetEnumerator() {
for(int i = 0; i < _stack.Length; i++) {
if(_stack[i] != null) {
yield return _stack[i];
}
}
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
public void Dispose() {
_stack = null;
}
}
上面要注意的是入栈时,是直接把值赋给栈顶,然后再将栈顶加1,出栈的时候,先将栈顶减一,再出栈,切记。
实例一:
用栈来实现进制转换,即十进制转化为2进制、8进制、16进制,代码如下:
/// <summary>
/// 十进制转换为其他进制
/// </summary>
/// <param name="number">要转换的数字</param>
/// <param name="target">目标进制</param>
/// <returns></returns>
public static string Process(int number, int target) {
string result = string.Empty;
string format = "0123456789ABCDEF";
MyStack<int> stack = new MyStack<int>(30);
int mod = 0;
while(number != 0) {
mod = number % target;
stack.Push(mod);
number = number / target;
}
while (!stack.IsEmpty()) {
int pos = stack.Pop();
result += format[pos];
}
return result;
}
实例二
用栈来检查一个字符串括号是否配对,代码如下:
/// <summary>
/// 检查字符串中括号是否成对匹配
/// </summary>
/// <param name="charter">待检查的字符串</param>
/// <returns></returns>
public static bool Check(string charter) {
bool result = false;
MyStack<char> stack = new MyStack<char>(30);
MyStack<char> needStack = new MyStack<char>(30);
char currentNeed = '0';
for (int i = 0; i < charter.Length; i++) {
if(charter[i] != currentNeed) {
char t = charter[i];
stack.Push(t);
switch (t) {
case '[':
if(currentNeed != '0') {
needStack.Push(currentNeed);
}
currentNeed = ']';
break;
case '(':
if(currentNeed != '0') {
needStack.Push(currentNeed);
}
currentNeed = ')';
break;
case '{':
if (currentNeed != '0') {
needStack.Push(currentNeed);
}
currentNeed = '}';
break;
}
}
else {
stack.Pop();
currentNeed = needStack.Pop();
}
}
if (stack.IsEmpty()) {
result = true;
}
return result;
}
总结:
希望能帮到你,望推荐!