0% found this document useful (0 votes)
89 views7 pages

DS LAB Fall 2022-23 Lab Task-4

The document provides code examples to implement three different data structures using arrays in C++: 1. A static array-based stack with push and pop operations that displays an error message if the stack is full or empty. 2. A static array-based queue with enqueue and dequeue operations that displays an error message if the queue is full or empty. 3. A dynamic array-based stack with push and pop operations that increases the array size if the stack is full to allow additional pushes, and displays a message if the stack is empty. Sample console outputs are provided for each implementation.

Uploaded by

MD Deniad Alam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views7 pages

DS LAB Fall 2022-23 Lab Task-4

The document provides code examples to implement three different data structures using arrays in C++: 1. A static array-based stack with push and pop operations that displays an error message if the stack is full or empty. 2. A static array-based queue with enqueue and dequeue operations that displays an error message if the queue is full or empty. 3. A dynamic array-based stack with push and pop operations that increases the array size if the stack is full to allow additional pushes, and displays a message if the stack is empty. Sample console outputs are provided for each implementation.

Uploaded by

MD Deniad Alam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab Task-4

1. Write a code to implement an array (static) based stack and its operations (Push and Pop). Get options
from the user to push, pop and display the stack. If the stack is full and you want to push another
value show message “Stack overflowed!!!”. If empty show “Stack is empty”

Example:
What you want to do?
1. Push element in the stack
2. Pop element from the stack
3. Display the stack
Your code here:
#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
Your whole Screenshot here: (Console Output):

2. Write a code to implement an array (static) based queue and its operations (Enqueue and Dequeue).
Get options from the user to Enqueue, Dequeue and display the queue. If the Queue is full and you
want to enqueue another value show message “Queue overflowed!!!”. If empty show “Queue is
empty”

Example:
What you want to do?
1. Enqueue element in the queue
2. Dequeue element in the queue
3. Display the stack
Your code here:
#include <iostream>
using namespace std;
int queue[100], n = 100, front = - 1, rear = - 1;
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}
}
void Delete() {
if (front == - 1 || front > rear) {
cout<<"Queue Underflow "<<endl;
return ;
} else {
cout<<"Element deleted from queue is : "<< queue[front] <<endl;
front++;;
}
}
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
int main() {
int ch;
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}

Your whole Screenshot here: (Console Output):

3. Write a code to implement a dynamic (memory) array based stack and its operations (Push and Pop).
Get options from the user to push, pop and display the stack. If the stack is full and you want to push
another value, it will allow you to increase the size of stack and push the data. If empty show “Stack is
empty”
Example:

What you want to do?


1. Push element in the stack
2. Pop element in the stack
3. Display the stack

Your code here:


#include<iostream>
using namespace std;
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{

top=-1;
cout<<"Enter the size of STACK[MAX=100]: " ;
cin>>n;

cout<<"What you want to do?"<<endl;


cout<<"1.Push element in the stack.\n2.Pop element from the stack. \n3.Display the stack.\n4.Exit.\n"<<endl;
do
{
cout<<"Enter option : "<<endl;
cin>>choice;
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
cout<<" Exit. ";
break;
}
default:
{
cout<<" Please Enter a Valid Choice(1/2/3/4)."<<endl;
}

}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
cout<<"Stack is full.Size will increase by pushing next element."<<endl;
n++;
cout<<"Enter a value to be pushed:"<<endl;
cin>>x;
top++;
stack[top]=x;

}
else
{
cout<<"Enter an element to be pushed:"<<endl;
cin>>x;
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
cout<<" Stack is under flow"<<endl;
}
else
{
cout<<"The popped elements is "<<stack[top]<<endl;
top--;
}
}
void display()
{
if(top>=0)
{
cout<<"The elements in stack :" << endl;
for(i=top; i>=0; i--)
cout<<stack[i]<<endl;
cout<<endl;
}
else
{
cout<<"The stack is empty"<<endl;
}

}
Your whole Screenshot here: (Console Output):

You might also like