我需要一些帮助来弄清楚为什么我会遇到这种访问冲突。这是家庭作业,我已经全部写好了,但是在打印列表时最终遇到了访问冲突。我正在尝试正向和反向打印列表。我怀疑问题出在反向功能上。这是代码,感谢您的帮助。
列表.h
typedef int Titem;
// Interface of list
typedef struct node *Tpointer;
typedef struct node
{
Titem item;
Tpointer next, previous;
} Tnode;
typedef struct
{
Tpointer first;
Tpointer last;
}Tdbl;
void initialize_dbl (Tdbl *list);
void insert_to_dbl_front (Tdbl *list, Titem data);
void insert_to_dbl_back (Tdbl *list, Titem data);
void print_dbl (Tdbl const list);
void print_dbl_reverse (Tdbl const list);
列表.c
#include <stdio.h>
#include <stdlib.h>
#include "List.h"
#define TYPE_INT 0
#define TYPE_FLOAT 1
// Implementation of list (only obj is need in appl)
void initialize_dbl (Tdbl *list)
{
list->first = NULL;
list->last = NULL;
}
void insert_to_dbl_front (Tdbl *list, Titem data)
{
Tpointer newnode;
if(list->first == NULL)
{
newnode = (Tpointer) malloc(sizeof(Tnode));
newnode->item = data;
newnode->next = NULL;
newnode->previous = NULL;
list->first = newnode;
}
else
{
newnode = (Tpointer) malloc(sizeof(Tnode));
newnode->item = data;
newnode->next = list->first;
newnode->previous = NULL;
list->first = newnode;
}
}
void insert_to_dbl_back (Tdbl *list, Titem data)
{
Tpointer newnode;
newnode = (Tpointer) malloc(sizeof(Tnode));
newnode -> item = data;
if (list->first == NULL)
list->first = newnode; //first node
else
list->last->next = newnode; //not first node
list->last = newnode;
list->last->next = NULL;
}
void print_dbl (Tdbl const list)
{
Tpointer what;
printf("\nList forward:");
what = list.first;
while (what != NULL) {
printf("%d ", what->item);
what = what->next;
}
}
void print_dbl_reverse (Tdbl const list)
{
Tpointer last = list.last;
Tpointer temp = NULL;
printf("\nList reversed: ");
if(last == NULL)
{
printf("");
}
else
{
while(last != NULL)
{
temp = last->next;
last->next = last->previous;
last->previous = temp;
last = temp;
}
printf("\nList reverse:");
while (last != NULL)
{
printf("%d ", last->item);
last = last->next;
}
}
}
主程序
#include "list.h"
#include <stdio.h>
int main(void) {
Tdbl dbl;
initialize_dbl(&dbl);
print_dbl(dbl);
print_dbl_reverse(dbl);
insert_to_dbl_back(&dbl, 10);
print_dbl(dbl);
print_dbl_reverse(dbl);
insert_to_dbl_front(&dbl, 20);
print_dbl(dbl);
print_dbl_reverse(dbl);
insert_to_dbl_back(&dbl, 30);
print_dbl(dbl);
print_dbl_reverse(dbl);
insert_to_dbl_front(&dbl, 40);
print_dbl(dbl);
print_dbl_reverse(dbl);
insert_to_dbl_back(&dbl, 50);
print_dbl(dbl);
print_dbl_reverse(dbl);
fflush(stdin); getchar();
}
我查看了大约 10 个不同的链接列表示例,并在论坛中搜索了我的问题的答案。我尝试反转列表的每个示例似乎都没有执行任何操作或最终出现此访问冲突错误。哦,是的,主文件或头文件中的任何内容都无法更改。