Docsity

Implementation of Stacks and Queues using Arrays, Lab Reports of Data Structures and Algorithms

Toggle sidebar
bg1
18-EE-21
Lab # 03
Title:
To implement different operations on Stacks and Queues using arrays
Objective:
To perform operation on stacks and Queue using arrays
To know how to insertion, deletion, display element in stacks and Queue
Equipment Required:
PC/ Laptop
Dev C++ software
Stack data Structure:
Stack is a linear data structure in which the insertion and deletion operations are performed at only one
end. In a stack, adding and removing of elements are performed at single position which is known as "top".
That means, new element is added at top of the stack and an element is removed from the top of the
stack. In stack, the insertion and deletion operations are performed based on LIFO (Last In First Out)
principle.
Stack can also be defined as:
"A Collection of similar data items in which both insertion and deletion operations are performed based
on LIFO principle"
Stack Operations:
Following are the basic operations that are supported by the stack.
push – Adds or pushes an element into the stack.
pop – Removes or pops an element out of the stack.
peek – Gets the top element of the stack but doesn’t remove it.
isFull – Tests if the stack is full.
isEmpty – Tests if the stack is empty.
1 | P a g e
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Implementation of Stacks and Queues using Arrays and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Lab # 03

Title:

To implement different operations on Stacks and Queues using arrays

Objective:

 To perform operation on stacks and Queue using arrays

 To know how to insertion, deletion, display element in stacks and Queue

Equipment Required:

 PC/ Laptop

 Dev C++ software

Stack data Structure:

Stack is a linear data structure in which the insertion and deletion operations are performed at only one

end. In a stack, adding and removing of elements are performed at single position which is known as "top".

That means, new element is added at top of the stack and an element is removed from the top of the

stack. In stack, the insertion and deletion operations are performed based on LIFO (Last In First Out)

principle.

Stack can also be defined as:

"A Collection of similar data items in which both insertion and deletion operations are performed based

on LIFO principle"

 Stack Operations:

Following are the basic operations that are supported by the stack.

 push – Adds or pushes an element into the stack.

 pop – Removes or pops an element out of the stack.

 peek – Gets the top element of the stack but doesn’t remove it.

 isFull – Tests if the stack is full.

 isEmpty – Tests if the stack is empty.

 Basic features of Stack:

 Stack is an ordered list of similar data type.

 Stack is a LIFO (Last in First out) structure or we can say FILO (First in Last out).

 push() function is used to insert new elements into the Stack and pop() function is used to

remove an element from the stack. Both insertion and removal are allowed at only one end of

Stack called Top.

 Stack is said to be in Overflow state when it is completely full and is said to be

in Underflow state if it is completely empty.

 Algorithm for PUSH operation:

 Check if the stack is full or not.

 If the stack is full, then print error of overflow and exit the program.

 If the stack is not full, then increment the top and add the element.

 Algorithm for POP operation:

 Check if the stack is empty or not.

 If the stack is empty, then print error of underflow and exit the program.

 If the stack is not empty, then print the element at the top and decrement the top.

 Implementation of Stack Data Structure:

Stack can be easily implemented using an Array or a Linked List. Arrays are quick, but are limited in size and

Linked List requires overhead to allocate, link, unlink, and deallocate, but is not limited in size. Here we will

implement Stack using array

Queue Data Structure:

A queue is a useful data structure in programming. It is similar to the ticket queue outside a cinema hall,

where the first person entering the queue is the first person who gets the ticket. Queue follows the First In

First Out(FIFO) rule - the item that goes in first is the item that comes out first too

LAB TASKS

Task # 1: Write a program to implement following operations of stack using array data

structure

Program Code:

