Coder Social home page Coder Social logo

dsa_stack_c's Introduction

DSA Stack Implementation In C


This project implements a stack data structure in C. The stack is a Last-In-First-Out (LIFO) data structure that allows for efficient insertion and deletion of elements from one end only, called the top. The stack is implemented using a dynamic memory allocation and provides functions for creating, pushing, popping, peeking, checking if it is full or empty, and deleting the stack.

The stack uses a structure called stack_t to hold the necessary information, including the element size, element count, memory block pointer, and the index of the top element.

Example/Testing

Here's an example/test code to demonstrate the usage of the stack:

#include <stdio.h>
#include "stack.h"

int main() {
    stack_t *stack = stack_create(sizeof(int), 5);  // Create a new stack with element size 4 bytes and capacity for 5 elements
    if (stack == NULL) {
        printf("Failed to create stack. Exiting.\n");
        return 1;
    }

    int elements[] = {10, 20, 30, 40, 50};

    // Push elements onto the stack
    for (size_t i = 0; i < 5; i++) {
        if (!stack_push(stack, &(elements[i]))) {
            printf("Failed to push element %d onto the stack. Exiting.\n", elements[i]);
            stack_delete(&stack);
            return 1;
        }
    }

    // Pop and print elements from the stack
    int popped_element;
    while (!stack_is_empty(stack)) {
        if (!stack_pop(stack, &popped_element)) {
            printf("Failed to pop element from the stack. Exiting.\n");
            stack_delete(&stack);
            return 1;
        }
        printf("Popped element: %d\n", popped_element);
    }

    stack_delete(&stack);  // Delete the stack and free its resources

    return 0;
}

In this example, we create a stack to hold integers. We push five elements onto the stack and then pop and print each element until the stack becomes empty. Finally, we delete the stack to free its resources.

Compiling the Code

To compile the code, you need a C compiler such as GCC. Assuming you have GCC installed, you can use the following command to compile the code and generate an executable:

    gcc .\examples\main.c .\src\stack.c -I .\obj\ -DDEBUG_STACK
    a.exe

dsa_stack_c's People

Contributors

aajay5 avatar

Watchers

 avatar haresh sondagar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.