链表头节点移至尾部异常:输出出现多余节点问题
链表头移至尾部的旋转操作异常排查
我在实现将链表头节点移至尾部的旋转操作时遇到问题:现有链表为 4->1->0->2->3,预期旋转后输出 1->0->2->3->4,但实际运行后得到的输出是 1->0->2->3->0->4,无法定位问题所在。相关代码如下:
头文件代码
#ifndef HEADER_PUSH_SWAP_H # define HEADER_PUSH_SWAP_H # include "../libft/libft.h" # include "../ft_printf/include/ft_printf.h" typedef struct s_stack { int n; struct s_stack *next; } Stack; typedef struct s_actions { void (*pa)(Stack **a, Stack **b); void (*pb)(Stack **a, Stack **b); void (*sa)(Stack **a, Stack *x); void (*sb)(Stack **b, Stack *x); void (*ss)(Stack **a, Stack *x, Stack **b, Stack *y); void (*ra)(Stack **a); void (*rb)(Stack **b); void (*rr)(void); void (*rra)(void); void (*rrb)(void); void (*rrr)(void); } Actions; typedef struct s_important { int size; int length; int *collection_of_ints; char *collection; char **split; int a_len; int b_len; } t_important; Actions init(void); //parser functions void stack_nums_counter(char **av, t_important *data); void collect(char **av, t_important *data); void store(Stack **a, t_important *data); //Helpers void __collecting_ints(t_important *data); void __sorted__indacies(t_important *data); void ___bubble___(int *arrtmp, int length); void __store__(t_important *data); int is_sorted(int *ints, int len); int __repeats__(t_important *data); int __check__collection(t_important *data); //Error functions int errno(char *err); //sorting algorithm functions void __sort_a__(Stack **a, Stack **b, t_important *data, Actions action); void pa(Stack **a, Stack **b); void pb(Stack **a, Stack **b); void sa(Stack **a, Stack *x); void sb(Stack **b, Stack *x); void ss(Stack **a, Stack *x, Stack **b, Stack *y); void ra(Stack **a); void rb(Stack **b); void rr(void); void rra(void); void rrb(void); void rrr(void); int check_stack_length(Stack *stack); #endif
节点存储函数store
void store(Stack **a, t_important *data) { int i; Stack *tmp; tmp = *a; (*a)->next = tmp; i = 0; while(i < data->length) { tmp->n = data->collection_of_ints[i]; tmp->next = malloc(sizeof(Stack)); tmp = tmp->next; i++; } tmp->next = NULL; }
主函数main
#include "../includes/header_push_swap.h" int main(int ac, char **av) { Actions action; Stack *a; Stack *b; t_important *data; if(ac < 2) return (-1); data = malloc(sizeof(*data)); stack_nums_counter(av, data); collect(av, data); __check__collection(data); __collecting_ints(data); action = init(); a = malloc(sizeof(*a)); b = malloc(sizeof(*b)); store(&a, data); __sort_a__(&a, &b, data, action); return (0); }
排序函数__sort_a__
#include "../includes/header_push_swap.h" void __sort_a__(Stack **a, Stack **b, t_important *data, Actions action) { action.ra(a); while((*a) != NULL) { ft_printf("%d ", (*a)->n); *a = (*a)->next; } }
旋转函数ra
void ra(Stack **a) { Stack *first = *a; Stack *last = *a; if(check_stack_length(*a) <= 1) return ; while(last->next != NULL) last = last->next; *a = first->next; first->next = NULL; last->next = first; }
问题根源
- 初始循环引用错误:
store函数中(*a)->next = tmp;这行代码将头节点的next指向自身,形成临时循环链表,后续遍历找尾节点时会出现逻辑混乱。 - 多创建空节点:循环执行
data->length次,每次都创建新节点,最终生成data->length + 1个节点(初始头节点+循环中创建的data->length个),最后一个节点的n值未初始化,输出时会出现随机的0。
修复方案
修改store函数,修正链表初始化逻辑,避免循环引用和多余节点:
void store(Stack **a, t_important *data) { int i; Stack *tmp; Stack *prev; // 初始化头节点 (*a)->n = data->collection_of_ints[0]; (*a)->next = NULL; prev = *a; i = 1; // 从第2个元素开始创建节点 while(i < data->length) { tmp = malloc(sizeof(Stack)); tmp->n = data->collection_of_ints[i]; tmp->next = NULL; prev->next = tmp; prev = tmp; i++; } }
修改后,链表会被正确初始化为4->1->0->2->3,调用ra函数后,头节点4被移到尾部,输出结果符合预期的1->0->2->3->4。
内容的提问来源于stack exchange,提问作者Davit
相关产品推荐
相关产品推荐

