0% found this document useful (0 votes)
65 views10 pages

2019-Cpe-27 DS&a Lab Manual #07

The document is a lab manual for a data structures and algorithms course. It discusses stacks and their properties such as LIFO principle and restrictions on insertion and deletion only from the top. It provides problems to write programs for inserting an element, deleting an element, printing all elements, and searching an element in a stack. Students are expected to write code for each problem and show output.

Uploaded by

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

2019-Cpe-27 DS&a Lab Manual #07

The document is a lab manual for a data structures and algorithms course. It discusses stacks and their properties such as LIFO principle and restrictions on insertion and deletion only from the top. It provides problems to write programs for inserting an element, deleting an element, printing all elements, and searching an element in a stack. Students are expected to write code for each problem and show output.

Uploaded by

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

Lab # 7 - Data Structures & Algorithms (CPE 222)

Lab Manual # 07

Title: Stack Implementation with applications

Student Name: Muhammad Usama Saghar Class Roll: 2019-cpe-27

Subject Teacher: Dr. Muhammad Waqar Ashraf


Lab Engineer : Engr. Abdul Rehman

LAB POINTS SCORE


Lab Performance [10] 0 1 2 3 4 5
Lab Participation
Lab Activity Completion on the Same Day

Lab Submission[10] 0 1 2 3 4 5
Completeness & Correctness
Required Conclusion & Results

No of Checks
SUB TOTAL
TOTAL SCORE

Course Instructor

Version 1.0 Department of Computer Engineering FE&T BZU Multan


Lab # 7 - Data Structures & Algorithms (CPE 222)
Stack.
Stacks are dynamic data structures that follow the Last In First Out (LIFO) principle. The
last item to be inserted into a stack is the first one to be deleted from it.

For example, you have a stack of trays on a table. The tray at the top of the stack is the first
item to be moved if you require a tray from that stack.

Inserting and deleting elements

Stacks have restrictions on the insertion and deletion of elements. Elements can be inserted or
deleted only from one end of the stack i.e. from the top. The element at the top is called
the top element. The operations of inserting and deleting elements are
called push() and pop() respectively.

When the top element of a stack is deleted, if the stack remains non-empty, then the element
just below the previous top element becomes the new top element of the stack.

For example, in the stack of trays, if you take the tray on the top and do not replace it, then the
second tray automatically becomes the top element (tray) of that stack.

Features of stacks

• Dynamic data structures


• Do not have a fixed size
• Do not consume a fixed amount of memory
• Size of stack changes with each push() and pop() operation.
Each push() and pop() operation increases and decreases the size of the stack by 1,
respectively.

P-1 Write a program to insert an element in Stack


Your Code:
#include<iostream>
using namespace std;
int top=0,ar[10];
int push(int ar[])
{
if(top>10)
cout<<"Overflow Occur\n\n";
else
{
cout<<"Enter data: ";
top++;
cin>>ar[top];
}
}
void show(int ar[])
{
if(top==0)
Version 1.0 Department of Computer Engineering FE&T BZU Multan
Lab # 7 - Data Structures & Algorithms (CPE 222)
{
cout<<"Empty Stack\n";
}
else
{
for(int i=top;i>0;i--)
{
if(i==top)
{
cout<<"\t\t\t|\t|\n";
cout<<"\t\t\t| "<<ar[i]<<" |\n";
cout<<"\t\t\t|_______|\n";
}
else if(i>0)
{
cout<<"\t\t\t|\t|\n";
cout<<"\t\t\t| "<<ar[i]<<" |\n";
cout<<"\t\t\t|_______|\n";
}
}
}
}
int main()
{
int n;
repeat:
cout<<"\nEnter 1 for insert\nEnter 2 for Show\n\nEnter Your Selection:";
cin>>n;
if(n==1)
{
push(ar);
goto repeat;
}
else if(n==2)
{
show(ar);
}
else
{
cout<<"Invalid Selection\n";
}
return 0;
}

Version 1.0 Department of Computer Engineering FE&T BZU Multan


Lab # 7 - Data Structures & Algorithms (CPE 222)

Paste your output here

P-2 Write a program to delete an element in stack


