剑指offer之用链表实现栈(带头节点)
发布日期:2021-06-29 14:13:27 浏览次数:2 分类:技术文章

本文共 3612 字,大约阅读时间需要 12 分钟。

1 问题

用链表实现栈,栈先进后出.

 

 

 

 

2 代码实现

#include 
#include
#define true 1#define false 0typedef struct Node{ int value; struct Node *next;} Stack;/* *打印栈 */void print(Stack *stack){ if (stack == NULL) { printf("stack is NULL\n"); return; } struct Node *p = stack->next; while (p != NULL) { printf("value is: %d\n", p->value); p = p->next; } return;}/** *给栈添加一个节点 */int add_node(Stack *stack, int value){ if (stack == NULL) { printf("stack is NULL\n"); return false; } struct Node *node = NULL; node = (struct Node *)malloc(sizeof(struct Node)); if (node == NULL) { printf("addNode malloc fail\n"); return false; } node->value = value; node->next = stack->next; stack->next = node; return true;}/* *初始化栈 */struct Node* init(){ struct Node *head = NULL; head = (struct Node *)malloc(sizeof(struct Node)); if (head == NULL) { return NULL; } head->next = NULL; head->value = 0; return head;}/* *打印栈的大小 */int size_stack(Stack *stack){ if (stack == NULL) { return 0; } Stack *head = stack->next; int size = 0; while (head != NULL) { ++size; head = head->next; } return size;} /* *弹出栈顶元素 */int pop_stack(Stack *stack){ if (stack == NULL) { printf("stack is NULL"); return false; } struct Node *p = stack->next; if (p == NULL) { printf("stack->next is NULL"); return false; } stack->next = p->next; free(p); return true;}/* *获取栈顶元素 */struct Node* top_stack(Stack *stack){ /**if (stack == NULL);这里自己傻逼了,多加了一个分号导致程序走到里面 { printf("stack1 is NULL\n"); return NULL; }**/ if (stack == NULL) { printf("stack is is is NULL\n"); return NULL; } struct Node *p = stack->next; if (p == NULL) { printf("stack->next is NULL"); return NULL; } return p;}void clear_stack(Stack *stack){ if (stack == NULL) { return; } struct Node *q, *p = stack->next; while(p != NULL) { q = p; p = p->next; free(q); } stack->next = NULL;}int main(){ struct Node *head = init(); if (head == NULL) { printf("init stack fail\n"); return false; } printf("init success\n"); add_node(head, 1); add_node(head, 2); add_node(head, 5); add_node(head, 4); add_node(head, 3); print(head); struct Node* top = top_stack(head); if (top != NULL) { printf("top value is %d\n", top->value); } printf("stack size is %d\n", size_stack(head)); int result = pop_stack(head); if (result == true) { printf("pop_stack success\n"); } else { printf("pop_stack fail\n"); } print(head); printf("stack size is %d\n", size_stack(head)); clear_stack(head); if (head == NULL) { printf("head is NULL\n"); } printf("stack size is %d\n", size_stack(head)); head = init(); if (head == NULL) { printf("init stack fail\n"); return false; } printf("init success\n"); add_node(head, 6); add_node(head, 5); add_node(head, 2); add_node(head, 1); add_node(head, 9); print(head); printf("stack size is %d\n", size_stack(head)); return true;}

 

 

 

 

3 运行结果

init successvalue is: 3value is: 4value is: 5value is: 2value is: 1top value is 3stack size is 5pop_stack successvalue is: 4value is: 5value is: 2value is: 1stack size is 4stack size is 0init successvalue is: 9value is: 1value is: 2value is: 5value is: 6stack size is 5

 

转载地址:https://chenyu.blog.csdn.net/article/details/86451530 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:WireShark之抓包过滤链接部分
下一篇:WireShark抓DNS请求和回复数据报的分析

发表评论

最新留言

很好
[***.229.124.182]2024年04月23日 05时42分18秒