双向链表工作不正常
Doubly linked list working improperly
我有一个双向链表。当我告诉它向前打印列表中的项目时,它工作正常。当我告诉它向后打印列表中的项目时,它 returns 300 作为最后一个值,而它应该 return 100。我在节点的定义中找不到任何错误(它们是按顺序定义的)或在 dubblePrev 中(这是一个相当简单的递归函数),尽管我对指针(以及,通过扩展,链表)不熟悉,所以我可能错过了一些看起来相当明显的东西。这是怎么回事?
这是我的代码:
// Doubly linked list
#include <stdio.h>
struct entry
{
int value;
struct entry *next;
struct entry *prev;
};
void dubbleNext (struct entry *e) {
if (e != '[=10=]') {
printf ("%d\n", e->value);
dubbleNext(e->next);
}
}
void dubblePrev (struct entry *e) {
if (e != '[=10=]') {
printf ("%d\n", e->value);
dubbleNext(e->prev);
}
}
int main (void)
{
struct entry n1, n2, n3;
int i;
n1.value = 100;
n2.value = 200;
n3.value = 300;
n1.next = &n2;
n2.next = &n3;
n3.next = NULL;
n1.prev = NULL;
n2.prev = &n1;
n3.prev = &n2;
dubbleNext (&n1);
dubblePrev (&n3);
return 0;
}
您的 "doublePrev" 函数中有错字。试试这个。
void dubblePrev (struct entry *e) {
if (e != '[=10=]') {
printf ("%d\n", e->value);
dubblePrev(e->prev);
}
我有一个双向链表。当我告诉它向前打印列表中的项目时,它工作正常。当我告诉它向后打印列表中的项目时,它 returns 300 作为最后一个值,而它应该 return 100。我在节点的定义中找不到任何错误(它们是按顺序定义的)或在 dubblePrev 中(这是一个相当简单的递归函数),尽管我对指针(以及,通过扩展,链表)不熟悉,所以我可能错过了一些看起来相当明显的东西。这是怎么回事?
这是我的代码:
// Doubly linked list
#include <stdio.h>
struct entry
{
int value;
struct entry *next;
struct entry *prev;
};
void dubbleNext (struct entry *e) {
if (e != '[=10=]') {
printf ("%d\n", e->value);
dubbleNext(e->next);
}
}
void dubblePrev (struct entry *e) {
if (e != '[=10=]') {
printf ("%d\n", e->value);
dubbleNext(e->prev);
}
}
int main (void)
{
struct entry n1, n2, n3;
int i;
n1.value = 100;
n2.value = 200;
n3.value = 300;
n1.next = &n2;
n2.next = &n3;
n3.next = NULL;
n1.prev = NULL;
n2.prev = &n1;
n3.prev = &n2;
dubbleNext (&n1);
dubblePrev (&n3);
return 0;
}
您的 "doublePrev" 函数中有错字。试试这个。
void dubblePrev (struct entry *e) {
if (e != '[=10=]') {
printf ("%d\n", e->value);
dubblePrev(e->prev);
}