Linked Lists question


Hello everyone,

Could you please help with the code below?:

- First, I Wonder if I badly managed the pointers below. I tried to use the destractors as below, but it doesn't seem to prevent errors either.

- Second, I can't understand in the Print() :
 
	if(temp->returnNext()==NULL){cout << temp->returnData() << "-->" << "NULL" << endl;}


how this refers to the first element in the list. For me, "head" is already a pointer to the first node. So if "head" is NULL, the list is empty and temp->returnNext() returns the address of the first element and not the address of the second. So how does one conclude that if temp->returnNext()==NULL there's only one element?

Thank you for your help!!

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

#include <iostream>

using namespace std;

class Node {
	int _data;
	Node* _next;
public:
	Node(){};
//	~Node(){delete _next;};
	void setData(int data){_data=data;};
	void setNext(Node* next){_next=next;};
	int returnData(){return _data;};
	Node* returnNext(){return _next;};
};

class List {
	Node* head;
public:
	List(){head=NULL;};
//	~List(){delete head;};
	void Print();
	//void Append(int data);
	//void Delete(int data);
};

void List::Print(){
	Node* temp=head;
	if(temp==NULL){	cout << "The list is Empty" << endl;}
	if(temp->returnNext()==NULL){cout << temp->returnData() << "-->" << "NULL" << endl;}
	else{
		do{	cout << temp->returnData();
			cout << "-->" ;
			cout << temp->returnNext();
		}
		while(temp!=NULL);
		cout << "NULL" << endl;
	}
};

int main()
{
	List maListe;
	maListe.Print();

	system("PAUSE");
	return 0;
}
Hi JasBeckC,
First at all I am not a good programmer. I am shure others could give better explanations and perhaps better answers, but I`ll try my best.
I believe you have never worked with linked lists, am i right?
So how does one conclude that if temp->returnNext()==NULL there's only one element?“

A linked list is a bit like a scavenger hunt. You need a starting point. Each station needs a hint for the next treasure. But you have to know when the search is over. In a Linked list the end is marked with the nullpointer for the next element. That means you know the end is reached when _next points to NULL. When your fist element has the Nullpointer then it is the last element, too.
- Second, I can't understand in the Print() :
if(temp->returnNext()==NULL){cout << temp->returnData() << "-->" << "NULL" << endl;}

lets split it up a bit:
1
2
3
4
5
if(temp->returnNext()==NULL) //this condition gets true by reaching the end of the list.
{
cout << temp->returnData() //this returns the data of the last element in your list
	<<“-->“<<“NULL“<<endl;  //prints „-->NULL“ on screen and begins a new line.
}


in the code I could see 2 Mistakes
First:
if you want to use the function system(); you need to include <stdlib.h>

second:
1
2
3
4
5
6
7
8
	if(temp==NULL){	cout << "The list is Empty" << endl;}
	else if(temp->returnNext()==NULL){cout << temp->returnData() << "-->" << "NULL" << endl;}
	else{
		do{	cout << temp->returnData();
			cout << "-->" ;
			cout << temp->returnNext();
		}
		while(temp!=NULL);

without else in else if(temp->returnNext()==NULL){cout << temp->returnData() << "-->" << "NULL" << endl;}

1
2
3
4
5
else{
		do{	cout << temp->returnData();
			cout << "-->" ;
			cout << temp->returnNext();
		}

will be worked up when if(temp==NULL){ cout << "The list is Empty" << endl;} is true, but when temp is a nullpointer there is no data to return and a segmentation fault will arise.

I hope i could answer all your questions.
Asperadus
Hello Asperadus,
Thank you for your reply. That's true, I have never worked with Linked Lists. I guess I got the concept except one thing:
When I set temp=head, for me it's like temp will point to NULL if the link list is empty and point to something different from NULL in case there's one element.
But in the code says that if(temp->returnNext()==NULL) there's one element. This is precisely what I don't understand.
Thank you so much again for the help
Hi JasBeckC
head is your starting point, your anchor. Head shows you the address of the first data point in list. The Anchor itself holds no Data. So you are right you have no data stored in head.
Lets get back to the scavenger hunt picture: Before you can rewalk the list you have to know where your starting point is. Your (in this case) chosen starting point contains no treasure but only the waypoint to the first treasure.
else if(temp->returnNext()==NULL){cout << temp->returnData() << "-->" << "NULL" << endl;}
shows you the last element of the list if it is not the last one
1
2
3
4
5
6
          else{
		do{	cout << temp->returnData();  //gives you data from this point
			cout << "-->" ;
			cout << temp->returnNext();  //gives you the next address
		}
		while(temp!=NULL);


Perhaps you should read a tutorial to linked lists like this one. I just scanned it but it seems to explain all crucial points to linked lists in C. This could help you to show you what you need to know.
http://www.cprogramming.com/tutorial/c/lesson15.html
Topic archived. No new replies allowed.