脚踏实地-算法与数据结构-栈和队列

/*
  栈和队列是线性表的一种特殊形式,但是应用的时候较多。这两种结构自身的非常简单,但要想灵活应用,还是需要对其了解透彻。这篇文档不会再像上面的文档一样
有过多的叙述,基本上都在上篇文档介绍过了,如果需要请看上面的文档。
*/
View Code
/*
下面是一个用顺序表实现的循环队列,想着挺难,实际上的定义和操作都是挺简单的
*/
View Code
public int [] i;
        public SXqueue(int value)
        {
            i= new int[value];
            length = 0;
            Maxlength = value;
        }
        public int front;
        public int rear;
        public int length;

        public void insert(int value)
        {
            if (length == Maxlength )
            {
               
            }
            i[rear] = value;
            rear = (rear + 1) % Maxlength;
            length++;
        }

        public void outque()
        {
            i[front] = 0;//表示出栈吧
            front = (front + 1) % Maxlength;
            length--;
        }

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 源代码
{
    /// <summary>
    /// 我们所说的栈和队列,简单说就是链表的一种特定的表现形式,栈的定义是
    /// 先进后出,队列的定义是,先进先出,后面我们还会接触到一些双端队列,
    /// 循环队列等等哪些也只是更加特殊的形式,为了某种任务而被发明的。
    /// 所以不要感觉这些东西有多难,自己理解深层次的思想了,然后慢慢尝试
    /// 就一定能写出点实例代码来,我的代码没有调试,大家自己看一下。
    /// </summary>
    /// 直接写链式的了,不在写顺序存储结构形式的代码了
    class StackNode
    {
        //用一个int值来表示我的节点的值,当然最好用范式,那样更加通用一些
        public int value;
        public StackNode(int value)
        {
            this.value = value;
        }
        //指向和自己相邻的节点,用的是双向列表表示栈,你也可以用单项的
        public StackNode next;
        public StackNode pre;
    }

    /// <summary>
    /// 现在开始定义栈的基本操作
    /// </summary>
    class OperateStack
    {
        //栈有两个指针,一个值栈底指针,连一个指示栈顶,用于插入,删除,修改等操作
        //current 指针用于后面的一些操做,可省略
        public StackNode bottom;
        public StackNode top;
        public StackNode current;
        public int length;
        public OperateStack()
        {
            length = 0;
        }
        /// <summary>
        /// 像Stack中插入数据,这和链表差不多,区别是栈只能在栈头出进出
        /// 并且严格执行先进后出的策略
        /// </summary>
        /// <param name="value"></param>
        public void pushInStack(int value)
        {
            StackNode s = new StackNode(value);
            if (IsNull())
            {
                bottom = s;
                top = s;
                length++;
            }
                //加不加else都行  为了效率你看着办吧
            else 
            {
                top.next = s;
                s.pre = top;
                top = s;
                length++;
            }
        }
        public Boolean IsNull()//or  top==bottom
        {
            if (length == 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 出栈操作,把顶部的元素那出去用,至于干什么 你自己看着吧
        /// </summary>
        public StackNode  pushOutStack()
        {
            //仔细想象这里
            StackNode current = top;
            //top.pre = top.pre.pre;不必管相互之间的关系
            top = top.pre;
            length--;
            return current;
        }

        //栈中要是插入数据的话,那就坏了,和链表一样了


        /// <summary>
        /// 现在做的是个队列中插入数据的方法,队列和栈的区别也就是一点,队列
        /// 是先进先出,后进后出的操作方式,直接使用上面的节点以及方法即可
        /// </summary>
        public void PushInSeq(int value)
        {
            StackNode s = new StackNode(value);

            if (IsNull())
            {
                top = s;
                bottom = s;//表示队列的尾部
                length++;
            }
            else 
            {
                bottom.next = s;
                s.pre = bottom;
                bottom = s;
                length++;
            }
        }
        /// <summary>
        /// 在顶一个一个出队列的方法。将元素,从头结点移除
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public StackNode PushOutSeq(int value)
        {
            if (IsNull())
            {
                Console.WriteLine("");
                return null ;
            }
            else
            {
                StackNode current = top;
                //top .next .next .pre 
                top = top.next;
                length--;
                return current ;
            }
        }
    }
}
posted @ 2012-09-26 12:31  忧悦  阅读(177)  评论(0)    收藏  举报