You are on page 1of 11

A

TECHNICAL REPORT

ON
CSE302 ASSIGNMENT

PRESENTED BY: GROUP 07

SUPERVISED BY: DR FALOHUN A S

SUBMITTED TO:
DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING

FACULTY OF ENGINEERING AND TECHNOLOGY,


LADOKE AKINTOLA UNIVERSITY,
OGBOMOSO, OYO STATE

DECEMBER, 2021.

1
THE UNDERLISTED NAMES ARE THE MEMBER OF 300L GROUP 7

S/N NAMES MATRIC NO.

1. AMAO SODEEQ TUNDE 191941

2. GBADAMOSI EMMAUNEL 182174

3. ADEGBENRO OLAYINKA 183748

4. OYETUNJI OYEWUMI NAFISAT 191960

5. OJO MARY MOJOYINOLA 184033

6. ADELEKE ABUBAKAR A. 181170

7. ABIOLA JOSHUA O. 180594

8. AYANLOWO KAFAYAT F. 163968

9. OLAWALE TOBILOBA S. 196071

10. OSHIN TEMILOLUWA 183923

QUESTION
2
Stack and Queue are two important techniques in storing data in the computer's memory, do a
C# program that will implement the two using switch selection statement with options to
push onto the stack and POPfrom same

CODE

