C#中的栈和队列

什么是栈

栈和队列是非常重要的两种数据结构,在软件设计中应用很多。栈和队列也是线性结构,线性表、栈和队列这三种数据结构的数据元素以及数据元素间的逻辑关系完全相同,差别是线性表的操作不受限制,而栈和队列的操作受到眼制。栈的操作只能在表的一端进行,队列的插入操作在表的一端进行而其它操作在表的另一端进行,所以,把栈和队列称为操作受限的线性表。

BCL中的栈

使用原本有的栈

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 栈
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Stack<char> stack = new Stack<char>();
            stack.Push('a');
            stack.Push('b');
            stack.Push('c');//栈顶数据
            Console.WriteLine("push a b c之后的数据个数为" + stack.Count);
            char temp = stack.Pop();//去的栈顶的数据,并把栈顶的数据删除
            Console.WriteLine("pop 之后的数据是:"+temp);
            Console.WriteLine("pop 之后栈中数据的个数"+stack.Count);
            char temp2= stack.Peek();//取出栈顶数据,不删除
            Console.WriteLine("peek 之后栈中数据的个数" + stack.Count);
            stack.Clear();
            Console.WriteLine("clear之后栈中数据的个数" + stack.Count);
            Console.WriteLine("空栈的时候,去的栈顶的值为"+stack.Peek());//出现异常
            //空栈的时候不要进行peek和pop操作,否则会出现异常
            Console.ReadKey();
        }
    }
}
​

实现顺序栈

自定义顺序栈
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 栈
{
    internal interface IStackDS<T>
    {
         int Count { get; }//get data number
        int Getlenth();
        bool IsEmpty();
        void Clear();
        void Push(T item);
        T Pop();
        T Peek();
​
    }
}
​
​
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 栈
{
    internal class SeqStack<T> : IStackDS<T>
    {
        private T[] data;
​
        private int top;
​
        public SeqStack(int size)
        {
            data = new T[size];
            top = -1;
        }
        public SeqStack():this(10)
        {
​
        }
        public int Count
        {
            get { return top + 1; }
        }
​
        public void Clear()
        {
            top = -1;
        }
​
        public int Getlenth()
        {
            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++;
        }
    }
}
​
​
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​
namespace 栈
{
    internal class Program
    {
        static void Main(string[] args)
        {
            SeqStack<char> stack = new SeqStack<char>();
            stack.Push('a');
            stack.Push('b');
            stack.Push('c');//栈顶数据
            Console.WriteLine("push a b c之后的数据个数为" + stack.Count);
            char temp = stack.Pop();//去的栈顶的数据,并把栈顶的数据删除
            Console.WriteLine("pop 之后的数据是:"+temp);
            Console.WriteLine("pop 之后栈中数据的个数"+stack.Count);
            char temp2= stack.Peek();//取出栈顶数据,不删除
            Console.WriteLine("peek 之后栈中数据的个数" + stack.Count);
            stack.Clear();
            Console.WriteLine("clear之后栈中数据的个数" + stack.Count);
            Console.WriteLine("空栈的时候,去的栈顶的值为"+stack.Peek());//出现异常
            //空栈的时候不要进行peek和pop操作,否则会出现异常
            Console.ReadKey();
        }
    }
}
​

链栈

