使用進棧和出棧操作實現堆疊的 C# 程式
使用進棧操作設定具有向堆疊新增元素功能的堆疊 −
Stack st = new Stack();
st.Push('A');
st.Push('M');
st.Push('G');
st.Push('W');若要從堆疊彈出元素,請使用 Pop() 方法 −
st.Pop();
st.Pop();
以下示例演示了使用進棧和出棧操作實現堆疊 −
示例
using System;
using System.Collections;
namespace CollectionsApplication {
class Program {
static void Main(string[] args) {
Stack st = new Stack();
st.Push('A');
st.Push('M');
st.Push('G');
st.Push('W');
Console.WriteLine("Current stack: ");
foreach (char c in st) {
Console.Write(c + " ");
}
Console.WriteLine();
st.Push('V');
st.Push('H');
Console.WriteLine("The next poppable value in stack: {0}", st.Peek());
Console.WriteLine("Current stack: ");
foreach (char c in st) {
Console.Write(c + " ");
}
Console.WriteLine();
Console.WriteLine("Removing values ");
st.Pop();
st.Pop();
st.Pop();
Console.WriteLine("Current stack: ");
foreach (char c in st) {
Console.Write(c + " ");
}
}
}
}輸出
Current stack: W G M A The next poppable value in stack: H Current stack: H V W G M A Removing values Current stack: G M A
廣告
資料結構
聯網
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP