1. stack

 

class Stack

{

    int maxsize;        //顺序栈的容量
    object[] data;      //数组,用于存储栈中的数据
    int top;            //指示栈顶
 

    public object this[int index]

    {

        get{return data[index];}

        set{ data[index] = value;}

    }

    //栈容量属性
    public int Maxsize

    {

        get

        {

            return maxsize;

        }

        set

        {

            maxsize = value;

        }

    }

    //获得栈顶的属性
    public int Top

    {

        get

        {

            return top;

        }

    }

    //使用构造器初始化栈
    public Stack(int size)

    {

        data = new object[size];

        maxsize = size;

        top = -1;

    }

    //求栈的长度(栈中的元素个数)
    public int StackLength()

    {

        return top+1;

    }

    //清空顺序栈
    public void ClearStack()

    {

        top = -1;

    }

    //判断顺序栈是否为空
    public bool IsEmpty()

    {

        if (top == -1)

        {

            return true;

        }

        else

        {

            return false;

        }

    }

    //判断顺序栈是否为满
    public bool IsFull()

    {

        if (top == maxsize-1)

        {

            return true;

        }

        else

        {

            return false;

        }

    }

    //入栈操作
    public void Push(object e)

    {

        if(IsFull())

        {

            Console.WriteLine("栈已满!");

            return;

        }

        data[++top] = e;

    }

    //出栈操作,并返回出栈的元素
    public object Pop()

    {

        object temp = null;

        if (IsEmpty())

        {

            Console.WriteLine("栈为空!");

            return temp;

        }

        temp = data[top];

        top --;

        return temp;

    }

    //获取栈顶数据元素
    public object GetTop()

    {

        if (IsEmpty())

        {

            Console.WriteLine("栈为空!");

            return null;

        }

        return data[top];

    }

}

栈的应用:

 

 
将一个非负的十进制整数N转换为另一个等价的基为B的B进制数的问题,很容易通过"除B取余法"来解决。
  【例】将十进制数13转化为二进制数。
   
 解答:按除2取余法,得到的余数依次是1、0、1、1,则十进制数转化为二进制数为1101。
   
 分析:由于最先得到的余数是转化结果的最低位,最后得到的余数是转化结果的最高位,因此很容易用栈来解决。

 

 class Program
    {
        static void Main(string[] args)
        {          
            MultiBaseOutput(132);
        }

        static void MultiBaseOutput(int N, int B)
        {
            //假设N是非负的十进制整数,输出等值的B进制数           

            Stack a = new Stack(4);

            while (N != 0)
            {
                a.Push(N % B);
                N = N / B;
            }

            while (!a.IsEmpty())
            {
                // a.Pop();
                Console.WriteLine(a.Pop().ToString());
            }
        }
    }

 2. 循环队列

 

{
        object[] data;  //数据元素
        int maxsize;    //最大容量
        int front = 0;      //指向队头
        int rear = 0;       //指向队尾
        int count = 0;


        //初始化队列
        public LoopQueue(int size)
        {
            this.maxsize = size;

            data = new object[size];

            front = rear = -1;
        }

        //最大容量属性
        public int MaxSize
        {

            get
            {

                return this.maxsize;

            }

            set
            {

                this.maxsize = value;

            }

        }

        //队尾属性
        public int Rear
        {

            get
            {

                return this.rear;

            }

        }

        //队头属性
        public int Front
        {

            get
            {

                return this.front;

            }

        }

        //数据属性
        public object this[int index]
        {

            get
            {

                return data[index];

            }

        }

        //获得队列的长度
        public int GetQueueLength()
        {
            return count;
        }

        //判断队列是否满
        public bool IsFull()
        {
            return count == maxsize;
        }

        //判断队列是否为空
        public bool IsEmpty()
        {
            return count == 0;
        }

        //清空队列
        public void ClearQueue()
        {
            rear = front = -1;
        }

        //入队
        public void Enqueue(object e)
        {
            if (IsFull())
            {
                throw new Exception("Queue is full");
            }

            count = count + 1;
            rear = (rear + 1) % MaxSize;
            data[rear] = e;
        }

        //出队
        public object DeQueue()
        {
            if (IsEmpty())
            {
                throw new Exception("Queue is Empty");               
            }

            count = count - 1;
            front = (front + 1) % maxsize;

            object tmp = data[front];
            return tmp;
        }  

      

        //获得队头元素
        public object GetHead()
        {
            if (IsEmpty())
            {
                Console.WriteLine("队列为空!");
                return null;
            }

            return data[front + 1];
        }

    }

队列的应用:

 

  private static void LoopQueueSample()
        {
            LoopQueue test = new LoopQueue(3);
            test.Enqueue(1);
            test.Enqueue(2);
            test.Enqueue(3);
            Console.WriteLine(test.DeQueue());
            Console.WriteLine(test.DeQueue());
            Console.WriteLine(test.DeQueue());
            test.Enqueue(4);
            test.Enqueue(5);
            Console.WriteLine(test.DeQueue());
            Console.WriteLine(test.DeQueue());
        } 

 

posted on 2012-04-25 13:46  higirle  阅读(250)  评论(0)    收藏  举报