• 栈和队列


    栈;后进先出的线性表

    进栈,出栈


     

    View Code

     

    两栈共享空间


    分别以数组的顶端和末端为栈底,向中间延伸

     /// <summary>
            /// 两栈共享-进栈
            /// </summary>
            public void DPush(dstack s, int data,int stackNo)
            {
                //栈满
                if (s.top1 + 1 == s.top2)
                {
                    return;
                }
    
                //从栈1进栈
                if (stackNo == 1)
                {
                    s.data[++s.top1] = data;
                }
                //从栈2进栈
                else if (stackNo == 2)
                {
                    s.data[--s.top2] = data;
                }
            }
    
            /// <summary>
            /// 两栈共享-出栈
            /// </summary>
            public void DPop(dstack s, int data, int stackNo)
            {
                if (stackNo == 1)
                {
                    //栈1满
                    if (s.top1 == -1)
                    {
                        return;
                    }
    
                    s.top1--;
                }
                else if (stackNo == 2)
                {
                    //栈2满
                    if (s.top2 == s.size)
                    {
                        return;
                    }
                    s.top2++;
                }
            }
    View Code

     

    两栈共享空间的适用场景:当两个栈的空间需求具有相反关系时,比如买卖股票,有人买入也有人卖出.

     

    栈的链式存储


     进栈:

    更新栈顶元素

    出栈:

     

     /// <summary>
            /// 链栈进栈
            /// </summary>
            /// <param name="ls"></param>
            /// <param name="data"></param>
            public void LinkStackPush(linkstack ls,int data)
            {
                linkstackNode ln = new linkstackNode();
                ln.data = data;
    
                //把当前栈顶元素赋值给新节点的后继
                ln.next = ls.top;
    
                //更新栈顶元素
                ls.top = ln;
    
                ls.count++;            
            }
    
            /// <summary>
            /// 链栈出栈
            /// </summary>
            /// <param name="ls"></param>
            /// <param name="data"></param>
            public void LinkStackPop(linkstack ls)
            {
                linkstackNode p;
                if (ls==null)
                {
                    return;
                }
                p = ls.top;
    
                //要删除的数据
                int data = ls.top.data;
    
                //下移一位
                ls.top = ls.top.next;
                ls.count--;
    
            }
    
     public class linkstackNode
        {
            public dynamic data;
            public linkstackNode next;
        }
    
        public class linkstack
        {
            //栈顶指针
            public linkstackNode top;
            public int count;
        }
    View Code

       

    队列:先进先出的线性表


     1.顺序存储

    循环队列:将队列收尾相连, 解决了队列的假溢出问题.

     入队,出队

     /// <summary>
            /// 循环队列入队操作
            /// </summary>
            public void EnQueue(queue q,int data)
            {
                //队列已满
                if ((q.rear+1)%q.size==q.front)
                {
                    return;
                }
    
                q.data[q.rear] = data;
                q.rear = (q.rear + 1) % q.size;
            }
    
            /// <summary>
            /// 循环队列出队
            /// </summary>
            /// <param name="q"></param>
            public void DeQueue(queue q)
            {
                //队列为空
                if (q.front==q.rear)
                {
                    return;
                }
    
                //出队的数据
                int data = q.data[q.front];
    
                q.front = (q.front + 1) % q.size;
            }
    View Code

    2.链式存储

     入队:

     

    出队:

     /// <summary>
            /// 链队入队
            /// </summary>
            public void EnLQueue(QueueList q,int data)
            {
                QueueNode n = new QueueNode();
                n.data = data;
                n.next = null;
                q.rear.next = n;
                q.rear = n;
            }
    
            public void DeLQueue(QueueList q)
            {
                //空队列
                if (q.front == q.rear)
                {
                    return;
                }
                //要删除的节点
                QueueNode n = q.front.next;
                int data = (int)n.data;
                q.front.next = n.next;
    
                //若队头是队尾,则删除后将rear指向头结点
                if (q.rear == n)
                {
                    q.rear = q.front;
                }
    
            }
    View Code
  • 相关阅读:
    spring整合activemq发送MQ消息[Topic模式]实例
    Activemq消息持久化
    Activemq消息类型
    spring整合activemq发送MQ消息[queue模式]实例
    activemq安装与简单消息发送接收实例
    metaq安装实例
    持续集成工具Hudson安装实例
    nexus安装实例
    sonar的安装与代码质量检测实例
    dubbo发布web服务实例
  • 原文地址:https://www.cnblogs.com/Linky008/p/8059477.html
Copyright © 2020-2023  润新知