栈(Stack)
栈中元素是从上到下加入的,即“后进先出”
栈的应用
十进制转二进制
Console.WriteLine("请输入一个十进制数字"); int num = int.Parse(Console.ReadLine()); Stack<int> Remainder = new Stack<int>(); while (num != 0) { Remainder.Push(num % 2); num /= 2; } foreach (var item in Remainder) { Console.ForegroundColor = ConsoleColor.DarkBlue; Console.Write(item); } Console.ReadKey();
|
打印结果:
数组实现栈
栈的相关操作
Stack
void Push(t) 添加元素
T Pop() 删除栈顶
T Peek() 查询栈顶
int Count 栈中多少元素
bool IsEmpty 是否为空
数组在尾端添加删除最合适
新建接口
新建一个**接口Stack**实现以上方法
namespace AlgorithmTest09_实现栈Stack { interface IStack<T> { int Count { get; } bool IsEmpty { get; } void Push(T t); T Pop(); T Peek(); } }
|
实现接口
新建Array1Stack类实现接口功能
using AlgorithmTest06_List;
namespace AlgorithmTest09_实现栈Stack { class Array1Stack<T> : IStack<T> { private SeqList<T> arr;
public Array1Stack(int capacity) { arr = new SeqList<T>(capacity); } public Array1Stack() { arr = new SeqList<T>(); }
public int Count { get { return arr.GetCount(); } }
public bool IsEmpty { get { return arr.IsEmpty(); } }
public void Push(T t) { arr.AddLast(t); } public T Peek() { return arr[arr.count - 1]; } public T Pop() { return arr.RemoveLast(); }
public override string ToString() { return "Stack:" + arr.ToString() + "--Top"; } } }
|
实现效果
using System;
namespace AlgorithmTest09_实现栈Stack { class Program { static void Main(string[] args) { Array1Stack<int> stack = new Array1Stack<int>();
Console.WriteLine("-----栈顶添加元素-----"); for (int i = 0; i < 5; i++) { stack.Push(i); Console.WriteLine(stack); } Console.WriteLine("-----栈顶删除元素-----"); Console.WriteLine("删除:" + stack.Pop()); Console.WriteLine(stack); Console.WriteLine("查询栈顶:" + stack.Peek()); Console.WriteLine("元素个数:" + stack.Count); Console.WriteLine("是否为空:" + stack.IsEmpty); Console.ReadKey(); } } }
|
演示结果
链表实现栈
栈的相关操作
Stack
void Push(t) 添加元素
T Pop() 删除栈顶
T Peek() 查询栈顶
int Count 栈中多少元素
bool IsEmpty 是否为空
链表最方便的是在头部进行添加或删除操作
实现接口
新建LinkedList1Stack类实现IStack接口的方法
using AlgorithmTest08_链表; namespace AlgorithmTest09_实现栈Stack { class LinkedList1Stack<T> : IStack<T> { private LinkedList1<T> list;
public LinkedList1Stack() { list = new LinkedList1<T>(); }
public int Count { get { return list.Count; } }
public bool IsEmpty { get { return list.IsEmpty; } }
public void Push(T t) { list.AddFirst(t); } public T Peek() { return list.GetFirst(); } public T Pop() { return list.RemoveFirst(); }
public override string ToString() { return "Stack:Top--" + list.ToString(); } } }
|
实现效果
using System;
namespace AlgorithmTest09_实现栈Stack { class Program { static void Main(string[] args) { Array1Stack<int> stack = new Array1Stack<int>();
Console.WriteLine("-----栈顶添加元素-----"); for (int i = 0; i < 5; i++) { stack.Push(i); Console.WriteLine(stack); } Console.WriteLine("-----栈顶删除元素-----"); Console.WriteLine("删除:" + stack.Pop()); Console.WriteLine(stack); Console.WriteLine("查询栈顶:" + stack.Peek()); Console.WriteLine("元素个数:" + stack.Count); Console.WriteLine("是否为空:" + stack.IsEmpty);
Console.WriteLine("-----链表栈演示-----"); LinkedList1Stack<int> listStack = new LinkedList1Stack<int>();
Console.WriteLine("-----链表栈顶添加元素-----"); for (int i = 0; i < 5; i++) { listStack.Push(i); Console.WriteLine(listStack); } Console.WriteLine("-----链表栈顶删除元素-----"); Console.WriteLine("删除:" + listStack.Pop()); Console.WriteLine(listStack); Console.WriteLine("查询栈顶:" + listStack.Peek()); Console.WriteLine("元素个数:" + listStack.Count); Console.WriteLine("是否为空:" + listStack.IsEmpty); Console.ReadKey(); } } }
|
演示结果