初始化两个双向链表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

相关内容

  • 没有找到相关文章

最新更新