Fds U 3 Stack 1 1
Fds U 3 Stack 1 1
• Push
• Pop
• Stack Top
E top
D top D D
C top C C C
B top B B B B
A top A A A A A
Primitive Operations on
Stack
Push
the operation to place a new item at the
top of the stack
Pop
the operation to remove the next item
from the top of the stack
Primitive Operations on
Stack
Empty()
To determine if a stack is empty or not.
Full
To determine if a stack is full or not.
Data Structures: A Pseudocode Approach with C 7
Data Structures: A Pseudocode Approach with C 8
Data Structures: A Pseudocode Approach with C 9
Push
void push(int item)
{
if (top !=SIZE-1)
{
top++; //top incremented by 1
stack[top] = item; //Insert the element from stack
}
Pop
int pop( int top)
{
if (top == -1)
printf(“\nstack is empty”);
else
{
top--;
item=stack[top];
}//end else
return(item);
}
Full()
int full(int stack[10],int top)
{
if(top==MAX-1)
{
return(1);
}
return 0;
}
Empty()
int empty(int stack[10],int top)
{
if(top==-1)
{
return(1);
}
return 0;
}
Initialize()
Void initialize(int stack[10],int top)
{
top=-1;
}
Implementing a Stack
At least three different ways to
implement a stack
array
vector
linked list
Which method to use depends on the
application
what advantages and disadvantages
does each implementation have?
Implementing Stacks: Array
Advantages
best performance
Disadvantage
fixed size
Basic implementation
initially empty array
field to record where the next data gets placed
into
if array is full, push() returns false
otherwise adds it into the correct spot
if array is empty, pop() returns null
otherwise removes the next item in the stack
Stack Applications
Real life
Pile of books
Plate trays
More applications related to
computer science
Program execution stack
Expression evaluation-infix to
postfix,infix to prefix
Parsing
Simulation of Recursion
Function call
Data Structures: A Pseudocode Approach with C 18
Representation of two stacks in an array
Stack1 Stack 2
Top1 Top2
• Data Structure
• ADT Implemenation