Stack


 What is Stack?

Stack is a linear data structure which ids used to store in LIFO order (LAST IN FIRST OUT)
In this data structure insertion and deletion are performed only one position called TOP.
its main objective to evaluate mathematical expression in different notation as prefix, postfix and infix. These are called polish notation. It is a big application of stack .It is also used in different algorithm as Tower of Hanoi, traversal of tree and recursive algorithm.
Operation on stack
PUSH-operation:- to insert element in stack.
POP-operation:- to delete element in stack.
Overflow:- full stack.
Underflow:- empty stack.


Representation of stack using array.

In this representation we use one array variable which hold the value of stack and other variable is top that hold the index value.
If stack is full/overflow then top must be insilized with (size-1). A stack is called underflow if top is assigned with -1 . in this case we can store limited element in stack due to fixed size of array but we can avoid  with the help of linked list representation.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
 void push();
 void pop();
 void display();

 int stack1[50],size1,top=-1;
 int main()
 {
     int choice;
     printf("Enter the size of stack-");
     scanf("%d",&size1);
     do
     {
         system("cls");
         printf("====SELECT YOUR CHOICE=====\n press 1 for push-\n press 2 for display the stack-\n press 3 for pop-----\n  enter choice=");
         scanf("%d",&choice);
         switch(choice)
         {
         case 1:
            push(); break;
         case 2:
            display(); break;
         case 3:
            pop(); break;
         default:
            printf("invalid choice!");

         }
     }
     while(choice>=1||choice<=3);

 }

 void push()
 {
     int element;
     if(top==size1-1)
        printf("stack is over flow");
     else
     {
         printf("enter the value of stack");
         scanf("%d",&element);
         top=top+1;
         stack1[top]=element;
     }
          system("pause");

 }


 void display()
 {
     int i;
     if(top==-1)
        printf(" stack is underflow");
     else{
        printf("\t===All elements are===\n");
        for(i=top;i>=0;i--)
        {
            printf("\t\t%d\n",stack1[i]);
        }

     }
     system("pause");
 }
 void pop()
 {
     if(top==-1)
        printf("stack is unserflow");
     else
     {

         printf("deleted recode=%d",stack1[top]);
         top=top-1;
     }
          system("pause");
 }





Representation of stack using linked list.

In this representation we can store unlimited element in stack because linked list represent dynamic memory allocation. In this case the concept of overflow will not accrued with NULL.


 #include<stdio.h>

#include<conio.h>
#include<process.h>
#include<stdlib.h>

struct node
{
    int info;
    struct node *link;

};
struct node *top=NULL;
void push();
void display();
void pop();


 int main()
 {
     int choice;
     do
     {
         system("cls");
         printf("====SELECT YOUR CHOICE=====\n press 1 for push-\n press 2 for display the stack-\n press 3 for pop-----\n  enter choice=");
         scanf("%d",&choice);
         switch(choice)
         {
         case 1:
            push(); break;
         case 2:
            display(); break;
         case 3:
            pop(); break;
         default:
            printf("invalid choice!");

         }
     }
     while(choice>=1||choice<=3);

 }

 void push()
 {
     struct node *new1;
     new1=(struct node*)malloc(sizeof(struct node));
     printf("enter the value of node");
     scanf("%d",&new1->info);
     new1->link=NULL;
     if(top==NULL)
        top=new1;
     else{
        new1->link=top;
        top=new1;
     }
 }

 void display()
 {
     struct node *temp=top;
     if(top==NULL)
        printf("stack is under flow");
     else{
        printf("All element of stack\n");
        while(temp!=NULL)
        {
            printf("%d\t",temp->info);
            temp=temp->link;
        }
     }
  system("pause");
 }

 void pop()
 {
     if(top==NULL)
        printf("Stack is under flow\n");
     else{
        printf("Deleted element=%d",top->info);
        top=top->link;
     }

 }

Output










I hope this post is useful for you.
plzzz like and follow my facebook page.

Comments

Popular posts from this blog

python pattern programming

What is function?What is function in python?