Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

DS || Section B || Pgm 6 || 5: Program to implement Stack.

 #include <stdio.h>

#include <stdlib.h>

int stack[10];

int top = -1;

int size;


void push (int element)

{

// Check stack overflow

if (top >= size - 1)

{

printf ("Stack Overflow\n");

return;

}

stack[++top] = element;

printf ("Data pushed to stack.\n");

}


void pop ()

{

// Check stack underflow

if (top < 0)

{

printf ("Stack is empty.\n");

return;

}

printf ("Element poped is -> %d", stack[top--]);

return;

}

void display ()

{

int i = 0;

if (top == -1)

{

printf ("Stack is Empty .\n");

return;

}

printf ("Stack content are\n");

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

{

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

}

printf ("\n");

}


void main ()

{

int choice, data;

printf ("Enter stack size");

scanf ("%d", &size);

while (1)

{

printf ("1. Push\n");

printf ("2. Pop\n");

printf ("3. Display\n");

printf ("4. Exit\n");

printf ("Enter your choice: ");

scanf ("%d", &choice);

switch (choice)

{

case 1:

printf ("Enter data to push into stack: ");

scanf ("%d", &data);

push (data);

break;


case 2:

pop ();

break;


case 3:

display ();

break;


case 4:

exit (0);

break;


default:

printf ("Invalid choice, please try again.\n");

}


printf ("\n\n");

}


}

Post a Comment

0 Comments