19 Aug 2019

  • August 19, 2019
  • Amitraj
Implementation of Stack Data Structure


* Stack :-  stack is a linear data structure in which elements are inserted and deleted from only one end known as 'top' of the stack.

1.) stack follows a principle last in first out (LIFO). The element which came in the stack last will be deleted first.
2.) push :- Inserting a element into a stack is  known as push operation.
3.) pop :- Deleting elements from a stack is known as pop operation.
4.) underflow :- when we try to pop element from a stack, when the stack is already empty. we will get stack underflow.
5.) overflow :-  when we try to push an element into a stack, when the stack is already full. we will get stack overflow.


* Application of  stack/ usages of stack:-


1) stack is used in Recursion.
2) stack is used to evaluate post fix and infix expression.
3) many compiler use a stack for parsing the syntax of expressions, program block etc. before translating into low level code.
4) stack is used to reverse a string. we push the characters of string one by one into stack and the pop character from stack.
5) In DFS(Depth first search) stack is used.



/* C program to implement stack */         

#include<stdio.h>
#define SIZE 5
int a[SIZE],top=-1;
void push()
{
if(top==SIZE-1)
{
printf("\n Stack overflow");
}
else
  {
  printf("Enter a no. to push:");
  scanf("%d", &a[++top]);
  printf("Element pushed into stack:\n\n");
  }
}
void pop()
{
if(top==-1)
{
printf("\n Stack underflow");
}
else
{
top--;
printf("Element popped into stack is:%d", a[top+1]);
}
}
void show()
{
int i;
if(top==-1)
{
printf("Stack is empty:\n");
}
else

for(i=0; i<=top; i++)
printf("%d", a[i]);

}
int main()
{
int ch;

do
{
printf(":::Stack operation:::\n");
printf("1.push 2.pop 3.show 4.exit\n");
printf("Enter your choice:");
scanf("%d", &ch);
switch(ch)
{
case 1: push(); break;
case 2: pop(); break;
case 3: show(); break;
case 4: printf("Invalid choice..");
}
}while(ch!=4);
return 0;
}

//INPUT/OUTPUT:-


:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:1
Enter a no. to push:56
Element pushed into stack:

:::Stack operation:::

1.push 2.pop 3.show 4.exit
Enter your choice:1
Enter a no. to push:85
Element pushed into stack:

:::Stack operation:::

1.push 2.pop 3.show 4.exit
Enter your choice:1
Enter a no. to push:33
Element pushed into stack:

:::Stack operation:::

1.push 2.pop 3.show 4.exit
Enter your choice:1
Enter a no. to push:55
Element pushed into stack:

:::Stack operation:::

1.push 2.pop 3.show 4.exit
Enter your choice:1
Enter a no. to push:89
Element pushed into stack:

:::Stack operation:::

1.push 2.pop 3.show 4.exit
Enter your choice:1

 Stack overflow:::Stack operation:::

1.push 2.pop 3.show 4.exit
Enter your choice:56
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:1

 Stack overflow:::Stack operation:::

1.push 2.pop 3.show 4.exit
Enter your choice:3
5685335589:::Stack operation:::
1.push 2.pop 3.show 4.exit

Enter your choice:2

Element popped into stack is:89:::Stack operation:::
1.push 2.pop 3.show 4.exit

Enter your choice:2

Element popped into stack is:55:::Stack operation:::
1.push 2.pop 3.show 4.exit

Enter your choice:2

Element popped into stack is:33:::Stack operation:::
1.push 2.pop 3.show 4.exit

Enter your choice:2

Element popped into stack is:85:::Stack operation:::
1.push 2.pop 3.show 4.exit

Enter your choice:2

Element popped into stack is:56:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:2

 Stack underflow:::Stack operation:::

1.push 2.pop 3.show 4.exit
Enter your choice:2

 Stack underflow:::Stack operation:::

1.push 2.pop 3.show 4.exit

Enter your choice:3

Stack is empty:
:::Stack operation:::
1.push 2.pop 3.show 4.exit

Enter your choice:4

Invalid choice..








Translate

Popular Posts