0% found this document useful (0 votes)
14 views

Stackarray (Mayank Sharma)

The document contains code for a C program that implements a stack using an array. The program allows the user to push elements onto the stack, pop elements off the stack, and display the elements currently on the stack. It uses functions like push(), pop(), and display() to perform stack operations and a menu loop to continuously get user input until they choose to exit.

Uploaded by

Mayank Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Stackarray (Mayank Sharma)

The document contains code for a C program that implements a stack using an array. The program allows the user to push elements onto the stack, pop elements off the stack, and display the elements currently on the stack. It uses functions like push(), pop(), and display() to perform stack operations and a menu loop to continuously get user input until they choose to exit.

Uploaded by

Mayank Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h> Name : Mayank Sharma


#include <stdlib.h>
int *stack, n, top;
void push()
{
int x;
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 Stack is under flow");
}
else
{
printf("\n The popped elements is %d", stack[top]);
top--;
}
}
void display()
{
int i;
if (top >= 0)
{
printf("\n The elements in STACK \n");
for (i = top; i >= 0; i--)
printf("\n%d", stack[i]);
}
else
{
printf("\n The STACK is empty");
}
}
int main()
{
int choice;
top = -1;
printf("\n Enter the size of STACK :");
scanf("%d", &n);
stack = (int *)malloc(n * sizeof(int));
printf("\n STACK :");
printf("\n 1.PUSH\n 2.POP\n 3.DISPLAY\n 4.EXIT");
l:
printf("\n Enter the Choice:");
scanf("%d", &choice);
if (choice == 1)
{
push();
}
else if (choice == 2)
{
pop();
}
else if (choice == 3)
{
display();
}
else if (choice == 4)
{
printf("\n EXIT");
exit(0);
}
else
{
printf("\n Please Enter a Valid Choice(1/2/3/4)");
}
goto l;
return 0;
}
OUTPUT :

SIGNATURE:

You might also like