Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
42 views

Sample Programs For Stack Queue

This document discusses stacks and queues. It defines a queue as a first-in, first-out collection of objects where adding an item is called enqueue and removing an item is called dequeue. It lists common queue properties like count and methods like enqueue, dequeue, and contains. It also provides examples of using queues and defining a stack class with methods like push, pop, and peek.

Uploaded by

Ezra El Angus
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Sample Programs For Stack Queue

This document discusses stacks and queues. It defines a queue as a first-in, first-out collection of objects where adding an item is called enqueue and removing an item is called dequeue. It lists common queue properties like count and methods like enqueue, dequeue, and contains. It also provides examples of using queues and defining a stack class with methods like push, pop, and peek.

Uploaded by

Ezra El Angus
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

STACKS & QUEUES

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.

Methods and Properties of the Queue Class


The following table lists some of the commonly used properties of the Queue class.
Sr.No. Property & Description

1 Count
Gets the number of elements contained in the Queue.

The following table lists some of the commonly used methods of the Queue class.


Sr.No. Method & Description

1 public virtual void Clear();


Removes all elements from the Queue.

2 public virtual bool Contains(object obj);


Determines whether an element is in the Queue.

3 public virtual object Dequeue();


Removes and returns the object at the beginning of the Queue.

4 public virtual void Enqueue(object obj);


Adds an object to the end of the Queue.

5 public virtual object[] ToArray();


Copies the Queue to a new array.

6 public virtual void TrimToSize();


Sets the capacity to the actual number of elements in the Queue.
Examples: The following sample programs demonstrate the use of Stack and Queue.

//Sample Program No. 1


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

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("Current queue: ");


foreach (char c in q) Console.Write(c + " ");

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();
}
}
}

//Sample Program No. 2


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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;
}

public void push(int item)


{
if (top == max - 1)
{
Console.WriteLine("Stack Overflow");
return;
}
else
{
ele[++top] = item;
}
}

public int pop()


{
if (top == -1)
{
Console.WriteLine("Stack is Empty");
return -1;
}
else
{
Console.WriteLine("{0} popped from stack ", ele[top]);
return ele[top--];
}
}

public int peek()


{
if (top == -1)
{
Console.WriteLine("Stack is Empty");
return -1;
}
else
{
Console.WriteLine("{0} popped from stack ", ele[top]);
return ele[top];
}
}

public void printStack()


{
if (top == -1)
{
Console.WriteLine("Stack is Empty");
return;
}
else
{
for (int i = 0; i <= top; i++)
{
Console.WriteLine("{0} pushed into stack", ele[i]);
}
}
}
}

// Driver program to test above functions


class Program
{
static void Main()
{
Stack p = new Stack(5);

p.push(10);
p.push(20);
p.push(30);
p.printStack();
p.pop();
Console.ReadKey();
}
}
}

You might also like