From 2e58bc22071f12027af5f2d8b428f77172df36d5 Mon Sep 17 00:00:00 2001 From: dang hai Date: Mon, 1 Jul 2019 18:06:21 -0700 Subject: [PATCH] stack implementation by linkedlist --- data_structures/stack/README.md | 7 +- .../stack/stack_linkedlist/Makefile | 12 +++ data_structures/stack/stack_linkedlist/main.c | 22 ++++++ .../stack/stack_linkedlist/stack.c | 79 +++++++++++++++++++ .../stack/stack_linkedlist/stack.h | 15 ++++ 5 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 data_structures/stack/stack_linkedlist/Makefile create mode 100644 data_structures/stack/stack_linkedlist/main.c create mode 100644 data_structures/stack/stack_linkedlist/stack.c create mode 100644 data_structures/stack/stack_linkedlist/stack.h diff --git a/data_structures/stack/README.md b/data_structures/stack/README.md index 174e9454..18bd2fb1 100644 --- a/data_structures/stack/README.md +++ b/data_structures/stack/README.md @@ -1,4 +1,4 @@ -# Simple generic Stack +# Simple generic Stack This is a modular generic stack data-structure. The stack is self growing. @@ -6,7 +6,8 @@ This is a modular generic stack data-structure. The stack is self growing. * stack-Header file for import. * stack.c implementation of the stack -* main.c framework program for testing. +* main.c framework program for testing. +* stack_linkedlist: Another stack implementation by linkedlist You need to only import the **stack.h** @@ -18,7 +19,7 @@ Initializes the stack with a capacity of 10 elements. ``` void push(void * object); ``` -pushs the argument onto the stack +pushs the argument onto the stack ``` void * pop(); ``` diff --git a/data_structures/stack/stack_linkedlist/Makefile b/data_structures/stack/stack_linkedlist/Makefile new file mode 100644 index 00000000..1fcb6fd8 --- /dev/null +++ b/data_structures/stack/stack_linkedlist/Makefile @@ -0,0 +1,12 @@ +CC = gcc +CFLAGS = -c -Wall + +all: main +main: main.o stack.o + $(CC) main.o stack.o -o main + +stack.o: stack.c + $(CC) $(CFLAGS) stack.c + +clean: + rm *o main diff --git a/data_structures/stack/stack_linkedlist/main.c b/data_structures/stack/stack_linkedlist/main.c new file mode 100644 index 00000000..6c8d1477 --- /dev/null +++ b/data_structures/stack/stack_linkedlist/main.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include "stack.h" + +int main() { + Stack_T stk; + stk = Stack_init(); + Stack_push(stk, (int *) 1); + Stack_push(stk, (int *) 2); + Stack_push(stk, (int *) 3); + Stack_push(stk, (int *) 4); + printf("Size: %d\n", Stack_size(stk)); + Stack_print(stk); + Stack_pop(stk); + printf("Stack after popping: \n"); + Stack_print(stk); + Stack_pop(stk); + printf("Stack after popping: \n"); + Stack_print(stk); + return 0; +} diff --git a/data_structures/stack/stack_linkedlist/stack.c b/data_structures/stack/stack_linkedlist/stack.c new file mode 100644 index 00000000..1e4750b3 --- /dev/null +++ b/data_structures/stack/stack_linkedlist/stack.c @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include "stack.h" + +#define T Stack_T + +typedef struct elem { + void *val; + struct elem *next; +} elem_t; + +struct T { + int count; + elem_t *head; +}; + +/* Initial stack */ +T Stack_init (void) { + T stack; + stack = (T) malloc(sizeof(T)); + stack->count = 0; + stack->head = NULL; + return stack; +} + +/* Check empty stack*/ +int Stack_empty(T stack) { + assert(stack); + return stack->count == 0; +} + +/* Return size of the stack */ +int Stack_size(T stack) { + assert(stack); + return stack->count; +} + +/* Push an element into the stack */ +void Stack_push(T stack, void *val) { + elem_t *t; + + assert(stack); + t = (elem_t *) malloc(sizeof(elem_t)); + t->val = val; + t->next = stack->head; + stack->head = t; + stack->count++; +} + +/* Pop an element out of the stack */ +void *Stack_pop(T stack) { + void *val; + elem_t *t; + + assert(stack); + assert(stack->count > 0); + t = stack->head; + stack->head = t->next; + stack->count--; + val = t->val; + free(t); + return val; +} + +/* Print all elements in the stack */ +void Stack_print(Stack_T stack) { + assert(stack); + + int i, size = Stack_size(stack); + elem_t *current_elem = stack->head; + printf("Stack [Top --- Bottom]: "); + for(i = 0; i < size; ++i) { + printf("%p ", (int *)current_elem->val); + current_elem = current_elem->next; + } + printf("\n"); +} diff --git a/data_structures/stack/stack_linkedlist/stack.h b/data_structures/stack/stack_linkedlist/stack.h new file mode 100644 index 00000000..ba82ed21 --- /dev/null +++ b/data_structures/stack/stack_linkedlist/stack.h @@ -0,0 +1,15 @@ +#ifndef __STACK__ +#define __STACK__ + +#define T Stack_T +typedef struct T *T; + +extern T Stack_init (void); +extern int Stack_size (T stack); +extern int Stack_empty (T stack); +extern void Stack_push (T stack, void *val); +extern void *Stack_pop (T stack); +extern void Stack_print (T stack); + +#undef T +#endif