你的代码中有很多东西会跳出来 .
你分配了很多东西,经常是不必要和无用的 . 正如rcgldr所指出的,交换函数不应该分配新节点 . 毕竟,在交换之后,列表由相同的节点组成,只是以不同的顺序 . 没有新节点 .
您的"client code",即使用链接列表函数的函数,在您的示例中可能是 main ,不应该显式分配内存 . 也不应该手动填充节点 . 它应该只调用 add_node 和 delete_node ,你也应该编码以释放所有分配的内存 .
没有必要在您的情况下传递指针指针 . 传递指向节点和列表结构的指针就足够了 . 这允许您更改结构的字段 . 指向struct的指针只有在你想改变结构句柄本身时才有意义,例如通过重新分配它,但你不这样做 . (指向指针通常用于单链表,其中头不存储在结构中 . 即使在那里,将单个指针包装在结构中也是有用的,这样就不需要指针指针了 . . )
所有逻辑都应该在你的函数中发生 . 不要修改'main中的next和prev` 指针;这就是功能的用途 . 当你调用一个函数并从它返回时,某些"invariants"应该成立,例如:
当列表为空时, head 和 tail 都是 NULL .
否则, head 指向第一个节点; 'head-> prev is NULL . The tail points to the last node; ´tail->next 是 NULL .
当节点 nd 具有上一个节点时,则 nd->prev->next == nd .
同样,当节点 nd 具有下一个节点时,则为 nd->next->prev == nd .
您甚至可以编写一个完整性检查函数来强制执行函数进入和退出时的这些invarants .
您为所有字段分配数据 . 内存分配对于字符串是有意义的,字符串是字符数组,其长度你对标量变量 id 和 gpa 有意义 . 您可以将它们声明为非指针并仅指定它们 . (分配内存并通过指针访问它们并没有错,但直接访问要简单得多 . )
某些函数返回 void * ,void指针 . 那不是你想要的 . 您的函数应该是 void ,即没有返回值,或者它们应该返回指向节点的指针 . (void指针是一种合法的数据类型,它指的是指向任何数据类型的指针,你不能解除引用 . 它用于通用函数,如 qsort ,不应该在你的代码中使用 . 你不是在编写泛型函数,但功能为您的具体链表 . )
您可以将交换视为删除节点并在各自的旧前任之后重新插入它们 . 您仍然需要注意捕获节点相邻的情况 .
这是一个示例实现,试图尊重我上面提到的要点:
#include
#include
#include
typedef unsigned long int ulong;
struct node
{
ulong id;
char *name;
float gpa;
struct node *next;
struct node *prev;
};
struct list
{
struct node *head;
struct node *tail;
};
/*
* Create a new, unconnected node
*/
struct node *node_new(ulong id, const char *name, float gpa)
{
struct node *node = malloc(sizeof(*node)); // Error checking!
node->prev = NULL;
node->next = NULL;
node->id = id;
node->gpa = gpa;
node->name = malloc(strlen(name) + 1);
strcpy(node->name, name);
return node;
}
/*
* Create a list
*/
struct list *list_new()
{
struct list *list = malloc(sizeof(*list)); // Error checking!
list->head = list->tail = NULL;
return list;
}
/*
* Add a student to list
*/
struct node *list_add(struct list *list,
ulong id, const char *name, float gpa)
{
struct node *node = node_new(id, name, gpa);
node->prev = list->tail;
if (list->tail == NULL) {
list->head = list->tail = node;
} else {
list->tail->next = node;
list->tail = node;
}
return node;
}
/*
* Delete a node from the list.
*/
void list_delete(struct list *list, struct node *node)
{
if (node->prev) node->prev->next = node->next;
else list->head = node->next;
if (node->next) node->next->prev = node->prev;
else list->tail = node->prev;
free(node->name);
free(node);
}
/*
* Find student by id; return NULL if not found.
*/
struct node *list_find_by_id(const struct list *list, ulong id)
{
struct node *node = list->head;
while (node) {
if (node->id == id) return node;
node = node->next;
}
return NULL;
}
/*
* Extract a node without deleting
*/
void list_remove(struct list *list, struct node *node)
{
if (node->prev) node->prev->next = node->next;
else list->head = node->next;
if (node->next) node->next->prev = node->prev;
else list->tail = node->prev;
node->prev = node->next = NULL;
}
/*
* Insert node after prev or at the front when prev is NULL
*/
void list_insert_after(struct list *list,
struct node *node, struct node *prev)
{
if (prev) {
node->next = prev->next;
prev->next = node;
} else {
node->next = list->head;
list->head = node;
}
node->prev = prev;
if (node->next) node->next->prev = node;
}
/*
* Swap two nodes' positions in the list
*/
void list_swap(struct list *list, struct node *x, struct node *y)
{
if (x == y) return;
struct node *xprev = x->prev;
struct node *yprev = y->prev;
if (xprev == y) {
list_remove(list, x);
list_insert_after(list, x, yprev);
} else if (yprev == x) {
list_remove(list, y);
list_insert_after(list, y, xprev);
} else {
list_remove(list, x);
list_remove(list, y);
list_insert_after(list, x, yprev);
list_insert_after(list, y, xprev);
}
}
/*
* Print list
*/
void list_print(const struct list *list)
{
const struct node *node = list->head;
while (node) {
printf("%8lu %-20s %8.1f\n", node->id, node->name, node->gpa);
node = node->next;
}
printf("\n");
}
/*
* Delete a list and all its nodes
*/
void list_destroy(struct list *list)
{
while (list->head) list_delete(list, list->head);
free(list);
}
/*
* Example client code using the list
*/
int main()
{
struct list *list = list_new();
list_add(list, 342232, "Matthew", 3.2);
list_add(list, 342856, "John", 1.9);
list_add(list, 342109, "Rebecca", 6.4);
list_add(list, 342834, "Shirley", 2.6);
list_add(list, 343009, "Simon", 1.4);
list_add(list, 342170, "Antonio", 3.5);
list_print(list);
struct node *simon = list_find_by_id(list, 343009);
struct node *becky = list_find_by_id(list, 342109);
if (simon && becky) {
list_swap(list, simon, becky);
list_print(list);
}
list_destroy(list);
return 0;
}