初始化两个双向链表C++

Initializing two doubly linked lists C++

我正在尝试将我的两个双向链表初始化为空,但我的程序崩溃了。该程序从文本文件中分离偶数和奇数整数。我相信问题出在 InitializeList 函数中,但我尝试了几种不同的方法都无济于事。我需要将它们都初始化为:

Odds->top = NULL;
Odds->length = 0;

Evens->top = NULL;
Evens->length = 0;

谁能指出我做错了什么?

代码:

#include <iostream>
#include <fstream>
#include <cstddef>
#include <stdlib.h>
using namespace std;

struct node{
    int integer;
    node *next;
    node *prev;
};
struct list{
    int length;
    node *top;
};
bool EmptyList(list* head)
{
    bool empty;

    if(head == NULL)
        empty = true;
    else
        empty = false;

    return empty;
}

list* InitializeList(list* A_list)
{
    A_list->top = NULL;
    A_list->length = 0;
    return A_list;
}
bool OrderedListInsertion(list* &A_list, int number, int &counter)
{
    bool repeat,
         success;
    node *newOdd;
    node *newEven;
    node *EvenHead;
    node *oddHead;



    if((number % 2) == 0)
    {
        A_list = new list;
        newEven = new (nothrow) node;
        A_list->top = newEven;
        if(counter == 0)
        {
            if(newEven == NULL)
            {
                cout << "ERROR. Memory allocation failed!" << endl;
                success = false;
            }
            else
            {
                newEven->integer = number;
                newEven->next = NULL;
                newEven->prev = NULL;
                EvenHead = newEven;
                success = true;
            }
        }
        else
        {
            if(newEven == NULL)
            {
                cout << "ERROR. Memory allocation failed!" << endl;
                success = false;
            }
            else
            {
                newEven->integer = number;
                newEven->prev = EvenHead;
                newEven->next = NULL;
                EvenHead = newEven;
                success = true;
            }
        }
    }
    if((number % 2) != 0)
    {
        A_list = new list;
        if(counter == 0)
        {
            newOdd = new (nothrow) node;
            if(newOdd == NULL)
            {
                cout << "ERROR. Memory allocation failed!" << endl;
                success = false;
            }
            else
            {
                newOdd->integer = number;
                newOdd->prev = NULL;
                oddHead = newOdd;
                newOdd->next = NULL;
                success = true;
            }
        }
        else
        {
            newOdd = new (nothrow) node;
            if(newOdd == NULL)
            {
                cout << "ERROR. Memory allocation failed!" << endl;
                success = false;
            }
            else
            {
                newOdd->integer = number;
                newOdd->prev = newOdd;
                success = true;
            }
        }
    }
    return success;

}
int ReadFirst(list* &Odds, list* &Evens)
{
    string file1 = "Int1.txt";
    ifstream ReadInts;
    int number;
    int x = 0,
        y = 0;
    bool success;

    ReadInts.open(file1.c_str());
    ReadInts >> number;
    do
    {
        if((number % 2) == 0)
        {
            success = OrderedListInsertion(Evens, number, x);
            if(success)
            {
                x++;
                cout << "Even processed." << endl;
            }
            else
                return 1;
        }
        if((number % 2) != 0)
        {
            success = OrderedListInsertion(Odds, number, y);
            if(success)
            {
                y++;
                cout << "Odd processed." << endl;
            }
            else
                return 1;
        }
        ReadInts >> number;
    }while(ReadInts);

    ReadInts.close();

}
int main()
{
    list* Odds;
    list* Evens;

    Odds = InitializeList(Odds);
    Evens = InitializeList(Evens);
    ReadFirst(Odds, Evens);

    return 0;
}   

您的 OddsEvens 变量只是指针。但是它们指向什么?

您需要分配一些内存或执行其他操作,以便它们指向有效内存。实际上,您将它们传递给 InitializeList(),它设置甚至不存在的属性。

由于您甚至没有初始化这些指针,因此无法得知它们具有什么值,这意味着无法得知它们指向的内存。

像下面这样的东西(未经测试)会更有意义。

list Odds;
list Evens;

InitializeList(&Odds);
InitializeList(&Evens);
ReadFirst(&Odds, &Evens);

在这里,列表不是指针。它们是完整的对象。然后你只是将指向这些对象的指针传递给其他函数。

您的 InitializeList() 不会初始化 A_list,因此如果 A_list 为空,则您不能将其引用为 A_list->top

list* InitializeList(list* A_list)
{
    A_list->top = NULL;
    A_list->length = 0;
    return A_list;
}

看你怎么称呼它:

list* Odds

Odds = InitializeList(Odds);

Odds 什么也没有指向,所以你在 InitializeList()

中得到一个错误

要初始化list,你应该

list* Odds = new list