Learn implementation of Stack
Implementation of stack
Stack is the leaner data structure with follows the order FILO (First In Last Out ) Or LIFO(Last In First Out)
The element can be added or removed only one end of the end usually The insertion operation is called push operation and removing operation is called pop operation
How to understand stack properly :
There is many examples on stack lets take container witch carry 3 elements on it lets push the elements one after the other .the first elements will be at bottom of the stack in pop operation the upper most element should be removed
The following orations can be done in stack
- Push operation (inserting the element on stack)
- Pop operation (Deletion of element from stack)
Implementing stack operation
In the stack the pointer variable called top is used to point the top most element of the stack .
the top pointer is initialized by -1 to say that the stack is empty
if the stack pointer is at the position -1 then it is said to be stack is empty.
Push operation :
To insert element in the stack first check whether the stack is full condition
if(*top==size-1)
printf("Stack is empty \n");
If the stack is full print stack is full
else increment the top pointer and assign the element to the stack pointing by the top pointer
void push(int element , int *top , int stack[])
{
if(*top==size-1)
{
printf("Stack is full\n");
return;
}
*top=*top+1;
stack[*top]=item;
}
Pop operation :
To delete the element from the stack first we should check whether the stack is empty
if(*top==-1)
printf("Stack is empty\n");
If the stack pointer top is pointing to -1 then it is said that stack is empty
if the stack is empty the print stack is empty
else
access the element of the stack pointing at the top pointer and decrement the top pointer
int pop(int *top , int stack[])
{
int
item_deleted;
if(*top==-1)
printf("Stack is empty\n");
else{
item_deleted=s[(*top)--];
return
item_deleted;
}
}
To display the stack :
To display the elements of the stack we should check whether the stack contains any element on it if the the stack top pointer is at position -1 then print stack is full
if stack contains elements then we can print it .take the top as call by value or assign the value of top pointer to any variable print the stack by decrementing the variable or top value NOTE if the top variable is declared globally then decrementing of top will be effected .
void display(int top,int stack[])
{
if(top==-1)
{
printf("Stack
is empty\n");
return;
}
printf("Contents
of stack are\n");
for(i=0;i<=top;i++)
{
printf("%d\t",stack[i]);
}
}
C Program to Implementing stack using arrays
#include<stdio.h>
#include<conio.h>
#define size 5
void push(int element , int *top , int stack[])
{
if(*top==size-1)
{
printf("Stack
overflow\n");
return;
}
*top=*top+1;
stack[*top]=element;
}
int pop(int *top , int stack[])
{
int
item_deleted;
if(*top==-1)
return
0;
item_deleted=s[(*top)--];
return
item_deleted;
}
void display(int top , int stack[])
{
if(top==-1)
{
printf("Stack
is empty\n");
return;
}
printf("Contents
of stack are\n");
for(i=0;i<=top;i++)
{
printf("%d\t",stack[i]);
}
}
void main()
{
int
top=-1,stack[10],item_deleted,item,ch;
clrscr();
for(;;)
{
printf("1
: Push\t2 : Pop\n3 : Display\t4 : Exit\n");
scanf("%d",&ch);
switch(ch)
{
case
1:
printf("Enter
the item to be inserted\n");
scanf("%d",&item); push(item,&top,stack);
break;
case
2:
item_deleted=pop(&top,stack);
if(item_deleted==0)
printf("Stack
is empty\n");
else
printf("Item
deleted =%d\n",item_deleted);
break;
case
3:
display(top,stack);
break;
default:
exit(0);
}
}
}
Output:
1 : Push 2
: Pop
3 : Display 4
: Exit
1
Enter the item to be inserted
10
1 : Push 2
: Pop
3 : Display 4
: Exit 1
Enter the item to be inserted
20
1 : Push 2
: Pop
3 : Display 4
: Exit
1
Enter the item to be inserted
30
1 : Push 2
: Pop
3 : Display 4
: Exit
1
Enter the item to be inserted
40
1 : Push 2
: Pop
3 : Display 4
: Exit
1
Enter the item to be inserted
50
1 : Push 2
: Pop
3 : Display 4
: Exit
3
Contents of stack are
10 20 30 40 50
1 : Push 2
: Pop
3 : Display 4
: Exit
In my next post im going to Post the circular Stack
Comment below for any queries

Comments
Post a Comment