0% found this document useful (0 votes)
9 views4 pages

Lab#6 - Stack ADT

The document describes the Stack Abstract Data Type (ADT), which is a Last-In-First-Out (LIFO) data structure. It outlines basic terminology, operations (push, pop, display, and top), and provides algorithms for each operation along with a C++ implementation example. Key concepts include stack overflow, underflow, and maintaining a stack pointer to track the current position.

Uploaded by

cxgr8vmy6p
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)
9 views4 pages

Lab#6 - Stack ADT

The document describes the Stack Abstract Data Type (ADT), which is a Last-In-First-Out (LIFO) data structure. It outlines basic terminology, operations (push, pop, display, and top), and provides algorithms for each operation along with a C++ implementation example. Key concepts include stack overflow, underflow, and maintaining a stack pointer to track the current position.

Uploaded by

cxgr8vmy6p
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/ 4

Stack ADT

STACK: A stack is an ordered collection of data items into which new items may be inserted
and from which data items may be deleted at one end. Stacks are also called Last- In-First-out
(LIFO) lists.

Representation of a Stack

Basic terminology associated with stacks:


1) Stack Pointer (TOP): Keeps track of the current position of the stack.
2) Overflow: Occurs when we try to insert (push) more information on a stack than it can
hold.
3) Underflow: Occurs when we try to delete (pop) an item off a stack, which is empty.

Basic Operation Associated with Stacks:


1) Insert (Push) an item into the stack.
2) Delete (Pop) an item from the stack.
a) Algorithm For Inserting an Item into the Stack S:
Procedure PUSH()
Step 1: {Check for stack overflow} If TOP==MAXSIZE then
Prints(‘Stack overflow’)
Return else
Step 2: {Increment pointer top} TOP=TOP+1
Step 3: {Insert ITEM at top of the Stack} S[TOP]=ITEM
Return

b) Algorithm for Deletion of an Item from the Stack S


Procedure POP()
Step 1: {Check for stack underflow} If TOP==0 then
Prints(‘Stack underflow’) Return
Step 2: {Return former top element of stack} ITEM=(S[TOP]);
Step 3: {Decrement pointer TOP} TOP=TOP-1
Prints(‘Deleted item is:’, item);
Return

c) Algorithm to display the items of a Stack S


Procedure DISPLAY()
Step 1: {Check for stack underflow} If TOP==0 then
Prints(‘stack is empty’) Return
Step 2: {display stack elements until TOP value}
Prints(S[TOP])
TOP=TOP+1

d) Algorithm to display top item of the Stack S


Procedure TOP()
Step 1: {Check for stack underflow} If TOP=0 then
Prints(‘stack is empty’) Return
Step 2: {display TOP value into the Stack} Prints(S[TOP])
Example:
Write C++ programs to implement Stack ADT data structure using arrays.

#include<iostream>
using namespace std;
#define MAX 10
int top=-1,ch,i;
int stk[MAX], ele;

void Push()
{
if(top==(MAX-1))
cout<<"\n\nThe stack is full";
else
{
cout<<"\n\nEnter an element:";
cin>>ele;
top++;
stk[top]=ele;
cout<<"\n\nElement pushed successfully\n";
}
}

void Pop()
{
if(top==-1)
cout<<"\n\nThe stack is empty";
else
{
ele=stk[top];
top--;
cout<<"\n\nThe deleted element is:"<<ele;
}
}

void Top()
{
if(top==-1)
cout<<"\n\nThe stack is empty";
else
cout<<"The top element of the stack is:"<<stk[top];
}
void Display()
{
if(top==-1)
cout<<"\n\nThe stack is empty";
else
{
cout<<"\n\nThe elements in the stack are:";
for(i=top;i>=0;i--)
cout<<"\n"<<stk[i];
}
}

int main()
{
bool flag=true;
do
{
cout<<"\n****MENU****";
cout<<"\n1. Push\n2. Pop\n3. Top\n4. Display\n5. Exit";
cout<<"\nEnterur Choice:";
cin>>ch;

switch(ch)
{
case 1: Push();
break;
case 2: Pop();
break;
case 3: Top();
break;
case 4: Display();
break;
case 5: flag=false;
default: cout<<"Enter correct Choice";
}
}while(flag);
}

You might also like