Stack
Stack
Stacks
www.asyrani.com
This is stacks
A data structure in
which the
elements are
added and
removed from the
top only; a Last In
First Out (LIFO)
data structure.
Why People use stacks?
Say you are writing a notepad application.
A good use for a stack would be the
undo/redo feature. Every time the user
enters something, save the state (in this
case, the text) on a stack and if you need
to reverse something, just pop it off the
top of the undo stack and push it onto the
redo stack.
top
top E
D D D
C top C C C
B top B B B B
A A A A A
A top
Stacks
Advantages Disadvantages
Insertion/
deletion Easy to
from one Advantages implement
place
Disadvantages
Problem
when
Recursive accessing
Fast Function “the one
that on the
operation top”
BASIC OPERATION
• InitializeStacks – initializes
the stack to an empty state
• DestroyStack – removes all
the elements from the
state
• IsEmptyStack – Check the
stack whether is empty or
BASIC not
OPERATION • IsFullStack – Check
whether the stack is full or
not
Push and Pop
top = 6
4
top++
2
12
- top = 6
(before this top is equal to 5, top = 5)
32
54
5
Example push(40)
if(top<MAX)
top = 6 {
4
2
arr[top]=a;
12 }
32 - Yes, top is still lower than
54
5
MAX which is 10.
- So, arr[6] = 40
(since we receive a = 40)
Example push(40)
212 top = 9
21
else
23
{
40 cout<<"STACK
4 FULL!!"<<endl;
2 top--;
12 }
- But if top is equal to 10 (already full after top++),
32
then we put message STACK FULL, and set it back
54 to top--
5
Let us Implement Stacks
If it is not
- We give any topmost data inside arr[top] to local
variable int data
- Then, we set the arr[top] = NULL since we actually
remove the data
- We set back the top to top--. Example if top = 5, then
top now is 4.
- We will return the int data
Example (pop)
5 top = 7
64
4
int data = arr[top]
2
12 - int data = arr[7]=5
32
54
5
Example (pop)
top = 7
64
4
arr[top]= NULL
2
12 - arr[7] = NOTHING
32 INSIDE
54
5
Example (pop)
64 top = 7
4
top--
2
12 - Top = 6
32
54
5
Example (pop)
64 top = 7
4
return data
2
12 - We return data = 5 to
32 any function that call
54
or want to know what
5
value that the push has
throw away.
FULL SOURCE CODE
#include <iostream>
class stack
{
private:
int arr[MAX]; // Contains all the Data
int top; //Contains location of Topmost Data pushed onto Stack
public:
stack() //Constructor
{
top=-1; //Sets the Top Location to -1 indicating an empty stack
}
void push(int a) // Push ie. Add Value Function
{
top++; // increment to by 1
if(top<MAX)
{
arr[top]=a; //If Stack is Vacant store Value in Array
}
else
{
cout<<"STACK FULL!!"<<endl;
top--;
}
}
int pop() // Delete Item. Returns the deleted item
{
if(top==-1)
{
cout<<"STACK IS EMPTY!!!"<<endl;
return NULL;
}
else
{
int data=arr[top]; //Set Topmost Value in data
arr[top]=NULL; //Set Original Location to NULL
top--; // Decrement top by 1
return data; // Return deleted item
}
}
};
int main()
{
stack a;
a.push(3);
cout<<"3 is Pushed\n";
a.push(10);
cout<<"10 is Pushed\n";
a.push(1);
cout<<"1 is Pushed\n\n";
cout<<a.pop()<<" is Popped\n";
cout<<a.pop()<<" is Popped\n";
cout<<a.pop()<<" is Popped\n";
return 0;
}
APPLICATION
ALGORITHM CONVERTING DECIMAL
TO BINARY
How it works
• Let say you have a number: 148
Is 148 >= 128? Yes, so the 128 bit must be 1. 148 – 128 =
20, which means we need to find bits worth 20 more.
Display purpose
Source
• http://www.learncpp.com/cpp-tutorial/37-
converting-between-binary-and-decimal/
CONVERTING INFIX TO POSTFIX AND PREFIX
NOTATION
What is…
• Infix is
A*B
• Postfix is
AB*
• Prefix is
*AB
Infix to Postfix
• Let use an example
Infix to postfix Process
a
Postfix String
a
Postfix String
ab
Postfix String
ab
Postfix String
*
+ Then, we found “ * ”. We put
Our Stack
into Our Stack
Infix to postfix Process
abc
Postfix String
*
+ We found “C”. So put into
Our Stack
Postfix String
Infix to postfix Process
abc*
Postfix String
-
+ Next character scanned is '-'. The topmost character in
the stack is '*' which has a higher precedence than '-'.
Thus '*' will be popped out from the stack and added
Our Stack
to the Postfix string.
Infix to postfix Process
abc*+
Postfix String
abc*+e
Postfix String
abc*+e-
Postfix String
Convert function
Our Class items