【发布时间】:2015-01-27 02:43:47
【问题描述】:
我正在尝试使用 C 交换双向链表中的两个节点。如果我传入一些值,例如列表的头部和尾部,以及介于两者之间的一些值,它就可以工作。然而,在其他人身上,一个人的价值似乎被另一个人覆盖,我陷入了一个循环。
节点/列表:
struct node //node for linked list
{
unsigned long int *id;
char *firstname, *lastname, *department;
float *gpa;
struct node *next, *prev;
};
struct linked_list //doubly linked_list data structure
{
struct node *head, *tail;
};
我可以成功将节点添加到列表中,并将尾部移动到新添加的节点。
void *add_node(struct node **tail, unsigned long int *id, char *first, char *last, char *dept, float *gpa) //create a node, add to tail
{
struct node *newStudent = (struct node*)malloc(sizeof(struct node));
newStudent->firstname = (char*)malloc(strlen(first)+1);
newStudent->lastname = (char*)malloc(strlen(last)+1);
newStudent->department = (char*)malloc(strlen(dept)+1);
newStudent->id = (unsigned long int*)malloc(sizeof(unsigned long int));
newStudent->gpa = (float*)malloc(sizeof(float));
*(newStudent->id) = *id;
*(newStudent->gpa) = *gpa;
strcpy(newStudent->firstname, first);
strcpy(newStudent->lastname, last);
strcpy(newStudent->department, dept);
newStudent->next = NULL;
if(tail) //not the first node in the list
{
newStudent->prev = *tail;
(*tail)->next = newStudent;
*tail = newStudent;
}
else //head of the list
return newStudent;
}
最后,我的交换函数:
void *_swap(struct node **x, struct node **y, struct linked_list **list)
{
struct node *temp = (struct node*)malloc(sizeof(struct node));
memcpy(temp, *x, sizeof(struct node));
if( (*y)->prev ) /// if y has a previous...
{
(*x)->prev = (*y)->prev;
(*y)->prev->next = *x;
}
else
(*x)->prev = NULL;
if( (*y)->next ) /// if y has a next...
{
(*x)->next = (*y)->next;
(*y)->next->prev = *x;
}
else
(*x)->next = NULL;
if( temp->prev) /// if original x has a previous...
{
(*y)->prev = temp->prev;
temp->prev->next = *y;
}
else
(*y)->prev = NULL;
if(temp->next) /// if original x has a next...
{
(*y)->next = temp->next;
temp->next->prev = *y;
}
else
(*y)->next = NULL;
free(temp);
if((*list)->head == *x && (*list)->tail == *y)
{
(*list)->head = *y;
(*list)->tail=*x;
}
else if((*list)->head == *y && (*list)->tail == *x)
{
(*list)->head = *x;
(*list)->tail=*y;
}
else if((*list)->head == *x)
(*list)->head = *y;
else if((*list)->head == *y)
(*list)->head = *x;
else if((*list)->tail == *x)
(*list)->tail = *y;
else if((*list)->tail == *y)
(*list)->tail = *x;
printf("%s %s %s %s %s\n\n\n\n", (*list)->head->firstname, (*list)->head->next->firstname, (*list)->head->next->next->firstname, (*list)->head->next->next->next->firstname, (*list)->head->next->next->next->next->firstname);
}
当我调用类似 temp->next->prev = *y; 在这种情况下,它有时似乎会覆盖 x 的值,而不是简单地将linked_list 指针重新分配给 y。
我能够很好地建立我的列表:
struct linked_list *list = (struct linked_list*)malloc(sizeof(struct linked_list));
list->head = (struct node*)malloc(sizeof(struct node));
list->tail = (struct node*)malloc(sizeof(struct node));
unsigned long int *id = malloc(sizeof(unsigned long int));
*id = 343232;
float gpa = 3.2;
list->head = add_node(NULL, id, "Matthew", "D", "CECS", &gpa);
list->tail = list->head;
add_node(&(list->tail), id, "John", "X", "PNY", &gpa);
add_node(&(list->tail), id, "Rebecca", "H", "ECE", &gpa);
【问题讨论】:
-
没有看代码,所以只是猜测你和this question有同样的问题
-
交换节点函数,不应分配节点。如果需要,它应该只更改受影响节点的前一个和下一个指针,以及列表头和尾指针。要执行最小交换,通常使用临时变量: temp = a; a = b; b = 温度; .在这种情况下,临时变量将是指向节点的指针。如果节点相邻,则需要按特定顺序执行交换操作(操作更像是旋转而不是交换)。
标签: c list data-structures linked-list swap