Stack (Array)
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 10
#define EMPTY 'E'
typedef struct stack {
char letters[STACK_SIZE];
int top;
} stack;
// Function Prototypes
stack *createStack();
int isEmpty(stack *s);
int isFull(stack *s);
char peek(stack *s);
char pop(stack *s);
int push(stack *s, char c);
void printStack(stack *s);
void printChar(char c);
void printOptions();
// Returns initialized stack
stack *createStack() {
stack *s = malloc(sizeof(stack));
s->top = -1;
return s;
}
// Returns 1 if stack is NULL or is empty
int isEmpty(stack *s) { return (!s || s->top == -1); }
// Returns 1 if stack is not NULL and is full, otherwise returns 0
int isFull(stack *s) { return (s && (s->top == STACK_SIZE - 1)); }
// Return - WITHOUT removing - character from top of stack
char peek(stack *s) {
if (isEmpty(s))
return EMPTY;
return s->letters[s->top];
}
// Return/ remove character from top of stack
char pop(stack *s) {
if (isEmpty(s))
return EMPTY;
char letter = s->letters[(s->top)--];
return letter;
}
// Push character to top of stack
int push(stack *s, char c) {
if (!s || isFull(s))
return 0;
s->letters[++(s->top)] = c;
return 1;
}
// Empty all elements in stack
void clearStack(stack *s) {
while (!isEmpty(s))
pop(s);
}
// Prints stack to screen
void printStack(stack *s) {
if (isEmpty(s)) {
printf("Stack is empty\n");
return;
}
printf("(top)\n");
for (int i = s->top; i >= 0; i--) {
printf("%c\n", s->letters[i]);
}
printf("(bottom)\n");
}
// Print character
void printChar(char c) {
if (c == EMPTY)
printf("EMPTY\n");
else
printf("%c\n", c);
}
// Print options menu
void printOptions() {
printf("1) Push\n");
printf("2) Pop\n");
printf("3) Peek\n");
printf("4) Print Stack\n");
printf("5) Clear Stack\n");
printf("6) Quit\n");
printf("\n:");
}
int main(void) {
stack *s = createStack();
int choice = 0;
char c;
while (choice != 6) {
printOptions();
scanf("%d", &choice);
switch (choice) {
case 1: // Push
printf("Type a lowercase letter to add to stack:\n");
scanf(" %c", &c);
if (push(s, c))
printf("%c added to stack\n", c);
else
printf("Failed to add %c to stack\n", c);
break;
case 2: // Pop
c = pop(s);
printf("Popped: ");
printChar(c);
break;
case 3: // Peek
c = peek(s);
printf("Peeked: ");
printChar(c);
break;
case 4: // Print Stack
printStack(s);
break;
case 5: // Clear Stack
clearStack(s);
printf("Stack is now empty\n");
break;
default:
break;
}
printf("\n");
}
// Free Memory
free(s);
return 0;
}
Academic Notice: The code shown here is provided solely as a learning reference. Copying and pasting is intentionally disabled to encourage independent practice. Students should implement solutions on their own to demonstrate understanding. This material is not intended for direct submission in assignments.
Additionally: This code was written by a former CS1 student and may not reflect your professor’s intended solution or instructional approach. For coursework, students are expected to follow examples, conventions, and requirements presented in class and in professor-assigned materials.