Professional Documents
Culture Documents
05 Stacks
05 Stacks
Stacks
ARRAY IMPLEMENTATION OF STACKS
int stack[n];
Stacks
ARRAY IMPLEMENTATION OF STACKS
Stacks
ARRAY IMPLEMENTATION OF STACKS
Stacks
ARRAY IMPLEMENTATION OF STACKS
Stacks
OPERATIONS ON STACKS
int stack[MAX_SIZE];
int top;
Stacks
OPERATIONS ON STACKS
Stacks
OPERATIONS ON STACKS
Stacks
OPERATIONS ON STACKS
Stacks
OPERATIONS ON STACKS
• Function to Pop an Item from the Stack (and returns the item popped)
if (stack_empty(*ptr_top) == 1)
cout << "\nThe Stack is Empty!";
else
{
item = stack[*ptr_top];
-- (*ptr_top);
cout << "\nItem Removed from the Stack.";
return (item);
}
}
Stacks
OPERATIONS ON STACKS
Stacks
LINKED LIST IMPLEMENTATION OF STACKS
top 7 3 9
• If top = NULL, then the stack is empty. In using linked lists, there is
no such condition as a full stack. Theoretically, using linked lists
does not put any limit on the number of items the stack can have.
The only limitation is the size of the physical memory of the
machine.
Stacks
LINKED LIST IMPLEMENTATION OF STACKS
top 7 3 9
struct NODE
{
int value;
NODE *next;
};
NODE *top;
Stacks
PUSH OPERATION
top 7 3 9
Stacks
PUSH OPERATION
top 7 3 9
ptr 8
Stacks
POP OPERATION
top 7 3 9
if (top == NULL)
cout << "Stack is empty!";
else
{
item = top -> value;
ptr = top;
top = top -> next;
free(ptr);
}
Stacks
OPERATIONS ON STACKS
struct NODE
{
int value;
NODE *next;
};
NODE *top;
Stacks
OPERATIONS ON STACKS
Stacks
OPERATIONS ON STACKS
Stacks
OPERATIONS ON STACKS
if (stack_empty(*ptr_top)==1)
cout << "\nStack is Empty!";
else
{
item = (*ptr_top)-> value;
ptr = *ptr_top;
*ptr_top = (*ptr_top)->next;
free(ptr);
cout << "\n\nItem Removed from the Stack.";
return (item);
}
}
Stacks
OPERATIONS ON STACKS
Stacks