Data Structures 06 - Stack
Code
#include <stdio.h>
#include <stdlib.h>
#define STACK_MAX_SIZE 10 // Define the maximum capacity of the stack
// Define a character stack structure
typedef struct CharStack {
int top; // Stack top pointer, initialized to -1 indicating an empty stack
int data[STACK_MAX_SIZE]; // Stack storage array, fixed maximum length
} *CharStackPtr;
// Output the elements in the stack
void outputStack(CharStackPtr paraStack) {
for (int i = 0; i <= paraStack->top; i ++) {
printf("%c ", paraStack->data[i]);
}
printf("\r\n");
}
// Initialize an empty character stack
CharStackPtr charStackInit() {
CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(struct CharStack));
resultPtr->top = -1; // Initialize stack top pointer to -1
return resultPtr;
}
// Push an element onto the stack
void push(CharStackPtr paraStackPtr, int paraValue) {
if (paraStackPtr->top >= STACK_MAX_SIZE - 1) { // Check if stack space is full
printf("Cannot push element: stack full.\r\n");
return;
}
paraStackPtr->top++; // Update stack top pointer
paraStackPtr->data[paraStackPtr->top] = paraValue; // Push element onto the stack
}
// Pop an element from the stack
char pop(CharStackPtr paraStackPtr) {
if (paraStackPtr->top < 0) { // Check if stack is empty
printf("Cannot pop element: stack empty.\r\n");
return '\0'; // If stack is empty, return null character
}
paraStackPtr->top--; // Update stack top pointer
return paraStackPtr->data[paraStackPtr->top + 1]; // Return the popped element
}
// Test push and pop functions
void pushPopTest() {
printf("---- pushPopTest begins. ----\r\n");
CharStackPtr tempStack = charStackInit(); // Initialize stack
printf("After initialization, the stack is: ");
outputStack(tempStack);
// Push elements
for (char ch = 'a'; ch < 'm'; ch ++) {
printf("Pushing %c.\r\n", ch);
push(tempStack, ch);
outputStack(tempStack);
}
// Pop elements
char ch;
for (int i = 0; i < 3; i ++) {
ch = pop(tempStack);
printf("Pop %c.\r\n", ch);
outputStack(tempStack);
}
printf("---- pushPopTest ends. ----\r\n");
}
// Program entry point
int main() {
pushPopTest(); // Run test function
}
Code Summary:
-
Defines a fixed-size stack (
STACK_MAX_SIZEis 10) for storing characters. -
The
CharStackstructure contains a stack top pointertopand a data arraydata. The stack top pointer is initialized to -1, indicating an empty stack. -
The
charStackInitfunction initializes an empty stack and returns a pointer to the stack. -
The
pushfunction pushes a new element onto the stack. It first checks if the stack is full; if not, it pushes the new element onto the top of the stack. -
The
popfunction pops an element from the stack. It first checks if the stack is empty; if not, it pops the top element and returns it. -
The
outputStackfunction outputs all elements in the stack. -
The
pushPopTestfunction is a test function for testingpushandpopoperations. It initializes a stack, then pushes a series of characters, then pops several characters, outputting the current state of the stack after each operation. -
The
mainfunction is the program entry point, calling thepushPopTestfunction to execute the test.

Running Result
