0

我需要一些帮助来弄清楚为什么我会遇到这种访问冲突。这是家庭作业,我已经全部写好了,但是在打印列表时最终遇到了访问冲突。我正在尝试正向和反向打印列表。我怀疑问题出在反向功能上。这是代码,感谢您的帮助。

列表.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 个不同的链接列表示例,并在论坛中搜索了我的问题的答案。我尝试反转列表的每个示例似乎都没有执行任何操作或最终出现此访问冲突错误。哦,是的,主文件或头文件中的任何内容都无法更改。

4

3 回答 3

1

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;
    }

你没有设置list->last,所以仍然设置为NULL。但是,如果list->first不是NULL

else
{
    newnode = (Tpointer) malloc(sizeof(Tnode));
    newnode->item = data;
    newnode->next = list->first;
    newnode->previous = NULL;
    list->first = newnode;

}

您从未将previous当前第一个节点的指针设置为新节点,因此您实际上并没有双向链表。

当你在后面插入时,

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;
}

您永远不会设置previous新节点的指针。所以你仍然只有一个单链表。仅此一项不会导致访问冲突,但是在这里您从未设置newnode->previous为任何内容,因此它包含恰好位于该内存位置的所有位。

然后在print_dbl_reverse你交换一些previousnext指针并有

while (last != NULL) 
{
    printf("%d ", last->item);
    last = last->next;
}

在某些时候设置last为未初始化的非NULL指针,这会导致访问冲突。

于 2012-04-24T15:44:17.823 回答
0

您的代码中有很多错误。我写了一个简单的,所以你可以研究它,但把反向函数留给你。玩得开心!

#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 = (Tpointer) malloc(sizeof(Tnode));;
    newnode->item = data;
    newnode->previous = NULL;
    newnode->next = list->first;

    if(list->first != NULL) 
        list->first->previous = newnode;
    else 
        list->last = newnode;

    list->first = newnode;
}

void insert_to_dbl_back (Tdbl *list, Titem data)
{
    Tpointer newnode = (Tpointer) malloc(sizeof(Tnode));
    newnode->item = data;
    newnode->next = NULL;
    newnode->previous = list->last;

    if (list->first == NULL)
        list->first = newnode;
    else
        list->last->next = newnode;

    list->last = newnode;
}

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 = last;

    printf("\nList reverse: ");
    while (last != NULL) 
    {
            last = last->previous;
    }
}
于 2012-04-24T15:41:56.987 回答
0

以下是调试此问题的一些建议。

1) 修改您的源代码以打印调试语句。您甚至可以根据#ifdef DEBUG_STATEMENTS 标志编译出调试信息。

2)您可以使用调试器并在程序崩溃之前单步执行和/或设置断点。如果程序不是守护程序/系统服务或驱动程序,调试器将很容易工作。

其他建议我真的围绕实现而不是调试,并且与预分配内存和在预分配内存中操作您的链表有关。但是,因为这比前两个建议更复杂,所以我将停止对该想法的概述。

祝你好运。

于 2012-04-24T15:56:12.863 回答