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

Lab 5

Uploaded by

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

Lab 5

Uploaded by

qadirbhaiop
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab 5

Name: Abdul Qadir Khan


CMS:1318-2023
Semester:3rd
Course: DSA
Q1

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

top = -1;
cout << "\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT\n\t 5.Top\n\t"<<endl;
do
{
cout << "Enter the choice:" << endl;
cin >> choice;
switch (choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
cout << "EXIT POINT";
break;
}
case 5:
{
Top();
break;
}
default:
{
cout << "Please Enter a Valid Choice(1/2/3/4)"<<endl;
}
}
} while (choice != 4);
return 0;
}
void push()
{
if (top >= 4)
{
cout << "STACK is over flow";
}
else
{
cout << "Enter a value to be pushed:";
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 is STACK" << endl;
for (i = top; i >= 0; i--)
cout << stack[i] << endl;
}
else
{
cout << "The STACK is empty";
}
}
void Top()
{
if (top==-1)
cout<<"top is empty"<<endl;
else
cout<<"top value is:"<<stack[top]<<endl;
}

Q2

#include <iostream>
using namespace std;
int top=1;
int stack[7];
void push(int stack[],int item)
{
if(top>7)
{
cout<<"Stack is Over Flow";
}
else
{
top=top+2;
stack[top]=item;
}
return;
}
void pop(int stack[])
{
for(int i=0;i<1;i++)
{
if(top==0)
{
cout<<"Stack Is Uder Flow";
break;
}
else
{
stack[top]=stack[top+1];
top=top-1;
}
}
return;
}
void Top()
{
if(top==-1)
cout<<"Stack is overflow"<<endl;
else
cout<<"Topped value:"<<stack[top]<<endl;
}
int main()
{
push(stack,5);
push(stack,2);
push(stack,3);
pop(stack);
push(stack,6);
push(stack,9);
push(stack,5);
Top();
cout<<"Stack Value is:"<<endl;
while (top!=0)
{
if(stack[top]!=0)
{
cout<<" "<<stack[top]<<endl;
}
pop(stack);
}
return 0;
}

You might also like