Help Doubly linked list class

I am reading "Programming Principles and Practice Using C++" by Bjarne Stroustrup. I am currently on chapter 17.9.3 (just informing those of you who by any chance have read the book or have the book). I'm having trouble understanding the following class:

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
#include "std_lib_facilities.h"

struct Link
{
	string value;
	Link* prior_to;
	Link* succ_of; 
	Link(const string& v, Link* p = 0, Link* s = 0)
	: value(v), prior_to(p), succ_of(s) { }
};

Link* insert(Link* old_p, Link* new_p)
{
	new_p->succ_of = old_p; // new_p PlayStation 3 is a succ_of PlayStation 2
	if(old_p->prior_to) old_p->prior_to->succ_of = new_p; // If what?
	new_p->prior_to = old_p->prior_to; // What does this do?
	// I put some output statements to help me see what is going on,
	// but new_p->prior_to and old_p->prior_to are always 0
	if(new_p->prior_to != 0 && old_p->prior_to != 0)
	{
		cout << "old_p->prior_to->value = " << old_p->prior_to->value << "\n";
		cout << "new_p->prior_to->value = " << new_p->prior_to->value << "\n";
	}

	old_p->prior_to = new_p; // old_p PlayStation 2 is prior_to new_p PlayStation 3
	return new_p;
}

int main()
{
	Link* list = new Link("PlayStation 1", 0);
	list = insert(list, new Link("PlayStation 2"));
	list = insert(list, new Link("PlayStation 3"));
	while(list)
	{
		cout << list->value << "\n";
		list = list->succ_of;
		// list->value == PlayStation 3
		// PlayStation 3 is a succ_of PlayStation 2, and so on...
	}
	keep_window_open();
	return 0;
}


I put the comments so you guys can see how I understand the code.
Thanks in advance to anyone who replies!
Last edited on
Line 15 is checking that old_p->prior_to is not null

If you replace the words succ_of with 'next' and replace prior_to with 'prev', it might help you understand the code.

So insert(PlayStation 1)
prev = null
next = null

insert(PlayStation 2)
PlayStation 2->next = PlayStation 1 //PlayStation 1->next = null
PlayStation 1->prev is null so line 15 does not execute
PlayStation 1->prev = PlayStation 2 //PlayStation 2 ->next->prev = Playstation 2
return PlayStation 2 //PlayStation 2->prev = null Since it is the top node now

insert(PlayStation 3)
PlayStation 3->next = PlayStation 2//PlayStation 2->next = PlayStation 1
PlayStation 2->prev is null so line 15 does not execute
PlayStation 2->prev = PlayStation 3 //PlayStation 3->next->prev = PlayStation 3
return PlayStation 3 //PlayStation 3->prev = null Since it is the top node now

So as you can see the insert function inserts any new element at the top of the list, so as you call the next list, you will finally get to the first element that was inserted.

You can also get rid of line 15 seeing as it will never evaluate to true

EDIT:
Also your if statement will not print what it is supposed to print because both values are never true at the same time because you cannot know the previous link only the next link. So new_p->prior_to will only be true at the end of the next insert operation

Implementation in Java that inserts at the end of the list. Some attributes are inherited:

LinkedList:
http://ideone.com/NgGo4Q
DoublyLinkedList:
http://ideone.com/e8r8De
Last edited on
Thank you very much! Now I understand everything!
Topic archived. No new replies allowed.