0% found this document useful (0 votes)
4 views2 pages

Stack Implementation in C Using Arrays

This document contains a C program that implements a stack data structure using an array. It provides functionalities to push, pop, and print elements from the stack, along with user interaction through a menu-driven interface. The program initializes the stack, checks for full and empty conditions, and handles user inputs accordingly.

Uploaded by

Prasad balkawade
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)
4 views2 pages

Stack Implementation in C Using Arrays

This document contains a C program that implements a stack data structure using an array. It provides functionalities to push, pop, and print elements from the stack, along with user interaction through a menu-driven interface. The program initializes the stack, checks for full and empty conditions, and handles user inputs accordingly.

Uploaded by

Prasad balkawade
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

#include <stdio.

h>
#define MAX 6

/*
Author: prasad balkawade
Title:stack using array
*/

typedef struct stack {


int data[MAX];
int top;
} stack;

void init(stack *);


int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *, int);
void print(stack *);

int main() // use int main(), not void main


{
stack s;
int x, op;
init(&s);

do {
printf("\n\n1) Push\n2) Pop\n3) Print\n4) Quit");
printf("\nEnter Your choice: ");
scanf("%d", &op);

switch (op) {
case 1:
printf("\nEnter a number: ");
scanf("%d", &x);
if (!full(&s))
push(&s, x);
else
printf("\nStack is full...");
break;

case 2:
if (!empty(&s)) {
x = pop(&s);
printf("\nPopped value = %d", x);
} else
printf("\nStack is empty...");
break;

case 3:

print(&s);
break;
}
} while (op != 4);

return 0;
}

void init(stack *s) {


s->top = -1;
}

int empty(stack *s) {


return (s->top == -1);
}
int full(stack *s) {
return (s->top == MAX - 1);
}

void push(stack *s, int x) {


s->top++;
s->data[s->top] = x;
}

int pop(stack *s) {


int x = s->data[s->top];
s->top--;
return x;
}

void print(stack *s) {


int i;
printf("\nStack elements: ");
for (i = s->top; i >= 0; i--)
printf("%d ", s->data[i]);
printf("\n");
}

OUTPUT :
1) Push
2) Pop
3) Print
4) Quit
Enter Your choice: 1

Enter a number: 10

1) Push
2) Pop
3) Print
4) Quit
Enter Your choice: 1

Enter a number: 20

1) Push
2) Pop
3) Print
4) Quit
Enter Your choice: 2

Popped value = 20

1) Push
2) Pop
3) Print
4) Quit
Enter Your choice: 3

Stack elements: 10

1) Push
2) Pop
3) Print
4) Quit
Enter Your choice: 4

=== Code Execution Successful ===

You might also like