public class Node<T>
{
    public T Data { get; set; }
    public Node<T> Next { get; set; }
​
    public Node(T data)
    {
        Data = data;
        Next = null;
    }
}
​
public class LinkedStack<T>
{
    private Node<T> top;
​
    public LinkedStack()
    {
        top = null;
    }
​
    // 判断栈是否为空
    public bool IsEmpty()
    {
        return top == null;
    }
​
    // 入栈操作
    public void Push(T data)
    {
        Node<T> newNode = new Node<T>(data);
        newNode.Next = top;
        top = newNode;
    }
​
    // 出栈操作
    public T Pop()
    {
        if (IsEmpty())
        {
            throw new InvalidOperationException("栈为空,无法执行出栈操作。");
        }
        T data = top.Data;
        top = top.Next;
        return data;
    }
​
    // 查看栈顶元素
    public T Peek()
    {
        if (IsEmpty())
        {
            throw new InvalidOperationException("栈为空,无法查看栈顶元素。");
        }
        return top.Data;
    }
​
    // 清空栈
    public void Clear()
    {
        top = null;
    }
}
​
// 使用示例
public class Program
{
    public static void Main(string[] args)
    {
        LinkedStack<int> stack = new LinkedStack<int>();
​
        stack.Push(1);
        stack.Push(2);
        stack.Push(3);
​
        Console.WriteLine(stack.Pop());  // 输出 3
        Console.WriteLine(stack.Peek()); // 输出 2
​
        stack.Push(4);
        Console.WriteLine(stack.Pop());  // 输出 4
​
        while (!stack.IsEmpty())
        {
            Console.WriteLine(stack.Pop());
        }
    }
}

什么是队列

队列(Queue)是一种遵循先进先出(FIFO,First In First Out)原则的抽象数据类型。在队列中,元素的添加(入队)总是在一端进行,称为队尾(Rear),而元素的移除(出队)总是在另一端进行,称为队首(Front)。

顺序队列

using System;
​
public class SequentialQueue<T>
{
    private T[] queue;
    private int head;
    private int tail;
    private int count;
    private int capacity;
​
    public SequentialQueue(int capacity)
    {
        this.capacity = capacity;
        queue = new T[capacity];
        head = 0;
        tail = 0;
        count = 0;
    }
​
    // 判断队列是否为空
    public bool IsEmpty()
    {
        return count == 0;
    }
​
    // 判断队列是否已满
    public bool IsFull()
    {
        return count == capacity;
    }
​
    // 入队操作
    public void Enqueue(T item)
    {
        if (IsFull())
        {
            throw new InvalidOperationException("队列已满,无法入队。");
        }
        queue[tail] = item;
        tail = (tail + 1) % capacity;
        count++;
    }
​
    // 出队操作
    public T Dequeue()
    {
        if (IsEmpty())
        {
            throw new InvalidOperationException("队列为空,无法出队。");
        }
        T item = queue[head];
        queue[head] = default(T); // 清除队首元素
        head = (head + 1) % capacity;
        count--;
        return item;
    }
​
    // 查看队首元素
    public T Peek()
    {
        if (IsEmpty())
        {
            throw new InvalidOperationException("队列为空,无法查看队首元素。");
        }
        return queue[head];
    }
​
    // 获取队列的当前元素数量
    public int Count()
    {
        return count;
    }
}
​
// 使用示例
public class Program
{
    public static void Main(string[] args)
    {
        SequentialQueue<int> queue = new SequentialQueue<int>(5);
​
        queue.Enqueue(1);
        queue.Enqueue(2);
        queue.Enqueue(3);
​
        Console.WriteLine(queue.Dequeue());  // 输出 1
        Console.WriteLine(queue.Peek());     // 输出 2
​
        queue.Enqueue(4);
        queue.Enqueue(5);
        queue.Enqueue(6);
​
        while (!queue.IsEmpty())
        {
            Console.WriteLine(queue.Dequeue());
        }
    }
}

链队列

相关推荐

  1. c#队列

    2024-07-21 14:32:06       44 阅读
  2. C++面试:stl队列介绍

    2024-07-21 14:32:06       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 14:32:06       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 14:32:06       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 14:32:06       45 阅读
  4. Python语言-面向对象

    2024-07-21 14:32:06       55 阅读

热门阅读

  1. 设计App的后端接口分类以及环境依赖包详情

    2024-07-21 14:32:06       17 阅读
  2. MySQL_约束与进阶查询

    2024-07-21 14:32:06       17 阅读
  3. 代付是什么意思呢

    2024-07-21 14:32:06       15 阅读
  4. 【Node.js】调试 Node.js 程序

    2024-07-21 14:32:06       13 阅读
  5. 星火模型大体验简单实现一个LLM Chat平台

    2024-07-21 14:32:06       19 阅读
  6. python 图片类型转为 jpg

    2024-07-21 14:32:06       17 阅读