0% found this document useful (0 votes)
39 views

DS - Lab 1.3

Student Vansh agarwal implemented functions on a stack including PUSH, POP, and checking for overflow and underflow. The program allows the user to insert elements into the stack using PUSH, remove elements using POP, and displays the stack size. PUSH inserts elements at the top of the stack and increments the top pointer, while POP removes the top element and decrements top. Overflow is checked when trying to insert beyond the maximum size, and underflow when trying to POP an empty stack.

Uploaded by

Ritesh agrawal
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)
39 views

DS - Lab 1.3

Student Vansh agarwal implemented functions on a stack including PUSH, POP, and checking for overflow and underflow. The program allows the user to insert elements into the stack using PUSH, remove elements using POP, and displays the stack size. PUSH inserts elements at the top of the stack and increments the top pointer, while POP removes the top element and decrements top. Overflow is checked when trying to insert beyond the maximum size, and underflow when trying to POP an empty stack.

Uploaded by

Ritesh agrawal
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/ 10

Student Name: Vansh agarwal UID: 20BCS6889

Branch: AIML Section/Group: 5B


Semester: 3 Date of Performance: 6/09/2021
Subject Name: DS_Lab Subject Code: 21O-20CSP-236

1. Aim/Overview of the practical:


Write a program to implement the functions on a stack:
a. PUSH
b. POP
c. OVERFLOW& UNDERFLOW

2. Task to be done:

To insert an element in the stack using PUSH function

To remove an element from the stack using POP function

To display the size of stack.

3. Algorithm:
PUSH –
o We initialized top as -1.
o If top>= maximum size of stack then overflow condition is applied.
o Else top is incremented by 1 and element is inserted at that top position using
stack[top]=element.
POP –
o If top<0 then underflow condition is applied.
o Else element present at topmost position is removed as stack follows LIFO rule
and we return stack[top--] to the main calling function.
4.Flowchart:

START

PUSH POP

Is stack overflow Is stack underflow

Yes No Yes No

Top++
Top++ Cannot insert Return
Cannot insert
Stack[top]=element element stack[top--]
element

END
5. Code

#include <iostream>
#include<stdlib.h>
#define size 10
using namespace std;
int stack[size];
int top = -1;
void push(int element){
if(top>=size){
cout<<"Overflow condition,cannot insert element into the stack"<<endl;
return;
}
top++;
stack[top]=element;
cout<<element<<" pushed up successfully"<<endl;

cout<<"\nElements in the stack are: ";


for(int i=0;i<=top;i++){
cout<<stack[i]<<"\t";
}
cout<<"\n";
}

int pop(){
if (top<0){
cout<<"Underflow condition,stack is empty"<<endl;
return INT_MIN;
}

return stack[top--];
}
int main(){
int choice,element;
cout<<"Stack Implementation program"<<endl;

cout<<"________________________________________________________________"<<endl
;
cout<<"PRESS "<<endl;
cout<<"1-Insert an element"<<endl;
cout<<"2-Remove an element"<<endl;
cout<<"3-To display stack size"<<endl;
cout<<"4-Exit the program"<<endl;
cout<<"________________________________________________________________"<<endl
;
while(1){
cout<<"--------------------------------------------------------------------\n"<<endl;
cout<<"Eenter your choice: ";
cin>>choice;
cout<<"--------------------------------------------------------------------\n"<<endl;
switch(choice) {
case 1:
cout<<"Enter the element to insert in the stack: ";
cin>>element;
push(element);
break;

case 2:
element=pop();
if(element!=INT_MIN){
cout<<"Removed element is: "<<element<<endl;
cout<<"Remaining elements in the stack are: ";
for(int i=0;i<=top;i++){
cout<<stack[i]<<"\t";
}
}
cout<<"\n";
break;
case 3:
cout<<"Stack size: "<<top+1<<endl;
break;

case 4:
cout<<"Exiting the program";
exit (0);

default:
cout<<"Incorrect choice,enter again\n\n";
break;
}
}
return 0;
}

6. Output: Image of sample output to be attached here:


Learning outcomes (What I have learnt):

1. Pop operation on stack.

2. Push operation on stack.

3. Basics of stacks.
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

You might also like