Stack Abstract Data Structure with Array Implementation
Stack is a abstract data type which store the elements in a particular order called Last in first out (LIFO), where insertion is called PUSH operation and deletion called POP operation.
The basic operations of stack are:
The basic operations of stack are:
- PUSH insert the element into the stack
- POP deletion of the element from the stack
- PEEK return the TOP item of the stack
Stack implementation can be possible using:
- Linked List
- Array
Array implementation of stack has following advantages and disadvantages:
Advantages: Best Performance
Disadvantage: Fixed Size
Stack Applications
- Compiler (parsing the data between the brackets)
- Operating system (program stack)
- Artificial intelligence(finding the path)
Stack implementation using an array:
- First define the size of the array
- Take a index variable called TOP=-1
- If TOP==MAX then stack is full
- If TOP==-1 stack is empty
- PUSH operation is performed with increment of TOP
- POP operation is performed with TOP decrements
#include
#include
#define MAX 10
int a[MAX];
int top=-1;
void Push()
{
int i;
if(top==MAX)
{
printf("stack is full");
exit(0);
}
else
{
scanf("%d",&i);
top++;
a[top]=i;
printf("insert is sucessful\n");
}
}
void Pop()
{
if(top==-1)
{
printf("stack is empty");
exit(0);
}
else
{
printf("%d\n", a[top]);
top=top-1;
}
}
void display()
{
if(top==-1)
{
printf("stack is empty");
exit(0);
}
else
{
while(top!=-1)
printf("poped all elements are %d\n",a[top--]);
}
}
int main()
{
int n,c;
while(1)
{
printf("1: Push operation into stack\n");
printf("2: POP operation into stack\n");
printf("3: Display all the pop elements\n");
printf("4: exit\n");
printf("enter the choice");
scanf("%d",&n);
switch(n)
{
case 1:
printf("enter the data");
Push();
break;
case 2:
printf("poped elements");
Pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default :
printf("invalid choice");
}
}
}
This code you can get it on below link
https://drive.google.com/open?id=0B2O8tJ9QXc-3bXZNQXA5WDdGLTQ
https://drive.google.com/open?id=0B2O8tJ9QXc-3bXZNQXA5WDdGLTQ
Comments
Post a Comment