Your Code:
#include<iostream>
using namespace std;
int top=0,ar[5];
int push(int ar[])
{
for(int i=5; i>0; i--)
{
ar[i]=i;
top++;
cout<<"\t\t\t|\t|\n";
cout<<"\t\t\t| "<<ar[i]<<" |\n";
cout<<"\t\t\t|_______|\n";
}
}
int pop(int ar[])
{
if(top!=0)
{
cout<<"Deleted Secessfully\n";
top--;
}
else
cout<<"Underflow Occur\n";
}
void show(int ar[])
{
if(top==0)
{
cout<<"Empty Stack\n";
Version 1.0 Department of Computer Engineering FE&T BZU Multan
Lab # 7 - Data Structures & Algorithms (CPE 222)
}
else
{
for(int i=top;i>0;i--)
{
if(i==top)
{
cout<<"\t\t\t|\t|\n";
cout<<"\t\t\t| "<<ar[i]<<" |\n";
cout<<"\t\t\t|_______|\n";
}
else if(i>0)
{
cout<<"\t\t\t|\t|\n";
cout<<"\t\t\t| "<<ar[i]<<" |\n";
cout<<"\t\t\t|_______|\n";
}
}
}
}
int main()
{

int n;
cout<<"Given Stack\n";
push(ar);
repeat:
cout<<"\nEnter 1 for delete\nEnter 2 for Show\n\nEnter Your Selection: ";
cin>>n;
if(n==1)
{
pop(ar);
goto repeat;
}
else if(n==2)
{
show(ar);
}
else
{
cout<<"Invalid Selection\n";
}
return 0;
}
Paste your output here

Version 1.0 Department of Computer Engineering FE&T BZU Multan


Lab # 7 - Data Structures & Algorithms (CPE 222)

Version 1.0 Department of Computer Engineering FE&T BZU Multan


Lab # 7 - Data Structures & Algorithms (CPE 222)

P-3 Write a program to print all the element of stack


Your Code:
#include<iostream>
using namespace std;
int top=0,ar[4];
int push(int ar[])
{
for(int i=3; i>0; i--)
{
ar[i]=i;
top++;
}
}
void show(int ar[])
{
for(int i=top;i>0;i--)
{
if(i==top)
{
cout<<"\t\t\t|\t|\n";
cout<<"\t\t\t| "<<ar[i]<<" |\n";
cout<<"\t\t\t|_______|\n";
}
else if(i>0)
{
cout<<"\t\t\t|\t|\n";
cout<<"\t\t\t| "<<ar[i]<<" |\n";
cout<<"\t\t\t|_______|\n";
}
}
}
int main()
{
int n;
push(ar);
cout<<"\nEnter 1 for Show\nEnter 2 for Exit\n\nEnter Your Selection: ";
cin>>n;
if(n==1)
{
show(ar);
}
else if(n==2)
{
exit(0);
}
else
{
cout<<"Invalid Selection\n";
}
return 0;
}
Paste your output here
Version 1.0 Department of Computer Engineering FE&T BZU Multan
Lab # 7 - Data Structures & Algorithms (CPE 222)

Version 1.0 Department of Computer Engineering FE&T BZU Multan


Lab # 7 - Data Structures & Algorithms (CPE 222)
P-4 Write a program to search an element in Stack
Your Code:
#include<iostream>
using namespace std;
int top=0,ar[5];
int push(int ar[])
{
for(int i=1; i<4; i++)
{
ar[i]=i;
top++;
}
}
void search(int ar[])
{
int no,i,c=0;
cout<<"\nEnter the value to search: ";
cin>>no;
for(i=top; i>0; i--)
{
if(ar[i]==no)
{
c++;
}
}
if(c==0)
{
cout<<"\nElement not found in Stack";
}
else
{
cout<<"\nElement found in Stack";
}
}
void show(int ar[])
{
for(int i=top;i>0;i--)
{
if(i==top)
{
cout<<"\t\t\t|\t|\n";
cout<<"\t\t\t| "<<ar[i]<<" |\n";
cout<<"\t\t\t|_______|\n";
}
else if(i>0)
{
cout<<"\t\t\t|\t|\n";
cout<<"\t\t\t| "<<ar[i]<<" |\n";
cout<<"\t\t\t|_______|\n";
}
}
}
int main()
{
Version 1.0 Department of Computer Engineering FE&T BZU Multan
Lab # 7 - Data Structures & Algorithms (CPE 222)
int n;
push(ar);
repeat:
cout<<"\nEnter 1 for Show\nEnter 2 for Search\nEnter 3 for Exit\nEnter Your Selection: ";
cin>>n;
if(n==1)
{
show(ar);
goto repeat;
}
else if(n==2)
{
search(ar);
}
else if(n==3)
{
exit(0);
}
else
{
cout<<"Invalid Selection\n";
}
return 0;
}
Paste your output here

Version 1.0 Department of Computer Engineering FE&T BZU Multan

You might also like