Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing a Stack Data Structure in C

Tech Apr 25 47

A stack is a linear data structure commonly used in programming. It is a restricted linear list where insertion and deletion can only occur at one end, known as the 'top'. The opposite end is called the 'bottom'. Stacks follow the 'last in, first out' (LIFO) principle, making them useful for various applications.

Operations on a stack include:

  1. Pushing a element onto the stack
  2. Popping an element from the stack
  3. Initializing the stack
  4. Checking if the stack is empty
  5. Accessing the top element of the stack

The following header file defines the stack structure and function declarations:

#include <stdbool.h>

#ifndef C_STACK_H
#define C_STACK_H
#define STACK_EMPTY -1

typedef struct {
    int capacity;
    int top;
    int *data;
} Stack, *StackPtr;

void initStack(StackPtr stackPtr, int cap);
void pushStack(StackPtr stackPtr, int item);
int popStack(StackPtr stackPtr);
int peekStack(StackPtr stackPtr);
static int *reExpansion(StackPtr stackPtr);
void clearStack(StackPtr stackPtr);
bool isFull(StackPtr stackPtr);
bool isEmpty(StackPtr stackPtr);

#endif //C_STACK_H

The implementation file contains the function definitions:

#include "stack.h"
#include "../sorting_algorithms/sort_tools.h"
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>

void initStack(StackPtr stackPtr, int cap) {
    stackPtr->top = STACK_EMPTY;
    stackPtr->capacity = cap;
    stackPtr->data = getArrayPointer(cap);
}

void pushStack(StackPtr stackPtr, int item) {
    if (isFull(stackPtr)) {
        printf("this stack is full!!!");
    }
    stackPtr->data[++stackPtr->top] = item;
}

int popStack(StackPtr stackPtr) {
    int ret;
    if (isEmpty(stackPtr)) {
        printf("stack is empty!!!");
        ret = STACK_EMPTY;
    } else {
        ret = stackPtr->data[stackPtr->top--];
    }
    return ret;
}

int peekStack(StackPtr stackPtr) {
    int ret;
    if (isEmpty(stackPtr)) {
        printf("stack is empty!!!");
        ret = STACK_EMPTY;
    } else {
        ret = stackPtr->data[stackPtr->top];
    }
    return ret;
}

static int *reExpansion(StackPtr stackPtr) {
    stackPtr->capacity *= 2;
    int *newData = getArrayPointer(stackPtr->capacity);
    for (int i = 0; i < stackPtr->capacity / 2; ++i) {
        newData[i] = (stackPtr->data)[i];
    }
    free(stackPtr->data);
    return stackPtr->data = newData;
}

void clearStack(StackPtr stackPtr) {
    stackPtr->top = STACK_EMPTY;
}

bool isFull(StackPtr stackPtr) {
    return stackPtr->top + 1 == stackPtr->capacity;
}

bool isEmpty(StackPtr stackPtr) {
    return stackPtr->top == STACK_EMPTY;
}

Related Articles

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Comprehensive Guide to Hive SQL Syntax and Operations

This article provides a detailed walkthrough of Hive SQL, categorizing its features and syntax for practical use. Hive SQL is segmented into the following categories: DDL Statements: Operations on...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.