Can you initialize an object within an object taken as a parameter?

I have a header which allows the class to take another class of the same type as a parameter. Calling the functions of the parameratized object works just fine but I can't seem to create Node structs even within the object. I want to compare two lists by iterating through both of them and without the ability to reference objects created within the parametrized object, I have no idea how I would do the comparison. How can I create a Node struct within the parameratized object?

classLinkList.h
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifndef CLASSLINKLIST_H
#define CLASSLINKLIST_H

#include <iostream>

class List
{
	private:

	struct Node
	{
		int data;
		Node *next;
		Node *prev;
	}; 

	Node *head;
	Node *tail;
	int size;

	public:

	List()
	{
		init();
	}

	~List()
	{
		Node *currNode = head;
		Node *nextNode = NULL;

		while( currNode != NULL )
		{
			nextNode = currNode->next;
			delete currNode;
			currNode = nextNode;
		}
	}

	void init()
	{
		head = new Node;
		tail = new Node;
	
		head->next = tail;
		head->prev = nullptr;

		tail->next = nullptr;
		tail->prev = head;

		size = 2;
	}

	void InsertNode( int index, int x )
	{
		int currIndex = 1;
		Node *currNode = head;

		while( currNode && index > currIndex )
		{
			currNode = currNode->next;
			currIndex++;
		}

		Node *newNode = new Node;
		newNode->data = x;

		if( index == 0 )
		{
			newNode->next = head;
			head = newNode;
		}
		else
		{
			newNode->next = currNode-> next;
			currNode->next = newNode;
		}

		++size;

	}

	int FindNode( int x )
	{
		Node *currNode = head;
		int currIndex = 0;
		while( currNode && currNode->data != x )
		{
			currNode = currNode->next;
			currIndex++;
		}
		if( currNode )
		{
			return currIndex;
		}
		return 0;
	}

	int DeleteNode( int x )
	{
		Node *prevNode = NULL;
		Node *currNode = head;
		int currIndex = 1;

		while( currNode && currNode->data != x )
		{
			prevNode = currNode;
			currNode = currNode->next;
			currIndex++;

		}

		if( currNode )
		{
			if( prevNode )
			{
				prevNode->next = currNode->next;
				delete currNode;
			}
			else
			{
				head = currNode->next;
				delete currNode;
			}
			return currIndex;

			--size;
		}

		return 0;
	}

	void DisplayList()
	{
		int num = 0;
		Node *currNode = head;
		while( currNode != NULL )
		{
			std::cout << currNode->data << ",";
			currNode = currNode->next;
			num++;
		}
		std::cout << std::endl << "Number of nodes in the list: " << num << std::endl;
	}

	int findIntersections( List &iteratorList )
	{
	    //does something; haven't written this yet
	}	

};

#endif 





Last edited on
I have no idea how I would do the comparison.
How can I create a Node struct within the parameratized object?
This kind of contradicts. Either you want to compare or modify.

For findIntersections(...) you should create a third List which is filled with the intersections (simply using InsertNode(...). if iteratorList is supposed to be the result as well you can assign the third list to it at the end.
How can I compare them if I can't create nodes based on the referenced &iteratorList? I also don't know how a third object would help. I should have posted my intentions here, I realize.

I want to iterate through iteratorList and see if there is a match in the object we are in, compareList. If there is, print it. I don't want you to write the code for me but I don't know how to iterate through iteratorLiat without creating a Node stuct within it.
1
2
3
4
5
6
7
8
9
10
def print_common(aList, bList):
   for b in bList:
      if is_member(b, aList): //if there is a match
         print(b)

def is_member(node, list):
   for x in list:
      if x.data == node.data
         return true
   return false
¿is that what you want to do?

> Calling the functions of the parameratized object works just fine
> but I can't seem to create Node structs even within the object.
I don't understand the second part.
int findIntersections( List &iteratorList )
you have two lists there `iteratorList' (terrible name) and `*this',
you may traverse them starting from `iteratorList.head' and `this->head'
¿why do you need to create another Node?
Last edited on
Thank you all. I don't know why I didn't think I could access the nodes in the parameratized object.
Topic archived. No new replies allowed.