例如:我有一个链表: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
存储函数,用于在节点中存储数据。它是在sort_a((函数之前在主函数中调用的
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 Function在最后一行代码中调用sort_a((函数。
#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);
}
排序函数在我做某事的地方:输入
4->1->0->2->3
#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;
}
}
输出
1->0->2->3->0->4
旋转函数:
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;
}
旋转列表的代码似乎是正确的。然而,打印列表的代码有些令人不安:为什么要修改*a
?您应该只使用一个Stack
指针并按照链接进行操作。
修改您的程序以使用此修改版本打印旋转前后的列表:
void rotate_stack(Stack **a) {
Stack *first = *a;
Stack *last = first;
if (first != NULL && first->next != NULL) {
while (last->next != NULL)
last = last->next;
*a = first->next;
first->next = NULL;
last->next = first;
}
}
void print_stack(const Stack *s) {
while (s != NULL) {
ft_printf("%d ", s->n);
s = s->next;
}
ft_printf("n");
}
int main() {
Stack *s = NULL;
// construct the stack
[...]
print_stack(s);
rotate_stack(&s);
print_stack(s);
return 0;
}