From 9e9bd5c99aedb60f03dd36eec6ca13da0f888be5 Mon Sep 17 00:00:00 2001 From: VIJETA CHAVHAN <65402647+vijeta-2000@users.noreply.github.com> Date: Thu, 28 Oct 2021 10:14:54 +0530 Subject: [PATCH] stack.c --- C/stack.c | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 C/stack.c diff --git a/C/stack.c b/C/stack.c new file mode 100644 index 0000000..5835b0c --- /dev/null +++ b/C/stack.c @@ -0,0 +1,159 @@ +/** + * Stack data structure implementation. + */ + +//////////////////////////////////////////////////////////////////////////////// +// INCLUDES +#include +#include + +//////////////////////////////////////////////////////////////////////////////// +// MACROS: CONSTANTS + +//////////////////////////////////////////////////////////////////////////////// +// DATA STRUCTURES +/** + * creating a stucture with 'data'(type:int), two pointers 'next','pre' (type: struct node) . + */ +struct node +{ + int data; + struct node *next; + struct node *pre; +} * head, *tmp; + +//////////////////////////////////////////////////////////////////////////////// +// GLOBAL VARIABLES +int count = 0; + +//////////////////////////////////////////////////////////////////////////////// +// FUNCTION PROTOTYPES +void create(); +void push(int x); +int pop(); +int peek(); +int size(); +int isEmpty(); + +//////////////////////////////////////////////////////////////////////////////// +// MAIN ENTRY POINT + +int main(int argc, char const *argv[]) +{ + int x, y, z; + + create(); + push(4); + x = pop(); + // 4. Count: 0. Empty: 1. + printf("%d.\t\tCount: %d.\tEmpty: %d.\n", x, size(), isEmpty()); + + push(1); + push(2); + push(3); + x = pop(); + y = pop(); + // 3, 2. Count: 1. Empty: 0; + printf("%d, %d.\t\tCount: %d.\tEmpty: %d.\n", x, y, size(), isEmpty()); + pop(); // Empty the stack. + + push(5); + push(6); + x = peek(); + push(7); + y = pop(); + push(8); + z = pop(); + // 1, 6, 7, 8. Count: 2. Empty: 0. + printf("%d, %d, %d.\tCount: %d.\tEmpty: %d.\n", x, y, z, size(), isEmpty()); + + return 0; +} + +/** + * Initialize the stack to NULL. + */ +void create() { head = NULL; } + +/** + * Push data onto the stack. + */ +void push(int x) +{ + if (head == NULL) + { + head = (struct node *)malloc(1 * sizeof(struct node)); + head->next = NULL; + head->pre = NULL; + head->data = x; + } + else + { + tmp = (struct node *)malloc(1 * sizeof(struct node)); + tmp->data = x; + tmp->next = NULL; + tmp->pre = head; + head->next = tmp; + head = tmp; + } + ++count; +} + +/** + * Pop data from the stack + */ +int pop() +{ + int returnData; + if (head == NULL) + { + printf("ERROR: Pop from empty stack.\n"); + exit(1); + } + else + { + returnData = head->data; + + if (head->pre == NULL) + { + free(head); + head = NULL; + } + else + { + head = head->pre; + free(head->next); + } + } + --count; + return returnData; +} + +/** + * Returns the next value to be popped. + */ +int peek() +{ + if (head != NULL) + return head->data; + else + { + printf("ERROR: Peeking from empty stack."); + exit(1); + } +} + +/** + * Returns the size of the stack. + */ +int size() { return count; } + +/** + * Returns 1 if stack is empty, returns 0 if not empty. + */ +int isEmpty() +{ + if (count == 0) + return 1; + return 0; +}