【发布时间】:2019-07-26 13:21:39
【问题描述】:
我想打印我的链表的内容,它来自一个文件,如下:
1,Postit Notes,Sticky notes,3
2,Black pens,Gel pens with black ink,5
3,Blue pens, Gel pens with blue ink, 4
4, Red pens, Gel pens with red ink for grading, 3
5, Notecards,Ruled 3" by 5" notecards,2
7,Whiteout,For mistakes made when writting with ink,3
到目前为止,在我的程序中,我的输入被正确读取,并且我已经调试了该部分,所以我知道这不是问题所在。我在打印链接列表时遇到问题。
我尝试编写addRecord 方法(如下所示),在该方法中,我为新节点分配内存,分配所需的数据,并尝试将其添加到链表的末尾。我还有一个打印链表内容的方法,如下图:
static void *addRecord(List *list, int newID, char *newName, char *newSummary, int newCount)
{
//Allocate memory for the node
Node *new = (Node *)malloc(sizeof(Node));
//Add in data
new->id = newID;
strcpy(new->name, newName);
strcpy(new->summary, newSummary);
new->count = newCount;
//Node gets data you added in
new->next = list->head;
list->head = new;
return EXIT_SUCCESS;
}
void print(List *list)
{
printf("LIST IN FORWARD ORDER:\n");
//Create a temporary node to traverse the list
Node *temp = list->head;
//Traverse the entire list
while (temp != NULL) {
printf("Item ID: %d\n", temp->id);
printf("Name: %s\n", temp->name);
printf("Summary: %s\n", temp->summary);
printf("Count: %d\n", temp->count);
printf("-----\n");
temp = temp->next;
}
}
这是我的链接列表的设置方式:
//struct for each office item
struct NodeStruct {
int id;
char name[MAX_NAME];
char summary[MAX_SUM];
int count;
struct NodeStruct *next;
};
/** Structure for the whole list, including head and tail pointers. */
typedef struct {
/** Pointer to the first node on the list (or NULL ). */
Node *head;
} List;
我的预期输出应该是这样的:
LIST IN FORWARD ORDER:
Item ID: 1
Name: Postit Notes
Summary: Sticky notes
Count: 3
-----
Item ID: 2
Name: Black pens
Summary: Gel pens with black ink
Count: 5
-----
Item ID: 3
Name: Blue pens
Summary: Gel pens with blue ink
Count: 4
-----
Item ID: 4
Name: Red pens
Summary: Gel pens with red ink for grading
Count: 3
-----
Item ID: 5
Name: Notecards
Summary: Ruled 3" by 5" notecards
Count: 2
-----
Item ID: 7
Name: Whiteout
Summary: For mistakes made when writting with ink
Count: 3
-----
但是,我的实际输出是:
LIST IN FORWARD ORDER:
Item ID: 7
Name: Whiteout
Summary: For mistakes made when writting with ink
Count: 3
-----
Item ID: 5
Name: Notecards
Summary: Ruled 3" by 5" notecards
Count: 2
-----
Item ID: 4
Name: Red pens
Summary: Gel pens with red ink for grading
Count: 3
-----
Item ID: 3
Name: Blue pens
Summary: Gel pens with blue ink
Count: 4
-----
Item ID: 2
Name: Black pens
Summary: Gel pens with black ink
Count: 5
-----
Item ID: 1
Name: Postit Notes
Summary: Sticky notes
Count: 3
-----
在我的打印方法中,当我尝试打印头节点的内容时,我从 ID 为 7 的项目中获取数据/信息,而我应该获取 ID 为 1 的项目的信息。有人可以解释一下吗我的为什么我的列表是向后打印的?我试图追踪它,但我有点困惑。我尝试了其他方法来添加记录,但是当我尝试这样做时,我不断收到Segmentation Fault: 11。
提前谢谢你!
编辑:
在下面 Thomas Jager 的评论之后,我对我的 addRecord 方法进行了以下修改:
static void *addRecord(List *list, int newID, char *newName, char *newSummary, int newCount)
{
//Allocate memory for the node
Node *new = (Node *)malloc(sizeof(Node));
//Add in data
new->id = newID;
strcpy(new->name, newName);
strcpy(new->summary, newSummary);
new->count = newCount;
//Special case: If the first node is null, add the data here
if (list->head->next == NULL) {
list->head->next = new;
} else {
Node *temp = new;
while (temp != NULL) {
new = new->next;
}
}
return EXIT_SUCCESS;
}
但是,现在,我收到以下错误:
Segmentation fault: 11
我无法理解发生的分段错误错误,有人可以向我解释一下吗?
【问题讨论】:
-
添加到列表时,您添加到列表的前面。它不是向后打印,而是向后打印。
-
如果要添加到列表尾部,需要在
List结构体中维护列表尾部。在Node *head;之后添加Node *tail;,并更新addRecord中的tail指针。 Ylou 可能希望能够添加到头部或尾部。这是一个很好的练习。 -
嗨@ThomasJager,非常感谢您指出这一点。您能否查看我对上述问题的编辑,因为我仍然遇到一些错误并且不确定我做错了什么导致分段错误?
-
@PomegranateSociety 在您修改后的代码中,您访问
new->next,这几乎肯定是一些垃圾值。您想做类似的事情,但通过现有列表的next值,而不是new的值。请参阅下面的答案。
标签: c data-structures printing linked-list insert