【发布时间】:2019-07-27 13:55:02
【问题描述】:
我的最小可重现示例:
#include <stdio.h>
#include <stdlib.h>
typedef struct NodeStruct Node;
//struct for each office item
struct NodeStruct {
int id;
struct NodeStruct *next;
struct NodeStruct *prev; //Create doubly linked list node
};
/** Structure for the whole list, including head and tail pointers. */
typedef struct {
/** Pointer to the first node on the list (or NULL ). */
Node *head;
Node *last;
} List;
List *list;
List *makeList();
static void *addRecord(List *list, int newID);
static void printReverse(List *list);
int main(int argc, char **argv) {
//Create an empty list for you to start.
list = (List *)makeList();
addRecord(list, 1);
addRecord(list, 2);
addRecord(list, 3);
addRecord(list, 4);
addRecord(list, 15);
printReverse(list);
return 0;
}
List *makeList() {
List *list = (List *)malloc(sizeof(List));
list->head = NULL;
return list;
}
static void *addRecord(List *list, int newID) {
//Allocate memory for the node
Node *new = (Node *)malloc(sizeof(Node));
//Add in data
new->id = newID;
//New node has no next, yet
new->next = NULL;
Node **next_p = &list->head;
while (*next_p) {
next_p = &(*next_p)->next;
}
*next_p = new;
return EXIT_SUCCESS;
}
static void printReverse(List *list) {
Node **tail = &list->last;
printf("LIST IN REVERSE ORDER:\n");
//Traversing until tail end of linked list
while (*tail) {
printf("Item ID: %d\n", (*tail)->id);
tail = &(*tail)->prev;
}
}
输入:
1 -> 2 -> 3 -> 4 -> 15
预期输出:
15 -> 4 -> 3 -> 2 -> 1
实际输出:
分段错误
编辑:在链表中设置prev节点:
#include <stdio.h>
#include <stdlib.h>
typedef struct NodeStruct Node;
//struct for each office item
struct NodeStruct {
int id;
struct NodeStruct *next;
struct NodeStruct *prev; //Create doubly linked list node
};
/** Structure for the whole list, including head and tail pointers. */
typedef struct {
/** Pointer to the first node on the list (or NULL ). */
Node *head;
Node *last;
} List;
List *list;
List *makeList();
static void *addRecord(List *list, int newID);
static void printReverse(List *list);
int main(int argc, char **argv) {
// Create an empty list for you to start.
list = (List *)makeList();
addRecord(list, 1);
addRecord(list, 2);
addRecord(list, 3);
addRecord(list, 4);
addRecord(list, 15);
printReverse(list);
return 0;
}
List *makeList() {
List *list = (List *)malloc(sizeof(List));
list->head = NULL;
return list;
}
static void *addRecord(List *list, int newID) {
//Allocate memory for the node
Node *new = (Node *)malloc(sizeof(Node));
//Add in data
new->id = newID;
new->prev = NULL;
//New node has no next, yet
new->next = NULL;
Node **next_p = &list->head;
while (*next_p) {
next_p = &(*next_p)->next;
}
*next_p = new;
list->last = new;
new->prev = *next_p;
return EXIT_SUCCESS;
}
static void printReverse(List *list) {
Node **tail = &list->last;
printf("LIST IN REVERSE ORDER:\n");
//Traversing until tail end of linked list
while (*tail) {
printf("Item ID: %d\n", (*tail)->id);
tail = &(*tail)->prev;
}
}
对addRecord 进行此编辑后,我不断得到一个无限循环,一遍又一遍地打印Item ID: 15。
【问题讨论】:
-
不应该是tail = &(*tail)->prev吗?为什么要使用指向指针的指针?无论如何,请提供一个可重现的示例,可能是您没有将某些指针或其他问题设置为 NULL。
-
我正在努力获得一个最小的可重现示例!
标签: c pointers printf nodes doubly-linked-list