diff --git a/c-cpp/08_stack/linkList/linklist_stack.c b/c-cpp/08_stack/linkList/linklist_stack.c new file mode 100644 index 0000000..08846bd --- /dev/null +++ b/c-cpp/08_stack/linkList/linklist_stack.c @@ -0,0 +1,136 @@ +/************************************************************************* + > File Name: linklist_stack.c + > Author: jinshaohui + > Mail: jinshaohui789@163.com + > Time: 18-10-12 + > Desc: + ************************************************************************/ +#include +#include +#include +#include "./linklist_stack.h" + +linklist_stack * stack_create() +{ + linklist_stack * stack = NULL; + + stack = (linklist_stack *)malloc(sizeof(linklist_stack)); + if (stack == NULL) + { + return NULL; + } + + stack->next = NULL; + + return stack; +} + +void stack_destory(linklist_stack* stack) +{ + linklist_stack * ptmp = NULL; + + while(!stack_is_empty(stack)) + { + ptmp = stack->next; + stack->next = stack->next->next; + + free(ptmp); + } + + free(stack); + + return; +} + +int stack_push(linklist_stack *stack,int data) +{ + linklist_stack * ptmp = NULL; + + ptmp = (linklist_stack *)malloc(sizeof(linklist_stack)); + if (ptmp == NULL) + { + return -1; + } + + ptmp->data = data; + ptmp->next = stack->next; + stack->next = ptmp; + + return 0; +} + +int stack_pop(linklist_stack*stack,int *data) +{ + linklist_stack *ptmp = NULL; + if (data == NULL) + { + return -1; + } + if(stack_is_empty(stack)) + { + return -1; + } + *data = stack->next->data; + ptmp = stack->next; + stack->next = ptmp->next; + free(ptmp); + + return 0; +} + + +void stack_dump(linklist_stack *stack) +{ + linklist_stack * ptmp = stack->next; + + while(ptmp != NULL) + { + printf("\r\n data = %d",ptmp->data); + ptmp = ptmp->next; + } + return; +} + +int main() +{ + int i = 0; + int ret = 0; + int data = 0; + linklist_stack * stack = NULL; + + stack = stack_create(); + if (stack == NULL) + { + printf("\r\n stack create falied."); + return 0; + } + + for (i = 0; i < 4; i++) + { + ret = stack_push(stack,i); + if(ret != 0) + { + printf("\r\n stack push %d falied.",i); + } + } + + stack_dump(stack); + + for (i = 0; i < 5; i++) + { + ret = stack_pop(stack,&data); + if(ret != 0) + { + printf("\r\n stack pop%d falied.", i); + } + else + { + printf("\r\n data = %d,",data); + } + } + + stack_destory(stack); + + return 0; + +} diff --git a/c-cpp/08_stack/linkList/linklist_stack.h b/c-cpp/08_stack/linkList/linklist_stack.h new file mode 100644 index 0000000..43123b5 --- /dev/null +++ b/c-cpp/08_stack/linkList/linklist_stack.h @@ -0,0 +1,22 @@ +/************************************************************************* + > File Name: linklist_stack.h + > Author: jinshaohui + > Mail: jinshaohui789@163.com + > Time: 18-10-12 + > Desc: + ************************************************************************/ + +#ifndef STACK_LINK_LIST_H +#define STACK_LINK_LIST_H + +typedef struct _linkliststack +{ + int data; + struct _linkliststack *next; +}linklist_stack; + + +#define stack_is_empty(liststack) (liststack->next == NULL) + +#endif +