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

时间:2015-01-16 01:51:03

标签: c++ struct null linked-list doubly-linked-list

我正在尝试将我的两个双向链接列表初始化为空,但我的程序崩溃了。程序将偶数和奇数整数与文本文件分开。我相信问题在于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;
}   

2 个答案:

答案 0 :(得分:0)

您的OddsEvens变量只是指针。但他们指出了什么?

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

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

以下(未经测试)之类的内容会更有意义。

list Odds;
list Evens;

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

这里,列表不是指针。它们是完整的物体。然后你只是将指针传递给其他函数。

答案 1 :(得分:0)

您的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