Implementing a Stack in C Using an Array: A Comprehensive Guide
In computer science, a stack is a linear data structure that follows the Last In First Out (LIFO) principle. This article will guide you through implementing a stack using an array in the C programming language. The implementation will include essential stack operations such as push, pop, and display.
Overview of the Implementation
This implementation of a stack will cover defining the stack structure, implementing the necessary stack operations, and providing a complete example with a main function to demonstrate usage.
Stack Structure Definition
The stack structure consists of two main components: an array to hold the stack elements and an integer variable to track the index of the top element.
Structure
typedef struct Stack { int arr[MAX]; // Array to hold stack elements int top; // Index of the top element } Stack;Here, MAX is defined as the maximum size of the stack. In this example, MAX is set to 100.
Function Definitions
In this section, we will define the functions needed to create the stack, check if the stack is full or empty, and perform the stack operations.
Create Stack
Stack createStack() { Stack stack malloc(sizeof(Stack)); stack->top -1; // Stack is initially empty return stack; }This function creates a stack and initializes the top index to -1, indicating an empty stack.
Check if Full
int isFull(Stack stack) { return stack->top MAX - 1; }This function checks if the stack is full by comparing the top index with MAX - 1.
Check if Empty
int isEmpty(Stack stack) { return stack->top -1; }This function checks if the stack is empty by comparing the top index with -1.
Add an Element (Push)
void push(Stack stack, int value) { if (isFull(stack)) { printf("Stack is full. Cannot push element. "); return; } stack->arr[stack->top] value; printf("Pushed %d onto the stack. ", value); }This function adds a new element to the stack. It first checks if the stack is full. If not, it adds the element to the current top index and increments the top index.
Remove an Element (Pop)
int pop(Stack stack) { if (isEmpty(stack)) { printf("Stack is empty. Cannot pop element. "); return INT_MIN; // Return a sentinel value } return stack->arr[stack->top--]; }This function removes and returns the top element of the stack. It first checks if the stack is empty. If not, it removes the top element and decrements the top index before returning the value.
Get Top Element (Peek)
int peek(Stack stack) { if (isEmpty(stack)) { printf("Stack is empty. Cannot peek element. "); return INT_MIN; // Return a sentinel value } return stack->arr[stack->top]; }This function returns the top element of the stack without removing it. It first checks if the stack is empty. If not, it returns the top element.
Display Stack Elements
void display(Stack stack) { if (isEmpty(stack)) { printf("Stack is empty. "); return; } printf("Stack elements are: "); for (int i 0; i top; i ) { printf("%dt", stack->arr[i]); } printf(" "); }This function prints all the elements in the stack. It first checks if the stack is empty. If not, it prints each element in the stack.
Main Function
The main function demonstrates the stack operations by pushing, popping, and displaying stack elements.
int main() { Stack stack createStack(); push(stack, 10); push(stack, 20); push(stack, 30); display(stack); printf(" "); printf("Popped element: %d ", pop(stack)); display(stack); free(stack); // Free allocated memory return 0; }This main function creates the stack, pushes three elements, displays the stack, pops an element, and displays the stack again. Finally, it frees the allocated memory.
Compilation and Execution
To compile and run the program:
gcc stack.c -o stack ./stackThis program provides a basic stack implementation using an array in C, allowing for fundamental stack operations. You can extend it further by adding error handling or dynamic resizing if needed.
Conclusion
The implementation explained in this article offers a simple yet robust way to create a stack using an array in C. By understanding and working through these examples, you can further enhance your knowledge of data structures in C and apply similar techniques to more complex scenarios.