Title: Variations on Singly Linked Lists
1- Variations on Singly Linked Lists
- Inserting or deleting at the front of a list is
different from at any other point in the list. - No previous node.
- Can we get rid of this special case?
2Variations on Singly Linked Lists Head Nodes /
Dummy Nodes A dummy node is not used to store
data, it is created when the list is created. An
empty list with a dummy node looks like
head
3Insertions go after the dummy node
head
item
This code always works, assuming that
previousNode is a pointer to the node after which
the new node is to be inserted Node newNode
makeNode( item, previousNode-gtnext
)previousNode-gtnext newNode
4Deletions always have a previous node
victim
head
item2
item1
This code always works, assuming that
previousNode is a pointer to the node before the
one we are deleting previousNode-gtnext
victim-gtnext or we can say previousNode-gtnext
previousNode-gtnext-gtnext
5Dummy node summary Simplifies our algorithms for
insertion and deletion. Uses more memory, the
empty list is bigger (and slower to create).
6Linked list application Logged in users on a
shared computer. List size grows and shrinks as
necessary. On logoff, we can remove a user from
the middle. The sharing policy is time-slicing
each user gets access to the systems resources
for some fixed period of time. The which user
goes next code has a special case for the end of
the list. Could we get rid of this special case?
7Circular Linked Lists The node at the end of the
list contains a link to the node at the head of
the list
8Another problem with our lists What if we want to
traverse our list backwards? Say, to search for
the last occurrence of an item. Or what if we
want to delete an element when we have a pointer
to it (from the find operation)?
9Doubly Linked Lists Each node has a pointer to
the previous node as well as a pointer to the
next node. Such a list looks as follows
Doubly Linked Circular Lists First points (back)
to last and last points (forward) to first.
10Lets look at doubly linked circular lists with a
dummy node typedef int Item_type struct Node
Item_type item Node next, prev We
need a makeEmpty() function that creates an empty
list (one with only a dummy node, but no
realnodes)
11Implementing the makeEmpty()function Node
makeEmpty() Node head new
Node head-gtnext head head-gtprev
head return head
12Implementing the insertFirst() function void
insertFirst( Node head, const Item_type
item )//Pre head points to the dummy node of a
list//Post item is inserted at the front of the
list Node newNode new Node
newNode-gtitem item newNode-gtnext ???
newNode-gtprev ??? ???-gtprev newNode
???-gtnext newNode
13Implementing the insertLast() function void
insertLast( Node head, const Item_type item
)//Pre head points to the dummy node of a list
//Post item is inserted at the end of the
list Node newNode new Node
newNode-gtitem item newNode-gtnext ???
newNode-gtprev ??? ???-gtprev newNode
???-gtnext newNode
14Implementing the find() function does the old
version work? Node find( Node head, const
Item_type item )//Pre head points to the dummy
node of a list //Post if item is in the list, a
pointer to the node//containing it is returned
otherwise a null pointer//is returned
Node cursor head while( cursor ! NULL
cursor-gtitem ! item ) cursor
cursor-gtnext return cursor
15Implementing the find() function the new
version Node find( Node head, const
Item_type item )//Pre head points to the dummy
node of a list //Post if item is in the list, a
pointer to the node//containing it is returned
otherwise a null pointer//is returned
Node cursor head-gtnext while( cursor !
head cursor-gtitem ! item ) cursor
cursor-gtnext return (cursor head) ? NULL
cursor
16Implementing the remove() function void remove(
Node victim )//Pre victim points to the node
to be removed //Post the victim node is
removed from the list
17Implementing the remove() function void remove(
Node victim )//Pre victim points to the node
to be removed //Post the victim node is
removed from the list victim-gtnext-gtprev
victim-gtprev victim-gtprev-gtnext
victim-gtnext
18If we have two link fields in our nodes, we could
also maintain two lists through one set of data.
Suppose we want to sort students by last name
and student number. We can maintain a list of
students sorted by both criteria by using our two
link fields and two head pointers
nameList
numList