基于双向链表实现的C语言栈无输出问题排查求助
双向链表实现栈打印无输出的问题解决
问题核心是函数参数采用值传递,所有操作栈的函数(push、pop等)修改的都是栈结构体的副本,而非main函数中定义的原栈。原栈s1始终处于初始化的空状态,所以print_s遍历不到任何节点,自然无输出。
具体修改方案
将所有操作栈的函数参数改为指针传递,确保函数内的修改作用于原栈:
- 修改
push函数(新增内存分配失败判断):
void push(Stack *s, int elm) { Node* updated_head = create_node(elm); if (!updated_head) return; if (!s->head) { s->head = updated_head; s->tail = s->head; } else { updated_head->next = s->head; s->head->prev = updated_head; s->head = updated_head; } s->size++; s->top = s->head->data; }
- 修改
pop函数(新增空栈判断,避免空指针访问):
int pop(Stack *s) { if (is_empty_s(*s)) { fprintf(stderr, "栈为空,无法执行pop操作\n"); exit(EXIT_FAILURE); } Node* node = s->head; int elm = node->data; s->head = s->head->next; if (s->head) { s->head->prev = NULL; s->top = s->head->data; } else { s->tail = NULL; s->top = -1; } s->size--; free(node); return elm; }
- 修改
top函数(新增空栈判断):
int top(Stack *s) { if (is_empty_s(*s)) { fprintf(stderr, "栈为空,无法获取top值\n"); exit(EXIT_FAILURE); } return s->top; }
- 修改
clear_s函数:
void clear_s(Stack *s) { while (!is_empty_s(*s)) pop(s); }
- 修改
reverse_s函数(改为直接修改原栈,无需返回值):
void reverse_s(Stack *s) { Stack s2 = stack_init; while (!is_empty_s(*s)) { push(&s2, pop(s)); } *s = s2; }
- 修改
main函数中的调用方式:
int main() { Stack s1 = stack_init; push(&s1, 5); push(&s1, 4); push(&s1, 3); push(&s1, 2); push(&s1, 1); print_s(s1); // print_s仅读取栈内容,值传递即可 // 可选测试反转功能 reverse_s(&s1); printf("反转后:"); print_s(s1); clear_s(&s1); return 0; }
修正后的完整代码
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; typedef struct Stack { int size; Node* head; Node* tail; int top; } Stack; const Stack stack_init = { .size = 0, .head = NULL, .tail = NULL, .top = -1 }; Node* create_node(int elm) { Node* node = malloc(sizeof * node); if (!node) return node; node->data = elm; node->prev = NULL; node->next = NULL; return node; } void push(Stack *s, int elm) { Node* updated_head = create_node(elm); if (!updated_head) return; if (!s->head) { s->head = updated_head; s->tail = s->head; } else { updated_head->next = s->head; s->head->prev = updated_head; s->head = updated_head; } s->size++; s->top = s->head->data; } int pop(Stack *s) { if (is_empty_s(*s)) { fprintf(stderr, "栈为空,无法执行pop操作\n"); exit(EXIT_FAILURE); } Node* node = s->head; int elm = node->data; s->head = s->head->next; if (s->head) { s->head->prev = NULL; s->top = s->head->data; } else { s->tail = NULL; s->top = -1; } s->size--; free(node); return elm; } int top(Stack *s) { if (is_empty_s(*s)) { fprintf(stderr, "栈为空,无法获取top值\n"); exit(EXIT_FAILURE); } return s->top; } void clear_s(Stack *s) { while (!is_empty_s(*s)) pop(s); } void reverse_s(Stack *s) { Stack s2 = stack_init; while (!is_empty_s(*s)) { push(&s2, pop(s)); } *s = s2; } int is_empty_s(Stack s) { return s.tail == NULL; } void print_s(Stack s) { Node* trav = s.head; while (trav) { printf("%d ", trav->data); trav = trav->next; } printf("\n"); } int main() { Stack s1 = stack_init; push(&s1, 5); push(&s1, 4); push(&s1, 3); push(&s1, 2); push(&s1, 1); print_s(s1); reverse_s(&s1); printf("反转后:"); print_s(s1); clear_s(&s1); return 0; }
运行后会输出:
1 2 3 4 5 反转后:5 4 3 2 1
内容的提问来源于stack exchange,提问作者v_head
相关产品推荐
相关产品推荐

