6.C Program to implement Stack(Part B)

C Program to implement Stack(Part B)


ALGORITHM :
Step 1: Start
Step 2: Define a constant MAX_SIZE to represent the maximum size of the stack.
Step 3: Declare a stack structure with necessary variables (e.g., top to keep track of the top element, and an array to store elements).
Step 4: Implement the push function to add an item to the top of the stack.
        a. Check if the stack is full (top == MAX_SIZE - 1).
        b. If not full, increment top by 1 and add the item to the stack.
        c. If the stack is full, display an overflow error message.
Step 5: Implement the pop function to remove and return the top item from the stack.
        a. Check if the stack is empty (top == -1).
        b. If not empty, remove the top item from the stack and return it.
        c. If the stack is empty, display an underflow error message or return a special value to indicate the stack is empty (e.g., -1).
Step 6: Implement the peek function to get the top item from the stack without removing it.
        a. Check if the stack is empty (top == -1).
        b. If not empty, return the top item from the stack.
        c. If the stack is empty, display an error message or return a special value to indicate the stack is empty (e.g., -1).
Step 7: Implement the is_empty function to check if the stack is empty.
        a. Check if the stack is empty (top == -1).
        b. If empty, return 1 (true); otherwise, return 0 (false).
Step 8: Implement the is_full function to check if the stack is full.
        a. Check if the stack is full (top == MAX_SIZE - 1).
        b. If full, return 1 (true); otherwise, return 0 (false).
Step 9: End

SOURCE CODE :

#include<stdio.h>

int stack[100],choice,n,top,x,i;

void push(void);

void pop(void);

void display(void);

int main()

{

    //clrscr();

    top=-1;

    printf("\n Enter the size of STACK[MAX=100]:");

    scanf("%d",&n);

    printf("\n\t STACK OPERATIONS USING ARRAY");

    printf("\n\t--------------------------------");

    printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");

    do

    {

        printf("\n Enter the Choice:");

        scanf("%d",&choice);

        switch(choice)

        {

            case 1:

            {

                push();

                break;

            }

            case 2:

            {

                pop();

                break;

            }

            case 3:

            {

                display();

                break;

            }

            case 4:

            {

                printf("\n\t EXIT POINT ");

                break;

            }

            default:

            {

                printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");

            }

                

        }

    }

    while(choice!=4);

    return 0;

}

void push()

{

    if(top>=n-1)

    {

        printf("\n\tSTACK is over flow");

        

    }

    else

    {

        printf(" Enter a value to be pushed:");

        scanf("%d",&x);

        top++;

        stack[top]=x;

    }

}

void pop()

{

    if(top<=-1)

    {

        printf("\n\t Stack is under flow");

    }

    else

    {

        printf("\n\t The popped elements is %d",stack[top]);

        top--;

    }

}

void display()

{

    if(top>=0)

    {

        printf("\n The elements in STACK \n");

        for(i=top; i>=0; i--)

            printf("\n%d",stack[i]);

        printf("\n Press Next Choice");

    }

    else

    {

        printf("\n The STACK is empty");

    }

   

}


OUTPUTS : 



OUTPUT 2 :


OUTPUT 3 :


OUTPUT 4 :





No comments:

Post a Comment