数据结构学习之路-第三章:栈的应用
https://blog.csdn.net/libin1105/article/details/48295439
C# 栈的应用
https://blog.csdn.net/qq_42606051/article/details/81080385
在一个栈的输入序列为12345 下面哪个不可能是栈的输出序列?
A. 23415 B.54132 C.23145 D.15432
23415------>1进栈,2进栈,2出栈,3进栈,3出栈,4进栈,4出栈,1出栈,5进栈,5出栈
23145------>1进栈,2进栈,2出栈,3进栈,3出栈,1出栈,4进栈,4出栈,5进栈,5出栈
15432------>1进栈,1出栈,2进栈,2进栈,4进栈,5进栈,5出栈,4出栈,3出栈,2出栈
54132不可能。
何为栈
栈(stack):是限定仅在表尾进行插入和删除操作的线性表
栈(Stack)是操作限定在表的尾端进行的线性表。表尾由于要进行插入、删除等操作,所以,它具有特殊的含义,把表尾称为栈顶( Top),另一端是固定的,叫栈底( Bottom)。当栈中没有数据元素时叫空栈(Empty Stack)。 栈的操作示意图如图所示。
操作
public interface IStack<T> {
int Count{get;}
int GetLength(); //求栈的长度
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 _002_栈
{
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 int GetLength()
{
return Count;
}
public bool IsEmpty()
{
return Count == 0;
}
public void Clear()
{
top = -1;
}
public void Push(T item)
{
data[top + 1] = item;
top++;
}
public T Pop()
{
T temp = data[top];
top--;
return temp;
}
public T Peek()
{
return data[top];
}
}
}
链栈
结点
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _002_栈 {
/// <summary>
/// 链栈的结点
/// </summary>
/// <typeparam name="T"></typeparam>
class Node <T>
{
private T data; //数据
private Node<T> next; //指针
//初始化 数据,,指针
public Node()
{
data = default(T);
next = null;
}
//构造
public Node(T data)
{
this.data = data;
next = null;
}
public Node(T data, Node<T> next)
{
this.data = data;
this.next = next;
}
public Node(Node<T> next)
{
this.next = next;
data = default(T);
}
//属性
public T Data { get { return data; }set { data = value; } }
public Node<T> Next
{
get { return next; }
set { next = value; }
}
}
}
链表
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _002_栈 {
class LinkStack<T>:IStackDS<T>
{
private Node<T> top;// 栈顶元素结点
private int count = 0;//栈中元素的个数
/// <summary>
/// 取得栈中元素的个数
/// </summary>
public int Count
{
get { return count; }
}
/// <summary>
/// 取得栈中元素的个数
/// </summary>
/// <returns></returns>
public int GetLength()
{
return count;
}
/// <summary>
/// 判断栈中是否有数据
/// </summary>
/// <returns></returns>
public bool IsEmpty()
{
return count == 0;
}
/// <summary>
/// 清空栈中所有的数据
/// </summary>
public void Clear()
{
count = 0;
top = null;
}
/// <summary>
/// 入栈
/// </summary>
/// <param name="item"></param>
public void Push(T item)
{
//把新添加的元素作为头结点(栈顶)
Node<T> newNode = new Node<T>(item);
newNode.Next = top;
top = newNode;
count++;
}
/// <summary>
/// 出栈 取得栈顶元素,然后删除
/// </summary>
/// <returns></returns>
public T Pop()
{
T data = top.Data;
top = top.Next;
count--;
return data;
}
/// <summary>
/// 取得栈顶中的数据,不删除栈顶
/// </summary>
/// <returns></returns>
public T Peek()
{
return top.Data;
}
}
}
应用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _002_栈 {
class Program {
static void Main(string[] args) {
//1,使用BCL中的Stack<T>
//Stack<char> stack = new Stack<char>();
//2,使用我们自己的顺序栈
//IStackDS<char> stack = new SeqStack<char>(30);
//3,使用我们自己的链栈
IStackDS<char> stack = new LinkStack<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 之后得到的数据是:" + temp2);
Console.WriteLine("peek 之后栈中数据的个数:" + stack.Count);
stack.Clear();
Console.WriteLine("clear 之后栈中数据的个数:" + stack.Count);
//Console.WriteLine("空栈的时候,取得栈顶的值:"+stack.Peek());// 出现异常
//当空栈的时候,不要进行peek或者pop操作 ,否则会出现异常
Console.ReadKey();
}
}
}