Follow This Blog For more... 😊

What is stack? | Implement the stack with push, pop, peep, change & display function. | using C language | Data Structure |

🤔What is a stack?

👉 A stack is a linear data structure in which all the insertion(Push) and deletion(Pop) of data or you can say its values are done at one end only, rather than in the middle. 

  • Stack follows the ' LIFO ' manner which is Last In First Out which means that the element which the last inserted (using push) that delete first (using pop).
  • stack has one pointer which always points to the last element of the stack which is called a 'TOP Pointer'.
  • Stacks can be implemented by using arrays of type linear.

👉Functions of stack

  • Push ⇨ Used to insert an element into the stack at one end only (From the top of the stack)
  • Pop Used to delete elements from the stack at one end only (From the top of the stack)
  • Peep Used to reach the element of a particular position. (From the bottom of the stack)
  • Change Used to reach the element of a particular position and change its value. (position from the bottom of the stack)

👨‍💻Implement the stack with a push, pop, peep, change & display function. (Using C language)

NOTE: FIRST UNDERSTAND THE FUNDAMENTALS OF STACK AND TRY TO SOLVE YOURSELF, THEN SEE THE CODE. (Which helps improve your logic-building skills)

CODE :

   
    /* Implement the stack with push, pop, peep, change & display function */

    #include<stdio.h>
    #include<stdlib.h>

    #define N 50
    void push();
    void pop();
    void peep();
    void change();
    void display();
    void choise_list();
    int top=-1;
    int stack[N];

    main()
    {
        int select;
        choise_list();
        scanf("%d",&select);
        while(1)
        {
            switch(select)
            {
                case 0:{exit(0);}break;
                case 1:{push();}break;
                case 2:{pop();}break;
                case 3:{peep();}break;
                case 4:{change();}break;
                case 5:{display();}break;
            }
            choise_list();
            scanf("%d",&select);
        }
    }

    void push()
    {
        int data;
        if(top>N-1)
        {
            printf("Stack is Overflow !!\nSo you can not insert any data in stack");
        }
        else
        {
            printf("--> Enter data:");
            scanf("%d",&data);
            top++;
            stack[top]=data;
        }
    }

    void pop()
    {
        if(top==-1)
        {
            printf("\nStack is underflow !!\nSo you can not delelte\n");
        }
        else
        {
            printf("\nelement at %d position is %d deleted sucssefully",top,stack[top]);
            stack[top]='\0';
            top--;
        }
    }

    void peep()
    {
        int n;
        printf("At which position`s element`s data you want to see :");
        scanf("%d",&n);
        if(n<=top+1)
        {
            printf("--> At position %d element`s data is %d\n",n,stack[n-1]);
        }
        else
        {
            printf("But elements are upto position %d\n",top+1);
        }  
    }

    void change()
    {
        int n,newdata,temp;
        printf("At which position`s element`s data you want to change :");
        scanf("%d",&n);
        if(n<=top+1)
        {
            temp=stack[n-1];
            printf("--> At position %d element`s current data is %d\n",n,stack[n-1]);
            printf("Enter element`s new data:");
            scanf("%d",&newdata);
            stack[n-1]=newdata;
            printf("\nYou successfully changed old data:%d to new data:%d at position %d\n",temp,stack[n-1],n);
        }
        else
        {
            printf("But elements are upto position %d\n",top+1);
        }
    }


    void display()
    {
        int i;
        for(i=top;i>=0;i--)
        {
            if(i==top)
            printf("\nstack[%d]=| %d |<-- position %d <-- top pointer",i,stack[i],i+1);
            else
            printf("\nstack[%d]=| %d |<-- position %d",i,stack[i],i+1);
        }
        printf("\n");
    }
    void choise_list()
    {
        printf("\nOperation:");
        printf("\n0.Exit\n1.Push\n2.Pop\n3.Peep\n4.change\n5.Display\nyour option:");
    }

OUTPUT:




Comments

Popular Posts