【发布时间】:2021-05-01 20:28:15
【问题描述】:
我是 C 和数据结构的初学者,遇到了一个令人沮丧的异常。对比了其他的双向链表代码,没有发现错误。
在调试代码时,我从 stdio.h 收到有关读取访问冲突的警告,这是问题所在:
return __stdio_common_vfprintf(_CRT_INTERNAL_LOCAL_PRINTF_OPTIONS, _Stream, _Format, _Locale, _ArgList);
你能帮帮我吗?
struct Node* NewNode() {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node*));
new_node->next = NULL;
new_node->prev = NULL;
return new_node;
}
void InsertElement(char con, char name[51]) {
struct Node* new_node = NewNode();
strcpy(new_node->name,name);
if (head == NULL) {
head = new_node;
tail = head;
return;
}
if (con == 'H') {
head->prev = new_node;
new_node->next = head;
head = new_node;
}
else if (con == 'T') {
tail->next = new_node;
new_node->prev = tail;
tail = new_node;
}
}
void DisplayForward() {
if (head == NULL) {
printf("No Songs To Print\n*****\n");
return;
}
struct Node *temp = head;
while (temp != NULL) {
printf("%s\n", temp->name);
temp = temp->next;
}
printf("*****\n");
}
void DisplayReversed() {
if (head == NULL) {
printf("No Songs To Print\n*****\n");
return;
}
struct Node *temp = tail;
while (temp != NULL) {
printf("%s\n", temp->name);
temp = temp->prev;
}
printf("*****\n");
}
【问题讨论】:
-
提供一个演示问题的最小完整程序。
标签: c linked-list dynamic-memory-allocation singly-linked-list function-definition