0% found this document useful (0 votes)
3 views7 pages

Stack Assignment

The document contains two C programs that implement stack operations including push, pop, and display. The first program allows the user to input a number of elements to push onto the stack, while the second program utilizes a switch-case structure for user interaction to perform various stack operations. Both programs handle stack overflow and underflow conditions appropriately.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Stack Assignment

The document contains two C programs that implement stack operations including push, pop, and display. The first program allows the user to input a number of elements to push onto the stack, while the second program utilizes a switch-case structure for user interaction to perform various stack operations. Both programs handle stack overflow and underflow conditions appropriately.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link] Pop and Display Operation in Stack.

#include <stdio.h>

#define MAX 100

int stack[MAX];

int top = -1;

void push(int value)

if (top == MAX - 1)

printf("Stack Overflow! Cannot push %d\n");

else

top++;

stack[top] = value;

printf("%d pushed into stack.\n", value);

void pop()

if (top == -1)

printf("Stack Underflow! Cannot pop.\n");

else

printf("%d popped from stack.\n", stack[top]);

top--;

}
}

void display()

if (top == -1)

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

else

printf("Stack elements: ");

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

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

printf("\n");

int main()

int n, arr[MAX];

printf("Enter number of elements to push (max 100): ");

scanf("%d", &n);

printf("Enter %d elements: ", n);

for (int i = 0; i < n; i++)

scanf("%d", &arr[i]);

for (int i = 0; i < n; i++)

{
push(arr[i]);

display();

pop();

display();

return 0;

}
[Link] Pop and Display Operation in Stack using Switch case.
#include <stdio.h>

#define MAX 100

int stack[MAX];

int top = -1;

void push(int value)

if (top == MAX - 1)

printf("Stack Overflow\n");

else

top++;

stack[top] = value;

printf("%d pushed to stack\n", value);

void pop()

if (top == -1)

printf("Stack Underflow\n");

else

printf("%d popped from stack\n", stack[top]);

top--;

void peek()

{
if (top == -1)

printf("Stack is empty\n");

else

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

void display()

if (top == -1)

printf("Stack is empty\n");

else

printf("Stack elements are:\n");

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

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

int main()

int choice, value;

while (1)

printf("\nStack Menu\n");

printf("1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");

printf("Enter your choice: ");


scanf("%d", &choice);

switch (choice)

case 1:

printf("Enter value to push: ");

scanf("%d", &value);

push(value);

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

display();

break;

case 5:

return 0;

default:

printf("Invalid choice\n");

return 0;

You might also like