无法在双重 link 列表中从尾部遍历到头部
Can't traverse from tail to head in doubly link list
我已经用 C 编写了双重 link 列表的代码,它从头到尾很好地遍历,但是在从尾(尾)到头遍历时它陷入了无限循环并打印只有最后一个节点的数据,我不知道出了什么问题。
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
};
typedef struct node list;
list *head, *current, *newn, *end;
int main() {
int x, y;
current = (list*)malloc(sizeof(list));
printf("enter data:");
scanf("%d", ¤t->data);
current->next = NULL;
current->prev = NULL;
head = current;
printf("do you want to enter more:");
scanf("%d", &x);
while (x == 1) {
current->next = (list*)malloc(sizeof(list));
printf("enter data:");
scanf("%d", ¤t->next->data);
current->next->prev = current->next;
current->next->next = NULL;
current = current->next;
end = current;
printf("do yo want to enter more:");
scanf("%d", &x);
}
newn = head;
while (newn != NULL) {
printf("%d:%d:%d->", newn->prev, newn->data, newn->next);
newn = newn->next;
}
while (end->prev != NULL) {
printf("%d", end->data);
end = end->prev;
}
}
整理一下评论的答案,这里是固定代码:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
typedef struct node list;
list *head, *current, *newn, *end;
void exitWithFail() {
printf("Error: exiting.\n");
exit(1);
}
int main()
{
int x,y,r;
current = (list*)malloc(sizeof(list));
printf("enter data:");
r = scanf("%d",¤t->data);
if (r != 1) exitWithFail();
current->next = NULL;
current->prev = NULL;
head = end = current;
printf("do you want to enter more:");
r = scanf("%d",&x);
if (r != 1) exitWithFail();
while(x == 1)
{
current->next = (list*)malloc(sizeof(list));
printf("enter data:");
r = scanf("%d",¤t->next->data);
if (r != 1) exitWithFail();
current->next->prev = current; // problem No.1
current->next->next = NULL;
current = current->next;
end = current;
printf("do yo want to enter more:");
r = scanf("%d",&x);
if (r != 1) exitWithFail();
}
newn = head;
while(newn != NULL)
{
printf("%d ",newn->data);
newn = newn->next;
}
printf("\n");
while(end != NULL) // Problem No. 2
{
printf("%d ",end->data);
end = end->prev;
}
printf("\n");
}
我已经用 C 编写了双重 link 列表的代码,它从头到尾很好地遍历,但是在从尾(尾)到头遍历时它陷入了无限循环并打印只有最后一个节点的数据,我不知道出了什么问题。
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
};
typedef struct node list;
list *head, *current, *newn, *end;
int main() {
int x, y;
current = (list*)malloc(sizeof(list));
printf("enter data:");
scanf("%d", ¤t->data);
current->next = NULL;
current->prev = NULL;
head = current;
printf("do you want to enter more:");
scanf("%d", &x);
while (x == 1) {
current->next = (list*)malloc(sizeof(list));
printf("enter data:");
scanf("%d", ¤t->next->data);
current->next->prev = current->next;
current->next->next = NULL;
current = current->next;
end = current;
printf("do yo want to enter more:");
scanf("%d", &x);
}
newn = head;
while (newn != NULL) {
printf("%d:%d:%d->", newn->prev, newn->data, newn->next);
newn = newn->next;
}
while (end->prev != NULL) {
printf("%d", end->data);
end = end->prev;
}
}
整理一下评论的答案,这里是固定代码:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
typedef struct node list;
list *head, *current, *newn, *end;
void exitWithFail() {
printf("Error: exiting.\n");
exit(1);
}
int main()
{
int x,y,r;
current = (list*)malloc(sizeof(list));
printf("enter data:");
r = scanf("%d",¤t->data);
if (r != 1) exitWithFail();
current->next = NULL;
current->prev = NULL;
head = end = current;
printf("do you want to enter more:");
r = scanf("%d",&x);
if (r != 1) exitWithFail();
while(x == 1)
{
current->next = (list*)malloc(sizeof(list));
printf("enter data:");
r = scanf("%d",¤t->next->data);
if (r != 1) exitWithFail();
current->next->prev = current; // problem No.1
current->next->next = NULL;
current = current->next;
end = current;
printf("do yo want to enter more:");
r = scanf("%d",&x);
if (r != 1) exitWithFail();
}
newn = head;
while(newn != NULL)
{
printf("%d ",newn->data);
newn = newn->next;
}
printf("\n");
while(end != NULL) // Problem No. 2
{
printf("%d ",end->data);
end = end->prev;
}
printf("\n");
}