You can implement a stack data structure in C using an array or a linked list.
1. Array
#include <stdio.h> #include <stdbool.h> #define MAX_SIZE 100 // Define the stack structure typedef struct { int data[MAX_SIZE]; int top; } Stack; // Function to initialize the stack void initialize(Stack *stack) { stack->top = -1; // Set the top of the stack to -1 to indicate an empty stack } // Function to check if the stack is empty bool isEmpty(Stack *stack) { return stack->top == -1; } // Function to check if the stack is full bool isFull(Stack *stack) { return stack->top == MAX_SIZE - 1; } // Function to push an element onto the stack bool push(Stack *stack, int item) { if (isFull(stack)) { printf("Stack is full. Cannot push.\n"); return false; } stack->data[++stack->top] = item; return true; } // Function to pop an element from the stack int pop(Stack *stack) { if (isEmpty(stack)) { printf("Stack is empty. Cannot pop.\n"); return -1; // Return a sentinel value to indicate an error } return stack->data[stack->top--]; } // Function to get the top element of the stack without popping it int peek(Stack *stack) { if (isEmpty(stack)) { printf("Stack is empty. No top element.\n"); return -1; // Return a sentinel value to indicate an error } return stack->data[stack->top]; } int main() { Stack stack; initialize(&stack); // Push some elements onto the stack push(&stack, 10); push(&stack, 20); push(&stack, 30); // Print and pop elements from the stack while (!isEmpty(&stack)) { printf("Top element: %d\n", peek(&stack)); printf("Popped element: %d\n", pop(&stack)); } return 0; }
2. LinkedList
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> // Define a structure for a single node in the linked list typedef struct Node { int data; struct Node* next; } Node; // Define the stack structure with a pointer to the top of the stack typedef struct { Node* top; } Stack; // Function to initialize the stack void initialize(Stack* stack) { stack->top = NULL; } // Function to check if the stack is empty bool isEmpty(Stack* stack) { return stack->top == NULL; } // Function to push an element onto the stack void push(Stack* stack, int item) { Node* newNode = (Node*)malloc(sizeof(Node)); if (newNode == NULL) { printf("Memory allocation failed. Cannot push.\n"); return; } newNode->data = item; newNode->next = stack->top; stack->top = newNode; } // Function to pop an element from the stack int pop(Stack* stack) { if (isEmpty(stack)) { printf("Stack is empty. Cannot pop.\n"); return -1; // Return a sentinel value to indicate an error } Node* topNode = stack->top; int item = topNode->data; stack->top = topNode->next; free(topNode); return item; } // Function to get the top element of the stack without popping it int peek(Stack* stack) { if (isEmpty(stack)) { printf("Stack is empty. No top element.\n"); return -1; // Return a sentinel value to indicate an error } return stack->top->data; } // Function to free the memory used by the stack void destroy(Stack* stack) { while (!isEmpty(stack)) { pop(stack); } } int main() { Stack stack; initialize(&stack); // Push some elements onto the stack push(&stack, 10); push(&stack, 20); push(&stack, 30); // Print and pop elements from the stack while (!isEmpty(&stack)) { printf("Top element: %d\n", peek(&stack)); printf("Popped element: %d\n", pop(&stack)); } // Free the memory used by the stack destroy(&stack); return 0; }