什么是栈
栈和队列是非常重要的两种数据结构,在软件设计中应用很多。栈和队列也是线性结构,线性表、栈和队列这三种数据结构的数据元素以及数据元素间的逻辑关系完全相同,差别是线性表的操作不受限制,而栈和队列的操作受到眼制。栈的操作只能在表的一端进行,队列的插入操作在表的一端进行而其它操作在表的另一端进行,所以,把栈和队列称为操作受限的线性表。
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 numberint 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()); // 输出 3Console.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()); // 输出 1Console.WriteLine(queue.Peek()); // 输出 2 queue.Enqueue(4);queue.Enqueue(5);queue.Enqueue(6); while (!queue.IsEmpty()){Console.WriteLine(queue.Dequeue());}} }