Stack
Stack
stack
• The items into the stacks are stored in a sequential order from the first
location of the memory block
• A pointer TOP contains the location of the TOP element of the stack
• Similarly you cannot remove element beyond the base of the stack . If
such is the case . We will reach a stack underflow condition
TOP 7 1
TOP 6 19
TOP 5 15
TOP 4 18
TOP 3 2
TOP 2 7
TOP 1 12
TOP 0 10
TOP -1
STACK
POP
TOP 7 1
TOP 6 19
TOP 5 15
TOP 4 18
TOP 3 2
TOP 2 7
TOP 1 12
TOP 0 10
TOP -1
STACK
OBSERVATIONS
TERMINOLOGY
❖Overflow: When the stack is full and we try to push an element it is
called Overflow
❖Underflow: When the stack is empty and we try to pop an element it is
called underflow
PUSH OPERATION
➢The new element is added at the top most position of the stack
Push Algorithm
PUSH(STACK , TOP, SIZE, ITEM )
STACK is the array with N elements and TOP is the pointer to the top
element of the array. ITEM the element to be inserted.
#include<conio.h>
#include<process.h>
#define max 5
class stack
{
private:
int s[max],top,ele,i;
public:
stack()
{
top=-1;
}
void push() PUSH PROGRAM
{
if(top==(max-1))
{
cout<<“Stack overflow"<<endl;
}
else
{
cout<<“Enter the element to be inserted: "<<endl;
cin>>ele;
top=top+1;
s[top]=ele;
}
}
void display() PUSH PROGRAM
{
if(top==-1)
{
cout<<“Stack underflow"<<endl;
}
else
{
cout<<“The contents of the stack are "<<endl;
for(i=top;i>=0;i--)
cout<<s[i]<<endl;
}
}
}; // End of class
void main() PUSH PROGRAM
{
stack st;
int choice;
clrscr();
while(1)
{
cout<<endl<<" ------------------------------------------- "<<endl;
cout<<"1: Push"<<endl;
cout<<"2: Display"<<endl;
cout<<"3: Exit"<<endl;
cout<<“Enter your choice"<<endl;
cin>>choice;
switch(choice) PUSH PROGRAM
{
case 1: st.push();
break;
case 2: st.display();
break;
case 3: cout<<"thank you";
getch();
exit(0);
default: cout<<"invalid input!! try again"<<endl;
}
} // while loop
} // void main()
POP ALGORITHM
#include<conio.h>
#include<process.h>
#define max 5
class stack
{
private:
int s[max],top,ele,i;
public:
stack()
{
top=-1;
}
void push() POP PROGRAM
{
if(top==(max-1))
{
cout<<“Stack overflow"<<endl;
}
else
{
cout<<“Enter the element to be inserted: "<<endl;
cin>>ele;
top=top+1;
s[top]=ele;
}
}
void pop() POP PROGRAM
{
if(top==-1)
{
cout<<“Stack underflow"<<endl;
}
else
{
ele=s[top];
cout<<“The popped element is: "<<ele<<endl;
top=top-1;
}
}
void display() POP PROGRAM
{
if(top==-1)
{
cout<<“Stack underflow"<<endl;
}
else
{
cout<<“The contents of the stack are "<<endl;
for(i=top;i>=0;i--)
cout<<s[i]<<endl;
}
}
};
void main() POP PROGRAM
{
stack st;
int choice;
clrscr();
while(1)
{
cout<<endl<<" ------------------------------------------- "<<endl;
cout<<"1: Push"<<endl;
cout<<"2: Pop"<<endl;
cout<<"3: Display"<<endl;
cout<<"4: Exit"<<endl;
cout<<“Enter your choice"<<endl;
cin>>choice;
switch(choice) POP PROGRAM
{
case 1: st.push();
break;
case 2: st.pop();
break;
case 3: st.display();
break;
case 4: cout<<"thank you";
getch();
exit(0);
default: cout<<"invalid input!! try again"<<endl;
}
}
}
Operations performed on stacks
➢ stack(): It creates a new stack that is empty and it needs no parameters
and returns an empty stack
➢ peek(): It Returns the top item from the stack but does not remove it
I II
➢ Reverse of data
You push a given word to stack - letter by letter - and then pop letters from
the stack.
P 3 POP
PUSH
M 2 M
O 1 O
C 0 C
STACK STACK
➢ Processing Function :
Main Function can only be finished after Function 1 and Function 1 can only
be finished after Function 2. As a result Main Function should begin first and
finished last . Which follows the last in first out behaviour
PUSHING
conversion : 14 / 2
ORDER
2 14 0 1 POP ORDER
2 7 1 1
2 3 1 1
2 1 1 0
0
STACK
➢ Backtracking: This is a process when you need to access the most
recent data element in a series of elements
❖ Undo/redo- mechanism in text editors; this operation is
accomplished by keeping all text changes in a stack
❖ Processing Function calls
❖ support for recursion
➢ Delimiter checking:
Compiler’s syntax check for matching braces is implemented by using
stack.
➢ Memory Managment:
Space for parameters and local variables is created internally using a
stack.
Arithmetic expression
Example: ab +
Example: +ab