Stack: Prepared By: Eng. Ahmed & Mohamed Taha
Stack: Prepared By: Eng. Ahmed & Mohamed Taha
Prepared by:
Agenda
Introduction to Stack.
How Stack works.
Operations performed on Stack.
Stack Implementation.
Introduction to stack
All the addition and deletion in a stack is done from the top
of the stack, the last added element will be first removed
from the stack. That is why the stack is also called Last-inFirst-out (LIFO).
The insertion (or addition) operation is referred to as push.
The deletion (or remove) operation as pop.
A stack is said to be empty or underflow, if the stack
contains no elements. At this point the top of the stack is
present at the bottom of the stack.
A stack is overflow when it becomes full, i.e., no other
elements can be pushed onto the stack. At this point the
top pointer is at the highest location of the stack.
Introduction to Stack
Company Logo
Stack Working
Company Logo
Stack Implementation
Using
Address
Using
Pointers
Stack Implementation
Using
Address
Using
Pointers
Stack Implementation
#include <iostream.h>
#include <conio.h>
using address
Stack Implementation
/////////////////////////////funcions
// Initialize Function
void initialize(stack &s)
{
s.top=-1;
}
// Is_full Function
bolean is_full(stack &s)
{
if (s.top==max_size-1)
return TRUE;
return FALSE;
}
// Is_empty Function
bolean is_empty(stack &s)
{
if (s.top==-1)
return TRUE;
return FALSE;
}
using address
Stack Implementation
using address
// Push Function
void push(stack &s,int item)
{
if (is_full(s))
{
cout<<"Stack is overflow";
return;
}
// Pop Function
s.top++;
int pop(stack &s)
s.items[s.top]=item;
{
}
if (is_empty(s))
{
cout<<"Stack is underflow";
return -1;
}
int item=s.items[s.top];
s.top--;
return item;
}
Stack Implementation
// Print all elements Funcion
void print_all_elements(stack &s)
{
cout<<"the current items of the stack from top to bottom are:"<<endl;
for(int i=s.top;i>=0;i--)
cout<<s.items[i]<<endl;
// Stacktop Funcion
int stacktop(stack &s)
{
if (is_empty(s))
{
cout<<"Stack is underflow";
return -1;
}
return s.items[s.top];
}
using address
Stack Implementation
void main()
{
clrscr();
stack s;
initialize(s);
push(s,1);
push(s,2);
push(s,3);
push(s,4);
print_all_elements(s);
int val=pop(s);
print_all_elements(s);
//cout<<val;
getch();
}
using address
Stack Implementation
Using
Address
Using
Pointers
Stack Implementation
#include <iostream.h>
#include <conio.h>
using pointers
Stack Implementation
/////////////////////////////funcions
// Initialize Function
void initialize(stack *s)
{
s->top=-1;
}
// Is_empty Function
bolean is_empty(stack *s)
{
if (s->top==-1)
return TRUE;
return FALSE;
}
// Is_full Function
bolean is_full(stack *s)
{
if (s->top==max_size-1)
return TRUE;
return FALSE;
}
using pointers
Stack Implementation
using pointers
// Push Function
void push(stack *s,int item)
{
if (is_full(s))
{
cout<<"Stack is overflow";
return;
}
// Pop Function
s->top++;
int pop(stack *s)
s->items[s->top]=item;
{
}
if (is_empty(s))
{
cout<<"Stack is
underflow";
return -1;
}
int item=s->items[s->top];
s->top--;
return item;
}
Stack Implementation
using pointers
// Stacktop Funcion
int stacktop(stack *s)
{
if (is_empty(s))
{
cout<<"Stack is
underflow";
return -1;
}
return s->items[s->top];
}
Stack Implementation
void main()
{
clrscr();
stack s;
initialize(s);
stack *ps;
ps=&s;
push(ps,1);
push(ps,2);
push(ps,3);
push(ps,4);
print_all_elements(ps);
int val=pop(ps);
print_all_elements(ps);
//cout<<val;
getch();
}
using pointers
Thank you !