STACK USING ARRAY by smd


STACK USING ARRAY


#include <stdio.h>

int stack[100],top=-1,n,i,choice = 0;

void push();

void pop();

void peek();

void display();

void main()

{

printf("Enter no of elements which you want to insert\n");

scanf("%d",&n);

while (choice!=5)

{

printf(" Choose the option below \n");

printf("1.push\n2.pop\n3.peek\n4.display\n5.exit\n");

scanf("%d",&choice);

switch(choice)

{

case 1:

push();

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

display();

break;

case 5:

printf("Exiting.....\n");

break;

default:

printf("invalid entry\n");

break;

}

}

}

void push()

{

    if(top>=n-1)

    {

        printf("stack is overflow \n");

    }

    else

    {

     int x;

     printf("enter the x value\n");

     scanf("%d",&x);

     top++;

     stack[top]=x;

    }

}

void pop()

{

    if(top ==-1)

    {

        printf("stack is underflow \n");

    }

    else

    {

       printf("%d is deleted \n",stack[top]);

    top--;

    }

}

void peek()

{

    if(top==-1)

    {

        printf("stack is empty\n");

    }

    else

    {

        printf("%d is topmost element\n",stack[top]);

    }

}

void display()

{

    if(top==-1)

    {

        printf("stack is empty\n");

    }

    else

    {

         printf ("stack elements are\n");

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

         {

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

         }

    }

}














output:

Enter no of elements which you want to insert

5

 Choose the option below 

1.push

2.pop

3.peek

4.display

5.exit

1

enter the x value

10

 Choose the option below 

1.push

2.pop

3.peek

4.display

5.exit

1

enter the x value

20

 Choose the option below 

1.push

2.pop

3.peek

4.display

5.exit

1  

enter the x value

30

 Choose the option below 

1.push

2.pop

3.peek

4.display

5.exit

1

enter the x value

40

 Choose the option below 

1.push

2.pop

3.peek

4.display

5.exit

1

enter the x value

50

 Choose the option below 

1.push

2.pop

3.peek

4.display

5.exit

4

stack elements are

50

40

30

20

10

 Choose the option below

1.push

2.pop

3.peek

4.display

5.exit

2

50 is deleted 

 Choose the option below

1.push

2.pop

3.peek

4.display

5.exit

2

40 is deleted 

 Choose the option below

1.push

2.pop

3.peek

4.display

5.exit

3

30 is topmost element

 Choose the option below

1.push

2.pop

3.peek

4.display

5.exit

4

stack elements are

30

20

10

 Choose the option below

1.push

2.pop

3.peek

4.display

5.exit

5

Exiting.....

Comments

Popular posts from this blog

SINGLE LINKED LIST by smd

CLL by smd

QUEUE USING ARRAYS by smd