Circular reference not working?

It's compiling but it's not working, it enters in stack overflow. It's a Doubly Linked List I'm compiling in Visual Studio. I think there's nothing wrong with this declaration, but there's just might be it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class ListItem;	

class List
    {
    public:
        ListItem *firstItemRef;
        List();
        void PrintList();
		void Add(int cpfParam);
    };

class ListItem
    {
    public:
		List *list;
        ListItem *previous;
        ListItem *next;
		int cpf;
		ListItem(List *listParam, int cpfParam);
		void PrintCpf();
		void AddNew(ListItem *listItem);
    };


Thank you very much.
- I highly dislike your variable naming conventions, but that's just me.

- Moreover, i'm having trouble figuring why you have a pointer to a "List" object, in your "ListItem" object.
lol, sorry to bother you by the way I name them, and thank you for your reply. The List object happens to be in the ListItem because I need the list reference in case I need to replace the firstItemRef when adding a new list item in the list that makes a comparison with listItem's cpf in case it's lesser than firstItemRef it should replace his position.

Here's the entire code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef stdio
	#define stdio
	#include "stdio.h"
#endif

class ListItem;	

class List
    {
    public:
        ListItem *firstItemRef;
        List();
        void PrintList();
		void Add(int cpfParam);
    };

class ListItem
    {
    public:
		List *list;
        ListItem *previous;
        ListItem *next;
		int cpf;
		ListItem(List *listParam, int cpfParam);
		void PrintCpf();
		void AddNew(ListItem *listItem);
    };

List::List()
    {
    firstItemRef = NULL;
    }
    
void List::PrintList()
    {    
    if (firstItemRef != NULL)
		firstItemRef->PrintCpf();
    else
        printf("\nThe list is empty.");
    }

void List::Add(int cpfParam)
    {
	ListItem *listItemParam = new ListItem(this, cpfParam);

    if (firstItemRef != NULL)
		firstItemRef->AddNew(listItemParam);
    else
        firstItemRef = listItemParam;
    }

ListItem::ListItem(List *listParam, int cpfParam)
	{
	next = NULL;
	previous = NULL;
	list = listParam;
	cpf = cpfParam;
	}

void ListItem::PrintCpf()
	{
	printf("\n%i", this->cpf);
	if (this->next != NULL)
		this->next->PrintCpf();
	}

void ListItem::AddNew(ListItem *listItem)
	{
	if (listItem->cpf > this->cpf)
		{
		if (this->next != NULL)
			this->next->AddNew(listItem);
		else
			{
			this->next = listItem;
			listItem->previous = this;
			}
		}
	else
		{
		if (this->previous != NULL)
			{
			listItem->previous = this->previous;
			listItem->next = this;
			this->previous->next = listItem;
			this->previous = listItem;
			}
		else
			{
			listItem->next = this;
			this->previous = listItem;
			this->list->firstItemRef = listItem;
			}
		}
	}
I couldn't run the code as I said, but it's still compiling.

Here is the main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "stdafx.h"

#ifndef stdio
	#define stdio
	#include "stdio.h"
#endif

#include <iostream>
#include <cstdlib>

#include "DoublyLinkedList.h"

using namespace std;

#define ARRAY_SIZE 1000000

unsigned int arrayList [ARRAY_SIZE];

int greatestValue = 0;

//generate a list with random numbers
void GenerateList(int *list, int size)
	{
	int cpfnum = 923456781;

	for (int i = 0; i < size; i++)
		{
		cpfnum = (cpfnum + rand() %100000) % 999999999;
		
		if(cpfnum < 100000000)
			cpfnum += 100000000;

		list[i] = cpfnum;
		
		if (greatestValue < list[i])
			greatestValue = list[i];
		}
	}

int PegaIndice(int cpf)
	{
	return cpf / (greatestValue / ARRAY_SIZE);
	}

int main()
    {
	int * CPFlist;
	List *list[ARRAY_SIZE];
	
	printf("Programa iniciado.\n");
		
	CPFlist = new int[ARRAY_SIZE];

	GenerateList(CPFlist, ARRAY_SIZE);

	for (int i = 0; i < ARRAY_SIZE; i++)
		{
		if (list[PegaIndice(CPFlist[i])] == NULL)
			list[PegaIndice(CPFlist[i])] = new List();
		list[PegaIndice(CPFlist[i])]->Add(CPFlist[i]);
		}

    return 0;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main()
{
    int * CPFlist;
    List *list[ARRAY_SIZE] = {0} ;

    printf("Programa iniciado.\n");

    CPFlist = new int[ARRAY_SIZE];

    GenerateList(CPFlist, ARRAY_SIZE);

    for (int i = 0; i < ARRAY_SIZE; i++)
    {
        int index = PegaIndice(CPFlist[i]) ;

        if ( index >= ARRAY_SIZE )
        {
            printf( "Illegal index encountered.  Terminating program.\n") ;
            return 0 ;
        }

        if (list[index] == NULL)
            list[index] = new List();
        list[index]->Add(CPFlist[i]);
    }

    return 0;
}
I've just found out that the problem wasn't actually the circular reference declaration, but rather dynamically variable declaration. All I had to do is move the list variable to global scope.

Thanks.
- I personally write my linked lists differently.

- You have an "addmember" function as a part of your node structure instead of your list.

- Personally, i would make my list object have all of the methods, and merely use them to add, remove,etc the node objects.

- However, it seems that your method would also work.

Last edited on
I personally write my linked lists differently.
std::list


If NIH, then circular and with a header cell
Topic archived. No new replies allowed.