栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【数据结构】C#代码实现【顺序栈】和【链栈】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【数据结构】C#代码实现【顺序栈】和【链栈】

第一、创建接口
包含了栈的所有方法,为统一命名,使用接口,在顺序栈和链栈的具体实现中就继承该接口,以此统一命名。

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

namespace case2_Stack
{
    interface IStack
    {
        int Count { get; }
        int GetLength();
        bool IsEmpty();

        //对栈操作为空类型方法
        void Clear();//对栈操作 清空
        void Push(T item);//对栈操作 入栈

        //需要返回元素,定义为元素类型方法
        T Pop();//对元素操作 出栈 读取
        T Peek();//对元素操作 读取
    }
}

第二、实现顺序栈

    class SeqStack : IStack
    {
        private T[] data;//存储数据
        private int top;//指针 指向栈顶

        //构造函数
        public SeqStack(int size)
        {//可以初始化时,指定栈的大小
            data = new T[size];
            top = -1;

        }
        public SeqStack() : this(10)
        {//如果不指定栈的大小,则默认栈的大小为10

        }



        public int Count
        {
            get { return top + 1; }
        }


        public void Clear()
        {
            top = -1;
        }

        public int GetLength()
        {
            return Count;
        }

        public bool IsEmpty()
        {
            return Count == 0;
        }

        public T Peek()
        {
            return data[top];
        }

        public T Pop()
        {
            T temp = data[top];
            top--;
            return temp;
        }

        public void Push(T item)
        {
            data[top + 1] = item;
            top++;
        }
    }

第三、定义链的结点类
链栈的结点包含两个元素,一是数据,二是指针。这里的指针并不是C语言意义上的指针,而是也是一个结点——下一个结点。可以理解为结点的结点、结点类中的结点型成员。

//链栈的结点
    class Node
    {
        private T data;
        private Node next;

        //构造函数
        public Node()
        {
            data = default(T);
            next = null;
        }
        public Node(T data)
        {
            this.data = data;
            next = null;
        }
        public Node(T data ,Node next)
        {
            this.data = data;
            this.next = next;
        }
        public Node(Node next)
        {
            this.next = next;
            data = default(T);
        }

        public T Data { get { return data; } set { data = value; } }
        public Node Next { get { return next; } set { next = value; } }

    }

第四、实现链栈
顺序栈中用数组存储数据,链栈中用结点的类。结点只需要定义一个结点即可,因为结点的存储数据方式是:结点的结点的结点的结点…

class LinkStack : IStack
    {
        private Node top;//栈顶元素结点
        private int count = 0;//栈中元素个数
        public int Count 
        {
            get { return count; }
        }
        public void Clear()
        {
            count = 0;
            top = null;
        }

        public int GetLength()
        {
            return count;
        }

        public bool IsEmpty()
        {
            return count == 0;
        }

        public T Peek()
        {
            return top.Data;
        }

        public T Pop()
        {
            T temp = top.Data;
            top= top.Next;
            count--;
            return temp;
        }

        public void Push(T item)
        {
            //把新添加的元素作为头结点(栈顶)
            Node newNode = new Node(item);
            newNode.Next = top;
            top = newNode;
            count++;
        }
    }

测试顺序栈和链栈,分别实例化

    class Program
    {
        static void Main(string[] args)
        {
            //1.使用BCL中的Stack
            //构造一个char型的栈
            //Stack temp = new Stack();
            //2.使用自己的栈
            //SeqStack temp = new SeqStack();
            //IStack temp = new SeqStack();//注意这个初始化方法!
            //3.使用链栈(当前使用)
            IStack temp = new LinkStack();

            temp.Push('a');
            temp.Push('b');
            temp.Push('c');
            Console.WriteLine("栈中数据个数为:" + temp.Count);

            
            Console.WriteLine("第一个出栈元素为" + temp.Pop());
            Console.WriteLine("第二个出栈元素为" + temp.Pop());
            Console.WriteLine("第三个出栈元素为" + temp.Pop());

            Console.ReadLine();
        }
    }

可以把上面所有的类和接口都放在一个源文件里;也可以各自放在一个源文件里,但是要放在一个项目/命名空间下。
结果一样且正确~不正确的话就要在顺序栈和链栈的具体实现中去调啦

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/836462.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号