Aguen
Aguen

Reputation: 17

Linking a LinkedList as an element to a different LinkedList

I'm working on a C++ assignment where I'll create a search engine on a linked list of linked lists. As per the requirements, I can't use other libraries nor STL.

Basically it will be like this (I removed the variables from small list since they are irrelevant): enter image description here

My structs are these:

struct small
{
    int data;
    struct small *next;
};


struct big
{
    int playerID;
    string playerName;
    string playerTeam;
    struct small *goals;
    struct big *next;
};

Here's the relevant code snippet, I think the problem is at addGoals(...) where I'm failing to assign the small element to the temp->goals.

class biglist
{
private:
    big *head, *tail;

public:
    biglist()
    {
        head = NULL;
        tail = NULL;
    }

. . .

void createbig(int ID, string name, string team)
    {
        big *temp = new big;
        temp->playerID = ID;
        temp->playerName = name;
        temp->playerTeam = team;
        temp->goals = NULL;
        temp->next = NULL;

        if (head == NULL)
        {
            head = temp;
            tail = temp;
            temp = NULL;
        }
        else
        {
            tail->next = temp;
            tail = temp;
        }
    }

void addGoals(int id, small *s)
    {
        big *temp = head;
        while (temp != NULL)
        {
            if (temp->playerID == id)
            {
                temp->goals = s;
                break;
            }
            temp = temp->next;
        }
    }

    void test()
{
    big *temp = head;
    while (temp != NULL)
    {
        if (temp->playerID == 1)
        {
            if (temp->goals !=NULL)
            {
                cout << temp->goals->data << endl;
            }
            else
            {
                cout << "goals null" << endl;
            }
        }
        temp = temp->next;
    }
}
}
. . .

 

class smalllist
{
private:
    small *head, *tail;

public:
    smalllist()
    {
        head = NULL;
        tail = NULL;
    }

    void createsmall(int ID, biglist b)
    {
        small *temp = new small;
        temp->data = ID;
        temp->next = NULL;

        if (head == NULL)
        {
            head = temp;
            tail = temp;
            temp = NULL;
        }
        else
        {
            tail->next = temp;
            tail = temp;
        }


        b.addGoals(1, temp);
    }
};

Finally, my main code:

int main()
{
    biglist obj;
    obj.createbig(1, "Player1", "Team1");
    obj.createbig(2, "Player2", "Team2");
    obj.displaybig();

    smalllist sml;
    sml.createsmall(9, obj);
    sml.displaysmall();

    obj.displaybig();
    obj.test();
}

Debugging throws an exception at:

cout << temp->goals->data << endl;

saying that

Exception thrown: read access violation. temp->goals was nullptr.

I'm 90% sure I messed up something with pointers; but other stuff I've tried gave errors before compiling. I checked out some books / tutorials but couldn't figure it out.

Also if you have a better approach or saw one of the horrible mistakes that I'm making, please don't hold back :)

Thanks.


EDIT I changed my createbig() like this. Currently it works with following codes:

void createbig(int ID, string name, string team, small *s)
    {
        big *temp = new big;
        temp->playerID = ID;
        temp->playerName = name;
        temp->playerTeam = team;
        temp->goals = s;
        temp->next = NULL;

        if (head == NULL)
        {
            head = temp;
            tail = temp;
            temp = NULL;
        }
        else
        {
            tail->next = temp;
            tail = temp;
        }
    }

and added this to small

small getsmall(int i)
    {
        small *temp = head;
        while (temp != NULL)
        {
            if (temp->data == i)
            {
                return *temp;
            }
        }
    }

My final main function is

int main()
{
    smalllist sml;
    sml.createsmall(9);
    sml.displaysmall();

    biglist obj;
    small s = sml.getsmall(9);
    obj.createbig(1, "Player1", "Team1", &s);
    //obj.createbig(2, "Player2", "Team2");
    obj.displaybig();

    obj.test();

}

While it ends successfully now, it gives the address of goals and I get this in debug section:

enter image description here

Upvotes: 0

Views: 82

Answers (2)

JaMiT
JaMiT

Reputation: 16873

Let's look at what your code does, going through the main function. (Being able to walk through code like this is a useful skill. You can also use a debugger to help out, stepping through your function line-by-line.)

biglist obj;

Default construct a biglist. The head and tail are null. (By the way, nullptr is C++'s replacement for C's NULL.)

obj.createbig(1, "Player1", "Team1");
obj.createbig(2, "Player2", "Team2");

Add entries in obj for players with IDs 1 and 2. Their goals are null.

obj.displaybig();

Presumably an output of obj?

smalllist sml;
sml.createsmall(9);
sml.displaysmall();

These lines do something with a smalllist, but do not reference obj, so they are not relevant to this issue.

obj.displaybig();

Presumably an output of obj? Kind of redundant since nothing affected obj since the last display.

obj.test();

Call the test code, which finds the element for player ID 1 and outputs the data of that player's first goal. However, if you look up where that player was added, the goal is null, so you get a crash.


Separate from the above, there is probably some confusion in createsmall. Inside that function, a new biglist is created (not obj), and that list is told to add a goal to the player with ID 1. However, this has no effect the biglist in the main function.

Upvotes: 1

Deepan
Deepan

Reputation: 134

You don't seem to have added any goals, so I'm assuming the code initializes with null. and so the nullptr exception.

call addgoals() with the goals to player before test().

the other suggestions would be

  1. to add a null check before printing goals
  2. temp pointers need not be initialized with new big or small just the head of the list would be enough

Upvotes: 1

Related Questions