03.队列
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了03.队列相关的知识,希望对你有一定的参考价值。
1 public class DecConvert 2 { 3 //N为需进行转换的数字,D为转换成的进制 4 public static string Convert(int N,int D) 5 { 6 if (D<2||D>16) 7 { 8 throw new ArgumentOutOfRangeException("D","只支持2、8、10、16进制的转换!"); 9 } 10 MyStack stack = new MyStack(); 11 do 12 { 13 //取余 14 int Residue = N % D; 15 char c=(Residue<10)?(char)(Residue+48):(char)(Residue+55); 16 stack.Push(c); 17 } while ((N=N/D)!=0);//当商为0时代表运算结束 18 string s = string.Empty; 19 20 while (stack.Count>0) 21 { 22 //弹出所有的元素 23 s += stack.Pop().ToString(); 24 } 25 return s; 26 } 27 }
1 public class MyStack 2 { 3 //用于存放栈元素 4 private object[] _array; 5 //默认的栈容量 6 private const int _defaultCapactity = 10; 7 private int _size; 8 9 10 public MyStack() 11 { 12 this._array=new object[_defaultCapactity]; 13 this._size = 0; 14 } 15 16 public MyStack(int initalCapacity) 17 { 18 if (initalCapacity<0) 19 { 20 throw new ArgumentOutOfRangeException("initalCapacity","栈空间容量不可为负"); 21 } 22 if (initalCapacity<_defaultCapactity) 23 { 24 initalCapacity = _defaultCapactity; 25 } 26 this._array=new object[initalCapacity];//分配栈空间 27 this._size = 0; 28 } 29 30 //出栈 31 public virtual object Pop() 32 { 33 if (this._size==0) 34 { 35 throw new InvalidOperationException("栈下溢"); 36 } 37 object obj = this._array[--this._size]; 38 this._array[this._size] = null; 39 return obj; 40 } 41 42 //进栈 43 public virtual void Push(object obj) 44 { 45 if (this._size==this._array.Length) 46 { 47 //栈扩容 48 object[] dest=new object[2*this._array.Length]; 49 Array.Copy(this._array, 0, dest, 0, this._size); 50 this._array = dest; 51 } 52 this._array[this._size++] = obj; 53 } 54 55 public virtual int Count 56 { 57 get { return this._size;} 58 } 59 60 //获得栈顶元素 61 public virtual object Peek() 62 { 63 object obj = this._array[this._size]; 64 return obj; 65 } 66 }
以上是关于03.队列的主要内容,如果未能解决你的问题,请参考以下文章