Sample Programs For Stack Queue
Sample Programs For Stack Queue
Queue Class
It represents a first-in, first out collection of object. It is used when you need a first-in, first-out
access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is
called deque.
1 Count
Gets the number of elements contained in the Queue.
namespace Queue1
{
class Program
{
static void Main(string[] args)
{
Queue q = new Queue();
q.Enqueue('A');
q.Enqueue('M');
q.Enqueue('G');
q.Enqueue('W');
Console.WriteLine();
q.Enqueue('V');
q.Enqueue('H');
Console.WriteLine("Current queue: ");
foreach (char c in q) Console.Write(c + " ");
Console.WriteLine();
Console.WriteLine("Removing some values ");
char ch = (char)q.Dequeue();
Console.WriteLine("The removed value: {0}", ch);
ch = (char)q.Dequeue();
Console.WriteLine("The removed value: {0}", ch);
Console.ReadKey();
}
}
}
namespace ImplementStack
{
class Stack
{
private int[] ele;
private int top;
private int max;
public Stack(int size)
{
ele = new int[size]; // Maximum size of Stack
top = -1;
max = size;
}
p.push(10);
p.push(20);
p.push(30);
p.printStack();
p.pop();
Console.ReadKey();
}
}
}