栈
简要介绍
- 栈是限制线性表中元素的插入和删除只能在线性表的同一端进行的一种特殊线性表。
- 允许插入和删除的一端称为栈顶。
- 另一端为固定的一端,称作栈底。
- 遵循先入后出(FILO)的原则
图解
内部成员
[注]本例使用数组存储数据,链表实现栈代码在最后
修饰符 | 变量名 | 含义 |
---|---|---|
private int | top | 指向栈顶元素 |
private int | maxSize | 数组最大存储空间 |
private int[] | datas | 存储数据的数组 |
public bool | IsFull | 栈是否已满 |
public bool | IsEmpty | 栈是否为空 |
public void | Push(int value) | 向栈顶压入数据 |
public int | Pop() | 弹出栈顶数据 |
public int | Peek() | 查看栈顶数据,但不弹出 |
成员代码
构造器
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;
}
}
}