A doubly linked list is the exact same thing as a singly with just one more
added pointer, a previous poitner that points to the node before the current
one. Doubly linked lists are really good for constant operations (excluding
printing the whole list or deleteing the whole list). As before, you have
your list and node class with the one new pointer added in:
class list
public
member functions (i.e. constructor, destructor, append, etc.)
private
*firstpointer (points to the beginning of the list)
*lastpointer (optional: points to the end of the list)
class node
make this class a friend of class list
private
data feild
*nextpointer (points to the next node)
*previouspointer (points to the last node)
All operations are baisically the same for the doubly linked list as they
are for the singly list, but all you have to do is keep track of where the
previous poitner points too.
For example, to insert a new node in an empty list, you carry out the same
steps as you would in a singly linked list and just set the previous pointer
to NULL like so:
a) Create the list
first
\
\
->NULL
b) Create the new node (notice the new previous pointer)
+------+--+
temp----> | | | (next)
<---| node | |-->
(prev) +------+--+
c) Set first == to the new node and set the previous pointer = NULL
first
\
\ +------+--+
-->| node | |-->NULL
NULL<---| | |
/ +------+--+
/
/
temp
You have to set the previous poitner to null for the first node in case you
want to iterate backwards from the end, then you know where the beginning
of the list is. Also, you could use the previous pointer to point to
last->next to have a circular list.
first
\
\ +------+--+ (next)
-->| node | |--
(prev) ---| | | \
/ +------+--+ \
| |
| |
-------------------
Inserting to an existing list is also very easy. For example, to
insert at the end of a list, you just append the node the same way you
did for a singly linked list with the exception that you have to set
the previous pointer to point to last before you move the last pointer
to point to the new node you created.
a) The end of the list
last
/
/
+------+--+
<--| node | |-->NULL
+------+--+
b) Create the new node
+--next pointer
|
+------+--+ |
<--| node | |-->NULL
+------+--+
/
temp
c) insert the new node at the end of the list
last
/
/
+------+--+ (next)
more <--| node | |------------
nodes +------+--+ \ \ +------+--+
+----------- | node | |-->NULL
(prev) +------+--+
d) move last to point to the new node
last
+------+--+ (next) /
more <--| node | |------------ /
nodes +------+--+ \ \ +------+--+
+----------- | node | |-->NULL
(prev) +------+--+
Deleting from a list is also very easy, and in the case of the list above,
trivial. First, you create a new pointer and point it to the end of the list.
Then, in the case of the double link list, you move last to last->previous,
and just delete the temp pointer you created. You also have to be sure to
set the next pointer of the last node to null too.
a) create the temp pointer to point to the node to delete
last
+------+--+ (next) /
more <--| node | |------------ /
nodes +------+--+ \ \ +------+--+
+----------- | node | |-->NULL
(prev) +------+--+
/
/
temp
b) Move the last pointer to last->previous
last
/
/
+------+--+ (next)
more <--| node | |------------
nodes +------+--+ \ \ +------+--+
+----------- | node | |-->NULL
(prev) +------+--+
/
/
temp
c) Finaly delete temp and set next pointer of the last node to NULL
last
/
/
+------+--+
more <--| node | |-->NULL
nodes +------+--+
Lists aren't very hard at all, trust me, I always had problems with
them, and now I'm writing about them. I overcame them by just writing
code down and drawing a picture. Pictures always help you visualize
what you are doing, so it's good practice to draw them. I hope this
little page helped in some way.
Written by: John Mikhail