using System;
namespace StackQueueOperation
{
//+++ Start Stack class +++++
internal class Stack
{
static readonlyint MAX = 1000;
int top;
int[] stack = new int[MAX];

bool IsEmpty()
{
return (top < 0);
}
public Stack()
{
top = -1;
}
internal bool Push(int data)
{
if (top >= MAX)
{
Console.WriteLine("Stack Overflow");
return false;
}
else
{
stack[++top] = data;
return true;
}
}

internal int Pop()


{
if (top < 0)
{
Console.WriteLine("Stack Underflow");
return 0;
}
else
{
int value = stack[top--];
return value;
}
}

internal void Peek()


{
if (top < 0)
3
{
Console.WriteLine("Stack Underflow");
return;
}
else
Console.WriteLine("The topmost element of Stack is : {0}", stack[top]);
}

internal void PrintStack()


{
if (top < 0)
{
Console.WriteLine("Stack Underflow");
return;
}
else
{
Console.WriteLine("Items in the Stack are :");
for (inti = top; i>= 0; i--)
{
Console.WriteLine(stack[i]);
}
}
}
}
//++++++++++++++++++++++++++++++++++++++++ End Stack class ++++++++
+++++++

//+++ Start Queue class +++++


public class Queue
{
private static int front, rear, capacity;
private static int[] queue;

public Queue(int c)
{
front = rear = 0;
capacity = c;
queue = new int[capacity];
}

// function to insert an element


// at the rear of the queue
public void queueEnqueue(int data)
{
// check queue is full or not
if (capacity == rear)
{
Console.Write("\nQueue is full\n");
return;
}

// insert element at the rear


else
{
queue[rear] = data;
rear++;
}
4
return;
}

// function to delete an element


// from the front of the queue
public void queueDequeue()
{
// if queue is empty
if (front == rear)
{
Console.Write("\nQueue is empty\n");
return;
}

// shift all the elements from index 2 till rear


// to the right by one
else
{
for (inti = 0; i< rear - 1; i++)
{
queue[i] = queue[i + 1];
}

// store 0 at rear indicating there's no element


if (rear < capacity)
queue[rear] = 0;

// decrement rear
rear--;
}
Console.WriteLine("Dequeue Successfully");
return;
}

// print queue elements


public void queueDisplay()
{
inti;
if (front == rear)
{
Console.Write("\nQueue is Empty\n");
return;
}

// traverse front to rear and print elements


for (i = front; i< rear; i++)
{
Console.Write(" {0} <-- ", queue[i]);
}
return;
}

// print front of queue


public void queueFront()
{
if (front == rear)
{
Console.Write("\nQueue is Empty\n");
5
return;
}
Console.Write("\nFront Element is: {0}", queue[front]);
return;
}
}
//+++ End Queue class +++++
//++++++++++++++++++++++++++++++++++ End Classes ++++++++++++++++++
+++++

//++++Driver codes section++++


class Program
{
static void Main(string[] args)
{
string ext = "";
do
{
Console.Clear();
Console.WriteLine("================ Welcome! to StackQueue Operation
===========");
Console.WriteLine();
Console.WriteLine("1. Stack Operation");
Console.WriteLine("2. Queue Operation");
Console.WriteLine();
Console.Write("Please select Operation to perform: ");
intmychoice = Convert.ToInt32(Console.ReadLine());
switch (mychoice)
{
case 1:
//==========================Stack Driver
code========================
Console.WriteLine("++++++++++++ Welcome to Stack Operations +++++++++++
++");
Stack myStack = new Stack();
string x = "";
Console.WriteLine("\nHow many element you want to push to satck?");
intnum = Convert.ToInt32(Console.ReadLine());
for (inti = 1; i<= num; i++)
{
Console.WriteLine("Enter the element " + i);
int element = Convert.ToInt32(Console.ReadLine());
myStack.Push(element);
}
do
{
Console.Clear();
Console.WriteLine("++++++++++++ Welcome to Stack Operations +++++++++++
++");
Console.WriteLine();
Console.WriteLine("1. Print Stack");
Console.WriteLine("2. Push more Element");
Console.WriteLine("3. Pop Element");
Console.WriteLine("4. Peek Stack");
Console.WriteLine();
Console.WriteLine("Select operation to perform");
int select = Convert.ToInt32(Console.ReadLine());
switch (select)
6
{
case 1:
myStack.PrintStack();
break;
case 2:
Console.WriteLine("How many element you want to push to satck?");
int m = Convert.ToInt32(Console.ReadLine());
for (inti = 1; i<= m; i++)
{
Console.WriteLine("Enter the element " + i);
int element = Convert.ToInt32(Console.ReadLine());
myStack.Push(element);
}
break;
case 3:
Console.WriteLine("The Pop element is: " + myStack.Pop());
break;
case 4:
myStack.Peek();
break;
default:
Console.WriteLine("Oops!... Selection out range!!..choose btw 1 and
4");
break;
}
Console.Write("Do you want to perform more stack operation?..Enter Y
for Yes, any key to exit: ");
x = Console.ReadLine();
} while (x == "Y" || x == "y");
// ===========End of Stack Driver==============
break;
case 2:
//======================Queue Dirver
code=========================
Console.WriteLine("++++++++++++ Welcome to Queue Operations +++++++++++
++");
//Create a queue of capacity
Console.WriteLine("\nWhat is the capacity(size) of your queue?");
int size = Convert.ToInt32(Console.ReadLine());
Queue myqueue = new Queue(size);
string ex = "";
Console.Clear();
//// inserting elements in the queue
//Console.WriteLine("Now let enter some element
in the Queue..How many element you want to Enqueue this time?");
//int n = Convert.ToInt32(Console.ReadLine());
//if (n > size)
//{
// Console.WriteLine("Oops!..Capacity of the
queue can not hold that");
//}
//else
//{
// for (inti = 1; i<= n; i++)
// {
// Console.WriteLine("Enter the element
" + i);

7
// int element =
Convert.ToInt32(Console.ReadLine());
// myqueue.queueEnqueue(element);
// }
do
{
Console.Clear();
Console.WriteLine("++++++++++++ Welcome to Queue Operations +++++++++++
++");
Console.WriteLine();
Console.WriteLine("1. Print Queue");
Console.WriteLine("2. Enqueue Element");
Console.WriteLine("3. Dequeue Element");
Console.WriteLine("4. Peek Front Element");
Console.WriteLine();
Console.WriteLine("Select operation to perform");
int select = Convert.ToInt32(Console.ReadLine());
switch (select)
{
case 1:
// print Queue elements
myqueue.queueDisplay();
break;
case 2:
// Insert Queue elements
Console.WriteLine("Enter the element to enqueue.");
intNewElement = Convert.ToInt32(Console.ReadLine());
myqueue.queueEnqueue(NewElement);
Console.WriteLine("After Equeuing, New Queue is:");
myqueue.queueDisplay();
break;
case 3:
// Delecting Queue elements
myqueue.queueDequeue();
Console.WriteLine("After Dequeuing, New Queue is:");
myqueue.queueDisplay();
break;
case 4:
// Displaying Queue front
element
myqueue.queueFront();
break;
default:
Console.WriteLine("Oops!... Selection out range!!..choose btw 1 and
4");
break;
}
Console.Write("\n\nDo you want to perform more stack operation?..Enter
Y for Yes, any key to exit: ");
ex = Console.ReadLine();

} while (ex == "Y" || ex == "y");


//}
break;
default:
Console.WriteLine("Oops!... Selection out range!!..choose btw 1 and
2");
break;
8
}
Console.Write("\n\nDo you want to Choose btw Stack or Queue
Operation..Enter Y for Yes, any key to exit: ");
ext = Console.ReadLine();
} while (ext == "Y" || ext == "y");

}
}
}

OUPUT

9
10
11

You might also like