푸시 작업으로 스택을 설정하여 스택에 요소 추가 -
Stack st = new Stack();
st.Push('A');
st.Push('M');
st.Push('G');
st.Push('W'); 스택에서 요소를 팝하려면 Pop() 메서드를 사용하십시오 -
st.Pop();
st.팝();
다음은 푸시 및 팝 작업으로 스택을 구현하는 예입니다. -
예시
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