Clear a stack
using System;
using System.Collections;
class MainClass
{
static void Main(string[] args)
{
Stack a = new Stack(10);
int x = 0;
a.Push(x);
x++;
a.Push(x);
foreach (int y in a)
{
Console.WriteLine(y);
}
a.Pop();
a.Clear();
}
}
1
0
Pop and Peek
using System;
using System.Collections.Generic;
public class Tester
{
static void Main()
{
Stack<Int32> intStack = new Stack<Int32>();
for (int i = 0; i < 8; i++)
{
intStack.Push(i * 5);
}
PrintValues(intStack);
Console.WriteLine("\n(Pop)\t{0}",intStack.Pop());
PrintValues(intStack);
Console.WriteLine("\n(Pop)\t{0}",intStack.Pop());
PrintValues(intStack);
Console.WriteLine("\n(Peek) \t{0}",intStack.Peek());
PrintValues(intStack);
int[] targetArray = new int[12];
for (int i = 0; i < targetArray.Length; i++)
{
targetArray[i] = i * 100 + 100;
}
PrintValues(targetArray);
intStack.CopyTo(targetArray, 6);
PrintValues(targetArray);
}
public static void PrintValues(IEnumerable<Int32> myCollection)
{
IEnumerator<Int32> enumerator =myCollection.GetEnumerator();
while (enumerator.MoveNext())
Console.Write("{0} ", enumerator.Current);
}
}
Push and pop value
using System;
using System.Collections;
class StackDemo {
public static void Main() {
Stack st = new Stack();
st.Push(1);
st.Push(2);
foreach(int i in st)
Console.Write(i + " ");
st.Push(1);
Console.Write("stack: ");
foreach(int i in st)
Console.Write(i + " ");
Console.WriteLine();
Console.Write("Pop -> ");
int a = (int) st.Pop();
Console.WriteLine(a);
Console.Write("stack: ");
foreach(int i in st)
Console.Write(i + " ");
Console.WriteLine();
}
}
2 1 stack: 1 2 1
Pop -> 1
stack: 2 1
Stack And Queue
using System;
using System.Collections.Generic;
using System.ruponentModel;
class StackAndQueue
{
static void Main()
{
Queue<int> queue = new Queue<int>();
Stack<int> stack = new Stack<int>();
for (int i = 0; i < 10; i++)
{
queue.Enqueue(i);
stack.Push(i);
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Stack:{0} Queue:{1}",
stack.Pop(), queue.Dequeue());
}
}
}