GithubHelp home page GithubHelp logo

joegasewicz / stacks Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 19 KB

A stack structure static library written in C with a few extra features

CMake 4.03% C 95.97%
abstract-data-types c cprogramming stack

stacks's Introduction

Stacks

A stack structure static library written in C with a few extra features

  • Set the stack to have either a dynamic or static length
  • An error will be returned if the stack reaches its maximum set length
  • Get the stack's current (node's) index at any time

Initiate

Options allow the stack length to be set to dynamic or static. Without options the stack will have a dynamic length.

Stack *my_stack = S_Stack_new(NULL);

With options the stack will return a S_OUT_OF_BOUNDS_ERROR.

S_Options options = (S_Options){ .max_length = 1, .dynamic = false };
// still within max_length limit
int ok = S_Stack_push(my_stack, &data1);
assert(S_OUT_OF_BOUNDS_ERROR != ok);

// exceeded the max_length
int err = S_Stack_push(my_stack, &data2);
assert(S_OUT_OF_BOUNDS_ERROR == err);

Destroy

Detaches each node from the stack. This function does not destroy allocated memory assigned to the node's data.

if (S_Stack_destroy(my_stack) < 0)
    perror("panic!");

Push

Pushes the next node on the stack. You must initiate the stack by calling the S_Stack_new(NULL) first. It is the responsibility of the caller to manage the memory of void *data.

int data1 = 1, data2 = 2; // *void
S_Stack_push(my_stack, &data1);
S_Stack_push(my_stack, &data2);

Pop

Removes the top nod from the stack. The caller must manage the memory of void *data.

void* data = S_Stack_pop(my_stack);

Peek (macro)

Enables the caller to peek at the top node without popping it off the stack. It returns a reference to the top node.

void *data = S_PEEK(my_stack);

Size (macro)

Returns the size of the stack based on total count of nodes.

int stack_size = S_SIZE(my_stack);

Current Index (macro)

Returns the current node index (or top node on the stack).

int current_node_index = S_CURRENT_INDEX(my_stack);

stacks's People

Contributors

joegasewicz avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  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.