美文网首页
顺序栈两栈共享及实现

顺序栈两栈共享及实现

作者: 周末的游戏之旅 | 来源:发表于2019-08-02 15:34 被阅读0次

两栈共享

顺序栈的两栈共享是指两个栈共享一个数组。


两个栈的栈底分别为0和length-1。
当top[0]+1=top[1]时,即为栈满。

实现

cstack

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

namespace TwoShare
{
    class cstack<T>
    {
        int[] top = new int[2];
        T[] data;

        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="length"></param>
        public cstack(int length)
        {
            data = new T[length];
            top[0] = -1;
            top[1] = length;
        }

        /// <summary>
        /// 入栈
        /// </summary>
        /// <param name="data"></param>
        /// <param name="i"></param>
        public bool Push(T data, int i)
        {
            if (top[0] + 1 == top[1]) return false;

            switch (i)
            {
                case 0:
                    ++top[0];
                    this.data[top[0]] = data;
                    return true;
                case 1:
                    --top[1];
                    this.data[top[1]] = data;
                    return true;
                default:
                    return false;
            }
        }


        /// <summary>
        /// 出栈
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public T Pop(int i)
        {
            T d;
            switch (i)
            {
                case 0:
                    if (top[0] == -1) return default(T);
                    d = this.data[top[0]];
                    --top[0];
                    return d;
                case 1:
                    if (top[1] == this.data.Length) return default(T);
                    d = this.data[top[1]];
                    ++top[1];
                    return d;
                default:
                    return default(T);
            }
        }

        /// <summary>
        /// 读栈顶
        /// </summary>
        /// <returns></returns>
        public T ReadTop(int i)
        {
            switch (i)
            {
                case 0:
                    if (top[0] == -1) return default(T);
                    return this.data[top[0]];
                case 1:
                    if (top[1] == this.data.Length) return default(T);
                    return this.data[top[1]];
                default:
                    return default(T);
            }
        }
    }
}

Program

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

namespace TwoShare
{
    class Program
    {
        static void Main(string[] args)
        {
            cstack<string> cstack = new cstack<string>(5);
            cstack.Push("a", 0);
            cstack.Push("b", 0);
            cstack.Push("c", 0);
            cstack.Push("e", 1);
            cstack.Push("f", 1);

            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine(cstack.Pop(0));
            }

            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine(cstack.Pop(1));
            }
        }
    }
}

相关文章

  • 顺序栈两栈共享及实现

    两栈共享 顺序栈的两栈共享是指两个栈共享一个数组。 两个栈的栈底分别为0和length-1。当top[0]+1=t...

  • 顺序存储结构栈 共享栈 链式存储结构栈

  • 数据结构的各种代码

    第 02 章 线性表 顺序存储结构 链式存储结构 第 03 章 栈与队列 顺序栈 链栈 两栈共享空间 循环队列 链...

  • 数据结构基础--顺序栈

    顺序栈的概念:顺序栈是栈的顺序实现。顺序栈是指利用顺序存储结构实现的栈。采用地址连续的存储空间(数组)依次存储栈中...

  • 线性表 - 栈与队列

    1.栈 1.1 顺序栈 1.2 两栈共享空间 栈的顺序存储还是很方便的,因为它只允许栈顶进出元素,所以不存在线性表...

  • 顺序存储/链式存储设计栈结构

    一、顺序存储1.1 定义常量及结构 1.2 栈方法实现 二、链式存储2.1 定义常量及结构 2.2 栈方法实现

  • 共享栈

    利用栈底为止相对不变的特性,可让两个顺序栈共享一个一维数组空间,将两个栈的栈底分别设置在共享空间的两端,两个栈顶向...

  • C语言实现链栈以及基本操作

    链栈,即用链表实现栈存储结构。链栈的实现思路同顺序栈类似,顺序栈是将数顺序表(数组)的一端作为栈底,另一端为栈顶;...

  • 2018-07-09顺序表实现栈

    栈的实现 ——直接用顺序表(列表list)进行 栈结构实现 栈可以用顺序表实现,也可以用链表实现。 栈的操作 St...

  • 3. 栈的操作

    1. 栈的操作-c语言实现2. 栈操作的实现-顺序栈和链栈 3. 栈的实现与遍历4. c语言的函数调用栈5. 两个...

网友评论

      本文标题:顺序栈两栈共享及实现

      本文链接:https://www.haomeiwen.com/subject/rhbldctx.html