c# 实现栈

简要介绍

  1. 栈是限制线性表中元素的插入和删除只能在线性表的同一端进行的一种特殊线性表。
  2. 允许插入和删除的一端称为栈顶
  3. 另一端为固定的一端,称作栈底
  4. 遵循先入后出(FILO)的原则

图解

在这里插入图片描述

内部成员

[注]本例使用数组存储数据,链表实现栈代码在最后

修饰符变量名含义
private inttop指向栈顶元素
private intmaxSize数组最大存储空间
private int[]datas存储数据的数组
public boolIsFull栈是否已满
public boolIsEmpty栈是否为空
public voidPush(int value)向栈顶压入数据
public intPop()弹出栈顶数据
public intPeek()查看栈顶数据,但不弹出

成员代码

构造器

 public ArrayStack(int maxSize)
 {
     this.maxSize = maxSize;
     datas = new int[this.maxSize];
 }

IsFull

当 top与maxSize 相等时代表已经存满

public bool IsFull { get => top == maxSize - 1; }

IsEmpty

当top 等于-1 时代表栈为空

public bool IsEmpty { get => top == -1; }

Push(int value)

 public void Push(int value)
 {
     //判断栈是否满
     if (IsFull)
     {
         Console.WriteLine("栈满");
         return;
     }
     datas[++top] = value;
 }

Pop()

 public int Pop()
 {
     if (IsEmpty)
     {
         throw new Exception("栈空");
     }

     return datas[top--];
 }

Peek()

 public int Peek()
 {
     if (IsEmpty)
     {
         throw new Exception("栈空");
     }
     return datas[top];
 }

完整代码

class ArrayStack
{
    private int maxSize;    //栈的大小
    private int[] datas;    //存储数据在数组中
    private int top = -1;

    public bool IsFull { get => top == maxSize - 1; }
    public bool IsEmpty { get => top == -1; }
    public ArrayStack(int maxSize)
    {
        this.maxSize = maxSize;
        datas = new int[this.maxSize];
    }

    

    public void Push(int value)
    {
        //判断栈是否满
        if (IsFull)
        {
            Console.WriteLine("栈满");
            return;
        }
        datas[++top] = value;
    }

    public int Pop()
    {
        if (IsEmpty)
        {
            throw new Exception("栈空");
        }

        return datas[top--];
    }

    public int Peek()
    {
        if (IsEmpty)
        {
            throw new Exception("栈空");
        }
        return datas[top];
    }

    public void Traverse()
    {
        if (IsEmpty)
        {
            Console.WriteLine("栈空");
            return;
        }
        for (int i = top; i >= 0; i--)
        {
            Console.WriteLine($"stack[{i}] = {datas[i]}");

        }
    }
}

补充(链表实现栈)

尾插法单链表实现栈

using System;
using System.Reflection.Metadata.Ecma335;

namespace StackByLinkedList
{
    class Program
    {
        static void Main(string[] args)
        {
            
        }
    }

    class Node
    {
        public int Data { get; set; }
        public Node Next { get; set; }

        public Node(int iniData)
        {
            this.Data = iniData;
        }
    }

    class SingleLinkedList
    {
        public Node Head { get; }
        public Node Last { get; set; }
        public SingleLinkedList()
        {
            Head = new Node(-1);
        }

        public void Add(Node newNode)
        {
            var temp = Head;
            while (true)
            {
                if (temp.Next == null)
                {
                    break;
                }
                temp = temp.Next;
            }
            temp.Next = newNode;
            Last = newNode;
        }

        public void Traverse()
        {
            if (Head.Next == null)
            {
                Console.WriteLine("链表为空!");
                return;
            }
            var temp = Head;
            while (true)
            {

                if (temp.Next == null)
                {
                    break;
                }
                Console.WriteLine($"[Data: {temp.Next.Data}]");
                temp = temp.Next;
            }
        }

        public Node Remove()
        {
            if (Head.Next == null)
            {
                return null;
            }

            var temp = Head;
            while (temp.Next.Next != null)
            {
                temp = temp.Next;
            }
            var res = temp.Next;
            temp.Next = null;
            return res;
        }


    }

    class StackByLinkedList
    {
        private int top = -1;
        private SingleLinkedList list;

        public int Count { get => top + 1; }
        public bool IsEmpty { get => top == -1; }

        public StackByLinkedList()
        {
            list = new SingleLinkedList();
        }

        public void Push(int value)
        {
            list.Add(new Node(value));
            top++;
        }

        public int Pop()
        {
            if (IsEmpty)
            {
                throw new Exception("栈空!");
            }

            var n = list.Remove();
            top--;
            return n.Data;
        }

        public int Peek()
        {
            if (IsEmpty)
            {
                throw new Exception("栈空!");
            }
            return list.Last.Data;
        }
    }
}

头插法单链表实现栈

using System;
using System.Drawing;

namespace StackByLinkedList2
{
    class Program
    {
        static void Main(string[] args)
        {
            StackByLinkedList stack = new StackByLinkedList();
            for (int i = 0; i < 20; i++)
            {
                stack.Push(i);
            }

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

    class Node
    {
        public int Data { get; set; }
        public Node Next { get; set; }

        public Node(int data)
        {
            this.Data = data;
        }
    }

    class SingleLinkedList
    {
        public Node Head { get; }

        public SingleLinkedList()
        {
            this.Head = new Node(-1);
        }


        public void Add(Node newNode)
        {
            var temp = Head.Next;
            newNode.Next = temp;
            Head.Next = newNode;
        }

        public Node Remove()
        {
            if (Head.Next == null)
            {
                throw new Exception("链表为空");
            }
            var r = Head.Next;
            Head.Next = r.Next;
            return r;
        }

        public void Traverse()
        {
            if (Head.Next == null)
            {
                Console.WriteLine("链表为空");
                return;
            }
            var temp = Head.Next;
            while (temp != null)
            {
                Console.WriteLine($"[Data : {temp.Data}]");
                temp = temp.Next;
            }
        }
    }

    class StackByLinkedList
    {
        private int top = -1;
        private SingleLinkedList datas = new SingleLinkedList();

        public int Count { get => top + 1; }
        public bool IsEmpty { get => top == -1; }

        public void Push(int data)
        {
            Node node = new Node(data);
            datas.Add(node);
            top++;
        }

        public int Pop()
        {
            if (IsEmpty)
            {
                throw new Exception("栈空");
            }
            top--;
            return datas.Remove().Data;
        }

        public int Peek()
        {
            if (IsEmpty)
            {
                throw new Exception("栈空");
            }
            return datas.Head.Next.Data;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值