struct pointers pointers

Code!
#include <iostream>

using namespace std;


//Programma di test

struct Nodo {
int valore;
Nodo *nextPtr;
} ;


void inserimento(Nodo * &testa, int numero)

{
Nodo *temp = new Nodo;

temp->valore = numero;

temp->nextPtr =0;

if (testa==0) testa=temp;
else
{
Nodo *curr=testa;
while (curr->nextPtr!=0) curr=curr->nextPtr;
curr->nextPtr = temp;
}

}



void stampa(Nodo *testa)

{ cout << endl << endl;

for(Nodo *ptr = testa; ptr != 0; ptr = ptr->nextPtr)

cout << " " << ptr->valore << endl;

}

*/

// VERSIONE MIGLIORE

void invertiLista(Nodo *&testa, Nodo * &testaInv)

{ Nodo* prec,* cur, * temp;

if (testa==0) testaInv=testa;

else

{ prec=0;

cur=testa;

while(cur!=0)

{ temp=cur->nextPtr;

cur->nextPtr=prec;

prec=cur;

cur=temp;

}

testaInv=prec;

}

testa=0;

}

int main()

{ int numero;

Nodo *lista1 = 0;

Nodo *lista2 = 0;

Nodo *lista3 = 0;

int n;

cout << endl << "Quanti numeri si vogliono inserire in lista1? ";

cin >> n;

for(int i = 0; i < n; i++)

{ cout << "lista1[" << i << "] = ";

cin >> numero;

inserimento(lista1, numero);

}

stampa(lista1);

return 0;

}


Ok so i just finised learning pointers, dynamic memory and struct. And this code should be a code to place numbers in a list, print them out in ascending order and descending order.
am trying to read the code to make sense out of it(nice code, working fine) but am not being able to.
in function inserimento...from if (testa==0) testa = temp??? ok what does that mean? testa should always be equal to zero no? because in main lista1 was declared a null pointer..how come we expect testa to be other than zero or null, if we later pass the value of lista by reference? surely am getting something wrong..help...
I want a good samaritan to explain to me from function inserimento..line by line what happens...til the end of the code..been sitting on this thing for two to three hours now and am getting really bored...someone help me pls..exams is in a months time and i got classes and polymorphism to deal with :D
May be you should start with this article:
http://en.wikipedia.org/wiki/Linked_list

I may not be 'samaritan' but I will try to explain the code above. We can start with 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
int main()

{ 
	int numero;		//Declaring numero as an integer

	Nodo *lista1 = 0;	
	//lista1 is a pointer, which points to a type Nodo. Currently it is assigned to 0. So it is not pointing to anything.
	Nodo *lista2 = 0;	
	//Same for lista2
	Nodo *lista3 = 0;	
	//Same for lista3

	int n;		//Declaring n as an integer

	cout << endl << "Quanti numeri si vogliono inserire in lista1? ";	
	//Asking for the number of numbers we want to input.

	cin >> n;		
	//Reading the input in n. So now, n will contain the number of numbers we want to insert in the list. 
//Let us say we give 5, it will loop 5 times

	for(int i = 0; i < n; i++)		
	//Looping n times (eg: 5 times), to read each number and then insert it into the list.

	{ 
		cout << "lista1[" << i << "] = ";	
		//Asking for a number to be inserted into the list.

		cin >> numero;		
		//Reading the number temporarily into numero. Temporary because, in the next iteration of the loop, 
		//numero will be overwritten with the new number we enter

		inserimento(lista1, numero);	
		// Calling insert function. This will insert the number numero into the list lista1. 

	}

	//After the loop is completely executed, lista1 will be pointing to a Nodo (which contains value as the first numero we entered). 
	//The nextPtr pointer will be pointing to another Nodo. 
	//The list is considered to contain elements till one of the Nodo's nextPtr is pointing to 0.

	stampa(lista1);		//Display the list. i.e, it will go on from one Nodo to the next Nodo 
						//and keep printing the values, till when the Nodo's next is 0.

	return 0;

}


For your specific question:
in function inserimento...from if (testa==0) testa = temp??? ok what does that mean? testa should always be equal to zero no? because in main lista1 was declared a null pointer..how come we expect testa to be other than zero or null, if we later pass the value of lista by reference?


Are you confusing passing by value and passing by reference.
Since you are passing by reference, the changes made inside inserimento function will be reflected in main() as well. Hence the first time we call inserimento, testa will be 0. The next iteration, testa will have 1 Nodo. Hence it will not be 0.

Hope this clarifies.
Last edited on
I still dont get where and how come lista1 which was pointing to nothing started pointing to a nodo...where does the pointing direction change happens? is the first time am seeing a struct having a pointer as a member in our case Nodo * nextPtr(am just a beginner so bare); may be thats where my confusion is...pls explain again in other words trying to explain the struct Nodo and it member pointer nextPtr and how they work again..but thanks for the earlier explanation...i have some light now...but more will not be bad...thanks again..
am checking on your wikipedia link though ..thanks :)
Last edited on
The first time inserimento is called, i.e., when there are no nodes in the list, the following happens:
(I'm gonna use Node instead of Nodo in the function to avoid confusion in explanation)

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
void inserimento(Node * &testa, int numero)
{ 
	Node *temp = new Node;	
	//A variable called temp of type Node is created, and allocated some memory (because of new). 
	//The memory allocated is for an integer vaiable valore and a pointer which can point to Node type.

	temp->valore = numero;	//Now we are saying, for this temp Node, the value is numero. 

	temp->nextPtr =0;	//And the pointer within the temp Node, is pointing to 0. i.e., it is not pointing to any other Node. 

	if (testa==0)		//In first iteration, lista1 is 0, that means testa1 is 0. Hence the condition will be true. 
	{
		testa=temp;		//In first iteration, this line will be executed and the else part will be ignored.
	//This means that testa is a reference variable now pointing to temp.
	//Meaning, testa will now be pointing to the memory that was allocated for temp, i.e., temp.valore and temp.nextPtr; 
	//You can consider reference variables as alias names; or same as pointers, except their usage is similar to a normal variable.
	//the value of lista is passed to testa as reference. Hence whatever changes are made to testa will reflect to lista in main()
	//So testa now has 1 node. Therefore lista has 1 node.
	}
	else  
	{
		Nodo *curr=testa;
		while (curr->nextPtr!=0) curr=curr->nextPtr;
		curr->nextPtr = temp;
	}

}
You can verify this by printing the address of temp, testa and lista1 as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void inserimento(Nodo * &testa, int numero)

{ 
	Nodo *temp = new Nodo;
	cout<<"Address of temp is "<<temp<<endl;

	temp->valore = numero;

	temp->nextPtr =0;

	if (testa==0) 
	{
		testa=temp;
		cout<<"Address of testa is "<<testa<<endl;
	}
	else
	{
		Nodo *curr=testa;
		while (curr->nextPtr!=0) curr=curr->nextPtr;
		curr->nextPtr = temp;
	}

}


And in main after inserting each element
1
2
3
4
		inserimento(lista1, numero);	
		
		cout<<"Address of lista1 is "<<lista1<<endl;
Thank you soo much kameswarib..you're the good samaritan i was after...thanks
sorry kameswarib...can you pls...am ashamed to ask but i have to...can you pls explain line by line form..else Nodo*curr=testa? those three lines...??? thanks and one more thing...in the function void stampa... taking a look at the for loop....i thought for loops should have their third elements as ++ or --??? but over here is like we got ptr=ptr->nextPtr??? a lil bit confused...can you pls enlighten me? thanks
Last edited on
Topic archived. No new replies allowed.