#include #include<windows.h> #include using namespace std; int stack[100], n=100, top=-1; void push(int val) { system("cls"); if(top>=n-1) cout<<"\n\n\tStack Overflow\n"<<endl; else {top++; stack[top]=val; }} void pop() {system("cls"); if(top<=-1) {cout<<"\n\n\n\t\t\tStack Underflow\n"<<endl; cout<<"\n\t-----------------------------------------------------\a"; } else { cout<<"\n\n\n\t\t\tThe popped element is : "<<stack[top]<<endl; cout<<"\n\t-----------------------------------------------------\a"; top--; }} void display() { system("cls"); if(top>=0) { cout<<"\n\n\n\t\tStack elements are: "; for(int i=top; i>=0; i--) cout<<stack[i]<<" "; cout<<endl; cout<<"\n\t-----------------------------------------------------\a\a"; } else cout<<"\n\n\n\t\t\tStack is empty\n"; cout<<"\n\t-----------------------------------------------------\a\a"; } int main() {int ch, val; do { cout<<"\n\t\t\t>>>>>>>> Menu of Stack <<<<<<<<"; cout<<"\n"; cout<<"\n\t-------------------------------------------------------------"; cout<<"\n\tENTER 1: PUSH"; cout<<"\n\t\t----------------"; cout<<"\n\tENTER 2: POP"; cout<<"\n\t\t----------------"; cout<<"\n\tENTER 3: Display"; cout<<"\n\t\t----------------"; cout<<"\n\tENTER 4: To Exit"; cout<<"\n\t\t----------------"; cout<<"\n\n\tPlease Enter Your Choice: "; cin>>ch; switch(ch) { case 1: system("cls"); cout<<"\n\n\n\t\tEnter value to be pushed: ";

cin>>val; push(val); system("cls"); break; case 2: pop();Sleep(2500);system("cls");break; case 3: display();Sleep(2500);system("cls");break; case 4: cout<<"Exit"<<endl;break; default: cout<<"Invalid Choice"<<endl;} }while(ch!=4); return 0;}

Program Output:

Option # 01 is used to Adds or pushes an element into the stacks

I Push/ adds random element (10 20 30 55 99 80) into stack using

option 1 as shown in figure given below

Option # 03 is used to display stacks elements as shown in figure

given below

Task # 2: Write a program to implement following operations of Queue using array

data structure

Program Code:

#include #include<windows.h> #include using namespace std; class queue { public: int q[11],front,rear,x,result; void enq(); void dque(); void disp(); queue() { front=0; rear=0;}}; void queue::enq() { if(rear>=11){ system("cls"); cout<<"\n\n\n\t\t\tQueue overflow !!\n"; cout<<"\n\t-----------------------------------------------------\a\a";} else { cout<<"\n\n\n\t\tEnter the number to be inserted: "; cin>>x; rear++; q[rear]=x; cout<<"\n\n\n\t\t\tNumber pushed in the queue: "<<q[rear];} } void queue::dque() { system("cls"); if(rear==0) {cout<<"\n\n\n\t\t\tQueue underflow !!\n"; cout<<"\n\t-----------------------------------------------------\a\a";} else {if(front==rear) { front=0; rear=0;} else front++;} cout<<"\n\n\n\t\t\tDeleted element is: "; result=q[front]; cout<<result; cout<<"\n\t-----------------------------------------------------\a\a"; } void queue::disp() {system("cls"); if(rear==0){ cout<<"\n\n\n\t\t\tQueue underflow !!\n"; cout<<"\n\t-----------------------------------------------------\a\a";} else { cout<<"\n\n\n\t\tContents of queue is: "; for(int i=front+1;i<=rear;i++) cout<<q[i]<<" "; cout<<"\n\n\t-----------------------------------------------------\a\a";}

int main() { int c; queue qu; do{ cout<<"\n\t\t\t>>>>>>>> Menu of Queue <<<<<<<<"; cout<<"\n"; cout<<"\n\t-------------------------------------------------------------"; cout<<"\n\tENTER 1: Insertion"; cout<<"\n\t\t----------------"; cout<<"\n\tENTER 2: Deletion"; cout<<"\n\t\t----------------"; cout<<"\n\tENTER 3: Display"; cout<<"\n\t\t----------------"; cout<<"\n\tENTER 4: To Exit"; cout<<"\n\t\t----------------"; cout<<"\n\n\tPlease Enter Your Choice: "; cin>>c; switch(c){ case 1: system("cls"); qu.enq();system("cls"); break; case 2: qu.dque();Sleep(2500);system("cls"); break; case 3: qu.disp();Sleep(2500);system("cls"); break; case 4: cout<<"Exit"<<endl;break; default: cout<<"\nInvalid choice !!\n"; }} while(c!=4); return 0;}

Program Output:

Option # 01 is used Enqueue/ Add an element to the end of

the queue

When Queue is empty following message display

Option # 04 is used to Exit from queue menu