C Code Examples: Stack and Queue Datatype

This topic was published by and viewed 428 times since "". The last page revision was "".

Viewing 1 post (of 1 total)
  • Author
    Posts
  • DevynCJohnson
    DevynCJohnson
    Keymaster
    • Topics - 444
    • @devyncjohnson

    Stack

    // -*- coding: utf-8 -*-
    // vim:fileencoding=utf-8
    /**
    @author Devyn Collier Johnson <DevynCJohnson@Gmail.com>
    @copyright LGPLv3
    
    @brief Stack Datatype
    @version 2016.02.29
    
    @section LICENSE
    GNU Lesser General Public License v3
    Copyright (c) Devyn Collier Johnson, All rights reserved.
    
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3.0 of the License, or (at your option) any later version.
    
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library.
    */
    
    
    #ifndef DATATYPE_STACK_INCLUDED
    #define DATATYPE_STACK_INCLUDED
    
    
    #define STANDARD_MACROS_SPECIAL_SEEN
    
    
    /* INITIALIZATION */
    void init_stack(int *top);
    void push_stack(int *s, const int* top, const int element);
    int pop_stack(int *s, const int *top);
    int isstackfull(const int *top, const int size);
    int isstackempty(const int *top);
    
    
    /* FUNCTIONS */
    
    
    void init_stack(int *top) {
        // Initialize stack pointer; Usage: int stack, top; init_stack(&top);
        *top = 0x00;
    }
    
    
    void push_stack(int *s, const int* top, const int element) {
        // Push an element into stack (stack must not be full); Usage: push_stack(stack, &top, elem);
        s[(*top)++] = element;
    }
    
    
    int pop_stack(int *s, const int *top) {
        // Remove an element from stack (stack must not be empty); Usage: elem = pop_stack(stack, &top);
        return s[--(*top)];
    }
    
    
    int isstackfull(const int *top, const int size) {
        // Return 1 if stack is full, otherwise return 0; Usage: while(!isstackfull(&top, SIZE)) {}
        return (*top == size ? 1 : 0);
    }
    
    
    int isstackempty(const int *top) {
        // Return 1 if the stack is empty, otherwise return 0; Usage: isstackempty(&top);
        return (*top == 0 ? 1 : 0);
    }
    
    
    #endif

    Linked-Stack

    // -*- coding: utf-8 -*-
    // vim:fileencoding=utf-8
    /**
    @author Devyn Collier Johnson <DevynCJohnson@Gmail.com>
    @copyright LGPLv3
    
    @brief Linked Stack Datatype
    @version 2016.02.29
    
    @section LICENSE
    GNU Lesser General Public License v3
    Copyright (c) Devyn Collier Johnson, All rights reserved.
    
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3.0 of the License, or (at your option) any later version.
    
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library.
    */
    
    
    #ifndef DATATYPE_LINKED_STACK_INCLUDED
    #define DATATYPE_LINKED_STACK_INCLUDED
    
    
    #define STANDARD_MACROS_SPECIAL_SEEN
    
    #include <stdlib.h>
    
    
    /* INITIALIZATION */
    void init_linked_stack(struct node *s);
    struct node *push_linked_stack(struct node *s, int data);
    struct node *pop_linked_stack(struct node *s, int *data);
    int islinkedstackempty(struct node *s);
    
    typedef struct node {
        int data;
        struct node *next;
    } linked_stack_node_t;
    
    
    /* FUNCTIONS */
    
    
    void init_linked_stack(struct node *head) {
        // Init the stack; Usage: struct node *head = NULL; init_linked_stack(head);
        head = NULL;
    }
    
    
    struct node *push_linked_stack(struct node *head, int data) {
        // Push an element into stack; Usage: head = push_linked_stack(head, element);
        struct node *tmp = (struct node *)calloc(0x01, sizeof(struct node));
        if (tmp == NULL) exit(0);
        tmp->data = data;
        tmp->next = head;
        head = tmp;
        return head;
    }
    
    
    struct node *pop_linked_stack(struct node *head, int *element) {
        // Pop an element from the stack; Usage: head = pop_linked_stack(head, &element);
        struct node *tmp = head;
        *element = head->data;
        head = head->next;
        free(tmp);
        return head;
    }
    
    
    int islinkedstackempty(struct node *head) {
        // Returns 1 if the stack is empty, otherwise returns 0; Usage: islinkedstackempty(head);
        return (head == NULL ? 1 : 0);
    }
    
    
    #endif

    Queue

    // -*- coding: utf-8 -*-
    // vim:fileencoding=utf-8
    /**
    @author Devyn Collier Johnson <DevynCJohnson@Gmail.com>
    @copyright LGPLv3
    
    @brief Queue Datatype (FIFO Stack)
    @version 2016.02.29
    
    @section LICENSE
    GNU Lesser General Public License v3
    Copyright (c) Devyn Collier Johnson, All rights reserved.
    
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3.0 of the License, or (at your option) any later version.
    
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library.
    */
    
    
    #ifndef DATATYPE_QUEUE_INCLUDED
    #define DATATYPE_QUEUE_INCLUDED
    
    
    #define STANDARD_MACROS_SPECIAL_SEEN
    
    
    /* INITIALIZATION */
    void init_queue(int *head, int *tail);
    void enqueue(int *q, const int *tail, const int element);
    int dequeue(int *q, const int *head);
    int isqueuefull(const int tail, const int size);
    int isqueueempty(const int head, const int tail);
    
    
    /* FUNCTIONS */
    
    
    void init_queue(int *head, int *tail) {
        // Initialize queue pointers; Usage: int head, tail, element, queue; init_queue(&head, &tail);
        *head = *tail = 0x00;
    }
    
    
    void enqueue(int *q, const int *tail, const int element) {
        // Enqueue an element (queue must not be full); Usage: enqueue(queue, &tail, element);
        q[(*tail)++] = element;
    }
    
    
    int dequeue(int *q, const int *head) {
        // Dequeue an element (queue must not be empty); Usage: element = dequeue(queue, &head);
        return q[(*head)++];
    }
    
    
    int isqueuefull(const int tail, const int size) {
        // Return 1 if queue is full, otherwise return 0; Usage: isqueuefull(tail, SIZE);
        return (tail == size);
    }
    
    
    int isqueueempty(const int head, const int tail) {
        // Return 1 if the queue is empty, otherwise return 0; Usage: isqueueempty(head, tail);
        return (tail == head);
    }
    
    
    #endif

    Further Reading

Viewing 1 post (of 1 total)