C#实现顺序栈和链栈

顺序栈和链栈分别类似于顺序表和单链表,只是由于栈的First In Last Out性质,其操作相对简单,是顺序表和单链表的子集。

链栈中的链不使用Head属性,这一属性是多余的,使用链栈类的TopNode属性即可。另外,为了避免每次返回链栈的长度都要遍历所有结点,在链栈类中增加Num属性,Push操作时,Num自加,Pop操作时,Num自减,始终等于链栈中的结点数。

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Stack { //栈的接口 public interface IStackDS<T> { int GetLength(); bool IsEmpty(); void Clear(); void Push(T t); T Pop(); T GetTop(); } //顺序栈类 class SequenceStack<T> : IStackDS<T> { private int intMaxSize; private int intTopPointer; private T[] tData; public int MaxSize { get { return this.intMaxSize; } set { this.intMaxSize = value; } } public int TopPointer//没有set,不应该在类外修改top的Pointer { get { return this.intTopPointer; } } public T this[int i]//没有set,不支持类外修改 { get { return this.tData[i]; } } public SequenceStack():this(100)//默认MaxSize为100 { } public SequenceStack(int size) { this.tData = new T[size]; this.intMaxSize = size; this.intTopPointer = -1;//栈为空时,top的pointer为-1 } #region IStackDS<T> 成员 public int GetLength() { return this.intTopPointer+1; } public bool IsEmpty() { return this.intTopPointer == -1; } public void Clear() { this.intTopPointer = -1; } public void Push(T t) { this.intTopPointer++; this.tData[this.intTopPointer] = t; } public T Pop() { if (this.IsEmpty()) { Console.WriteLine("The stack has no elements!"); return default(T); } else { T t = this.tData[this.intTopPointer]; this.intTopPointer--; return t; } } public T GetTop() { if (this.IsEmpty()) { Console.WriteLine("The stack has no elements!"); return default(T); } else { return this.tData[this.intTopPointer]; } } #endregion } //链栈的结点类 class Node<T> { private T tVal; private Node<T> tNext; public T Val { get { return this.tVal; } set { this.tVal = value; } } public Node<T> Next { get { return this.tNext; } set { this.tNext = value; } } public Node() { this.tVal = default(T); this.tNext = null; } public Node(T t) { this.tVal = t; this.tNext = null; } } //链栈类。不设置里面链表的head属性,因为这是多余的。用TopNode表示栈顶即可。 //另,为了避免返回长度时对链表遍历,使用num计数,使num与栈内的结点个数同时变化。 class LinkedStack<T>:IStackDS<T> { private Node<T> nTopNode; private int intNum; public Node<T> TopNode { get { return this.nTopNode; } set { this.nTopNode = value; } } public int Num { get { return this.intNum; } set { this.intNum = value; } } public LinkedStack() { this.nTopNode = null; this.intNum = 0; } public LinkedStack(Node<T> node) { this.nTopNode = node; this.intNum = 1; } #region IStackDS<T> 成员 public int GetLength() { return this.intNum; } public bool IsEmpty() { return this.intNum == 0; } public void Clear() { this.nTopNode = null; this.intNum = 0; } public void Push(T t) { Node<T> node = new Node<T>(t); node.Next = this.nTopNode;//无论TopNode是否为null,都可以直接赋,无需判断是否为null this.nTopNode = node; this.intNum++; } public T Pop() { if (this.IsEmpty()) { Console.WriteLine("The stack has no elements!"); return default(T); } else { Node<T> node = this.nTopNode; this.nTopNode = this.nTopNode.Next; this.intNum--; return node.Val; } } public T GetTop() { if (this.IsEmpty()) { Console.WriteLine("The stack has no elements!"); return default(T); } else { Node<T> node = this.nTopNode; return node.Val; } } #endregion } class Program { static void Main(string[] args) { SequenceStack<int> stack = new SequenceStack<int>(10); stack.Push(3); stack.Push(9); while (!stack.IsEmpty()) { int i = stack.Pop(); Console.WriteLine(i); } Console.ReadLine(); LinkedStack<int> stack2 = new LinkedStack<int>(); stack2.Push(3); stack2.Push(9); while (!stack2.IsEmpty()) { int i = stack2.Pop(); Console.WriteLine(i); } Console.ReadLine(); } } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 C# 链栈实现数制转换的示例代码: ```csharp using System; // 定义链栈节点 class Node { public int data; // 节点数据 public Node next; // 下一个节点指针 public Node(int data) { this.data = data; this.next = null; } } // 定义链栈类 class LinkStack { private Node top; // 栈顶节点指针 public LinkStack() { this.top = null; } // 判断栈是否为空 public bool IsEmpty() { return this.top == null; } // 入栈 public void Push(int data) { Node newNode = new Node(data); newNode.next = this.top; this.top = newNode; } // 出栈 public int Pop() { if (this.IsEmpty()) { Console.WriteLine("栈已空!"); return -1; } int data = this.top.data; this.top = this.top.next; return data; } // 获取栈顶元素 public int GetTop() { if (this.IsEmpty()) { Console.WriteLine("栈已空!"); return -1; } return this.top.data; } } class Program { static void Main(string[] args) { int num, baseNum; Console.Write("请输入一个十进制整数:"); num = int.Parse(Console.ReadLine()); Console.Write("请输入要转换的进制数(2-9):"); baseNum = int.Parse(Console.ReadLine()); LinkStack stack = new LinkStack(); while (num > 0) { int remainder = num % baseNum; stack.Push(remainder); num /= baseNum; } Console.Write("转换后的结果为:"); while (!stack.IsEmpty()) { Console.Write(stack.Pop()); } Console.WriteLine(); } } ``` 运行程序,输入一个十进制整数和要转换的进制数,程序会使用链栈进行进制转换并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值