Linked list traversal issues

Hello guys thanks for taking a second to look at this.

I'm having a problem traversing a linked list:
This is the function:
1
2
3
4
5
6
7
8
9
10
  void ContactList::displayList(){
	Node* traveler_ptr;
	traveler_ptr = head_ptr;
	
	while(traveler_ptr != 0){
		traveler_ptr->display();
		traveler_ptr = traveler_ptr->Next();	
	}
		
}

This is the output
**********Contact Entry**********
Name:Luis
Number:(718)684-5472
*********************************

**********Contact Entry**********
Name:Rick
Number:(718)486-6837
*********************************

**********Contact Entry**********
Name:Paul
Number:(914)453-9374
*********************************

**********Contact Entry**********
Name:Cory
Number:(914)781-4581
*********************************

**********Contact Entry**********
Name:Nate
Number:(914)261-9193
*********************************
it outputs correctly, but then it crashes the program after. I'm sure it's something simple but I can't see it, can anyone tell me why it crashes after?


This is another function
1
2
3
4
5
6
7
8
9
10
11
12
13
Node* ContactList::get_ByName(string nameIn){
	Node* traveler_ptr;
	traveler_ptr = head_ptr;
	
	while(traveler_ptr != 0){
		
		if(traveler_ptr->Value().getName() == nameIn)
			{return traveler_ptr;}
			traveler_ptr = traveler_ptr->Next();
			
	}
	cout <<"A contact with that name was not found" << endl;
}

It DOES acquire the right node if that node exists, but if that node does not exist/ is not found, then it crashes the program i'm using (dev c++)

any assistance is appreciated.

Just wanted to add this 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
int main(int argc, char** argv) {
	
	
	Contact nate("Nate",  "(914)261-9193");
	Contact cory("Cory",  "(914)781-4581");
	Contact paul("Paul",  "(914)453-9374");
	Contact rick("Rick",  "(718)486-6837");
	Contact luis("Luis",  "(718)684-5472");
	
	
	ContactList myContactList;
	
	myContactList.addToHead(nate);
	myContactList.addToHead(cory);
	myContactList.addToHead(paul);
	myContactList.addToHead(rick);
	myContactList.addToHead(luis);
	
	myContactList.displayList();
	
	Node* testNode = myContactList.get_ByName("Luis");
	
	
	
	return 0;
}

Contact is a simple class with just 2 strings.
Last edited on
Are you sure traveler_ptr->Next() is returning 0 for the final node?

I'd recommend stepping through your code with a debugger, to find out where it's crashing, and what the state of the memory is at that point.
Topic archived. No new replies allowed.