Menu-Driven Stack Program

Menu-Driven Stack Program

Menu-Driven Stack Program

Write a menu driven program to allow the user to 1) Push, 2) Pop, 3) Print, and 4) Quit.

You must use separate compilation with files main.c, boolean.h, stack.h, and stack.c

Menu-Driven Stack Program

Check our essay writing services here

APA

Menu-Driven Stack Program

Is an interactive program that allows users to perform stack operations, such as pushing elements, popping elements, displaying the stack’s contents, and exiting the program through a simple menu interface. This program uses separate compilation for modularity, dividing the code into distinct files for structure (stack.h), logic (stack.c), and user interaction (main.c). It ensures error handling for stack overflow and underflow while providing a user-friendly interface to explore the stack’s functionality.

This program implements a menu-driven stack using separate compilation for modularity and clarity. It consists of four files: boolean.h (defines a boolean type), stack.h (provides the stack interface with function prototypes and structure definitions), stack.c (implements stack operations like initialization, push, pop, and print), and main.c (handles user interaction with a menu to perform stack operations). The stack supports adding elements (push), removing elements (pop), displaying contents (print), and exiting the program (quit), while handling errors like stack overflow and underflow gracefully. Separate compilation allows the stack logic to be reusable and maintainable, while the menu interface ensures a user-friendly experience for interacting with the stack.

Check  out the service Python programming language

boolean.h

#ifndef BOOLEAN_H
#define BOOLEAN_H

typedef enum { false, true } boolean;

#endif

stack.h

#ifndef STACK_H
#define STACK_H

#define MAX_SIZE 100

#include “boolean.h”

typedef struct {
int items[MAX_SIZE];
int top;
} Stack;

void initStack(Stack *s);
boolean push(Stack *s, int value);
boolean pop(Stack *s, int *value);
void printStack(Stack *s);

#endif

stack.c

#include <stdio.h>
#include “stack.h”

void initStack(Stack *s) {
s->top = -1;
}

boolean push(Stack *s, int value) {
if (s->top == MAX_SIZE – 1) {
printf(“Stack overflow! Cannot push %d.\n”, value);
return false;
}
s->items[++(s->top)] = value;
return true;
}

boolean pop(Stack *s, int *value) {
if (s->top == -1) {
printf(“Stack underflow! Cannot pop.\n”);
return false;
}
*value = s->items[(s->top)–];
return true;
}

void printStack(Stack *s) {
if (s->top == -1) {
printf(“Stack is empty.\n”);
return;
}
printf(“Stack contents: “);
for (int i = 0; i <= s->top; i++) {
printf(“%d “, s->items[i]);
}
printf(“\n”);
}………