Stack ADT
Stack ADT
Stack ADT
B.BHUVANESWARAN
ASSISTANT PROFESSOR (SS) / CSE
RAJALAKSHMI ENGINEERING COLLEGE
RAJALAKSHMI NAGAR
THANDALAM
CHENNAI 602 105
Introduction
Top 4 50
40
30
20
10
Push
Top 1 20
Top 0 10 10
Top 2 30
20 Top 1 20
10 10 Top 0 10
Overflow
Underflow
Overflow
Top 4 50
40
30
20
10
Underflow
Top -1
Implementation of Stacks
Array
Linked list
Array Implementation of Stack
int IsFull()
{
if(top == MAX - 1)
return 1;
else
return 0;
}
Routine to whether a Stack is Empty
int IsEmpty()
{
if(top == -1)
return 1;
else
return 0;
}
Routine to Push an Element on to the Stack
void Pop()
{
if(IsEmpty())
printf("Stack Underflow...!\n");
else
{
printf("%d\n", Stack[top]);
top = top - 1;
}
}
Routine to Return Top of Stack
void Top()
{
if(IsEmpty())
printf("Stack Underflow...!\n");
else
printf("%d\n", Stack[top]);
}
Routine to Display Stack Elements
void Display()
{
int i;
if(IsEmpty())
printf("Stack Underflow...!\n");
else
{
for(i = top; i >= 0; i--)
printf("%d\t", Stack[i]);
printf("\n");
}
}
Linked List Implementation of Stack
struct node
{
int Element;
struct node *Next;
}*List = NULL;
int IsEmpty()
{
if(List == NULL)
return 1;
else
return 0;
}
Push
void Push(int e)
{
Stack *NewNode = malloc(sizeof(Stack));
NewNode->Element = e;
if(IsEmpty())
NewNode->Next = NULL;
else
NewNode->Next = List;
List = NewNode;
}
Pop
void Pop()
{
if(IsEmpty())
printf("Stack is Underflow...!\n");
else
{
Stack *TempNode;
TempNode = List;
List = List->Next;
printf("%d\n", TempNode->Element);
free(TempNode);
}
}
Top
void Top()
{
if(IsEmpty())
printf("Stack is Underflow...!\n");
else
printf("%d\n", List->Element);
}
Display
Routine to Display Stack Elements
void Display()
{
if(IsEmpty())
printf("Stack is Underflow...!\n");
else
{
Stack *Position;
Position = List;
while(Position != NULL)
{
printf("%d\t", Position->Element);
Position = Position->Next;
}
printf("\n");
}
}
Queries?
Thanks...!