diff --git a/codes/c/chapter_stack_and_queue/linkedlist_stack.c b/codes/c/chapter_stack_and_queue/linkedlist_stack.c index fe457afd..714ed1fb 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_stack.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_stack.c @@ -6,74 +6,87 @@ #include "../include/include.h" +/* 基于链表实现的栈 */ struct LinkedListStack { - ListNode* stkTop; - size_t stkSize; + ListNode* stackTop; // 将头结点作为栈顶 + size_t stkSize; // 栈的长度 }; typedef struct LinkedListStack LinkedListStack; -void new(LinkedListStack* stk) { - // 创建头结点 - stk->stkTop = (ListNode *)malloc(sizeof(ListNode)); - stk->stkTop->next = NULL; - stk->stkTop->val = 0; - - // 初始化栈大小 +void constructor(LinkedListStack* stk) { + stk->stackTop = NULL; stk->stkSize = 0; } - +/* 获取栈的长度 */ size_t size(LinkedListStack* stk) { assert(stk); return stk->stkSize; } - +/* 判断栈是否为空 */ bool empty(LinkedListStack* stk) { assert(stk); return size(stk) == 0; } - -void push(LinkedListStack* stk, int num) { - assert(stk); - - // 创建一个新结点 - ListNode *n = (ListNode *)malloc(sizeof(ListNode)); - ListNode *h; - n->next = NULL; - n->val = num; - - // 遍历链表,将新结点挂在最后面 - h = stk->stkTop; - while(h && h->next) { - h = h->next; - } - h->next = n; - - // 栈大小自增 - stk->stkSize++; -} - -void pop(LinkedListStack* stk) { - assert(stk); - ListNode *h = stk->stkTop; - ListNode *n; - // 找到倒数第一个结点 h -> (h->next) -> (h->next->next) - while(h && h->next && h->next->next) { - h = h->next; - } - - // 先保存倒数个结点,断开 - n = h->next; - h->next = NULL; - - // 删除保存的结点 - free(n); -} - +/* 访问栈顶元素 */ int top(LinkedListStack* stk) { assert(stk); + if (size(stk) == 0 ) + // 抛出异常 + return stk->stackTop->val; +} +/* 入栈 */ +void push(LinkedListStack* stk, int num) { + assert(stk); + ListNode *node = (ListNode *)malloc(sizeof(ListNode)); + node->next = stk->stackTop; + node->val = num; + stk->stackTop = node; + stk->stkSize++; +} +/* 出栈 */ +void pop(LinkedListStack* stk) { + assert(stk); + int num = top(stk); + ListNode *tmp = stk->stackTop; + stk->stackTop = stk->stackTop->next; + // 释放内存 + free(tmp); + stk->stkSize--; } + +/* Driver Code */ int main() { + /* 初始化栈 */ + LinkedListStack stack; + + /* 元素入栈 */ + push(&stack, 1); + push(&stack, 3); + push(&stack, 2); + push(&stack, 5); + push(&stack, 4); + + printf("栈 stack = "); + printStack(&stack); + + /* 访问栈顶元素 */ + int stackTop = top(&stack); + printf("栈顶元素 top = %d\r\n", stackTop); + + /* 元素出栈 */ + pop(&stack); + printf("出栈元素 pop = %d, 出栈后 stack = ", stackTop); + printStack(&stack); + + /* 获取栈的长度 */ + size_t stackSize = size(&stack); + printf("栈的长度 size = %ld\r\n", stackSize); + + /* 判断是否为空 */ + bool isEmpty = empty(&stack); + printf("栈是否为空 = %d", isEmpty); + return 0; } diff --git a/codes/c/include/print_util.h b/codes/c/include/print_util.h index a0c3a150..f7955b58 100644 --- a/codes/c/include/print_util.h +++ b/codes/c/include/print_util.h @@ -127,6 +127,14 @@ static void printTree(TreeNode *root) { printTreeHelper(root, NULL, false); } +/** + * @brief Print a stack + * + * @param head + */ +static void printStack(void *stack) { + +} #ifdef __cplusplus }