16 DoublyLinkedLists

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 11

Menu

• Finding, Adding, and Removing in


Linked Lists
• Doubly Linked Lists

• Test (Reminder):
• There will be a 90 minute test
• Friday 15th at 3-5pm
• EA006
1
Fi ndi ng Nodes in a
SLL
If we use a list for ‘general’ data, we need operations to
find, add and remove.
 Find should traverse the list until it finds the target item:
Unordered List Ordered List

public E find (E item) { public E find (E item) {


if (value.equals(item)) int result =
return value; compare(value,item);
if (next == null) if(result == 0)
return null; return value;
else if (next == null || result > 0)
return next.find(item); return null;
} else
return next.find(item);
}
2
Recal l: Bi nar y Sear ch
 Return the index of where the item ought to be, whether
present or not.

private int findIndex(Object item){


Comparable<E> value = (Comparable<E>) item;
int low = 0;     
int high  =  count;      // index in [low .. high]
while (low < high){
    int mid  =  (low + high) / 2;
    if (value.compareTo(data[mid]) > 0) // index in [mid+1 .. high]
        low = mid + 1;          // index in [low .. high]  low <=
high
    else                    // index in [low .. mid]
        high = mid;        // index in [low .. high],
low<=high
}
3
return low;    // index in [low .. high] and low = high 
Fi ndi ng Nodes in a
SLL
It would be more useful for add (sorted list) and
remove if we returned the position in the list where it
ought to be (just like we saw with Add with Binary
Search).
Unordered List Ordered List
public LinkedNode<E> find(E item){
public LinkedNode<E> find(E item)
{ int result =
compare(value,item);
if(value.equals(item))
if(result == 0)
return this;
return this;
if (next == null)
if (next == null || result > 0)
return null;
return this;
else
else
return next.find(item);
return next.find(item);
}
}
4
Ad di ng node s in a SLL
• Unsorted list (put at the front)
• Sorted list, insert in the right place

Ordered List

public LinkedNode<E> add(E item){


LinkedNode<E> pos = find(item);
pos.next = new LinkedNode<E>(item, pos.next);
return this;
}

 What we need from find is:


– If it is in the list, we want the pointer to the item.
– If it isn’t in the list we need the pointer to the one
before.

5
Modi fie d Fi nd

• We can write this special version of find:


• that returns the previous node, but
• what about the first element in the list (messy), and
• we have to do an additional test for equality (and null),
and
• can’t reuse a find that ‘fits’ accepted semantics.
• This solution is ‘papering over the cracks’.

• Even if do write this special version of find,


• It’s no use for removing a node that we already have a
reference to.
• Ugly!
6
Remove Example
 If we have a pointer to an element in a list, we
cannot simply remove that element – we have to
traverse the list from the start to locate the
‘previous’ node.
ref ref.remove()
list

G
E I

?
A
K

7
Doubly Linked Lists
• To avoid having to traverse the list, we can
simply store a previous pointer in the node.
Doubly
Linked List
3 1 5 4 5
• Advantages:
• We can now implement the find, add and remove
operations much more elegantly (reuse of find).
• We can now manipulate the list using references (say
from an index).
• List can be traversed in either direction (i.e. browser
pages)
• Disadvantages:
• more effort in updating an extra set of pointers.
• More space required to store those pointers. 8
Remove Example DLL
ref.remove()
ref.prev.next = ref.next
ref.next.prev = ref.prev

ref
list

G
E

A I

9
Add Exercise DLL
• Write the code to insert E back into the list before ref.
• Note: The first and last node in a list is always a special
case (in both SLL and DLL), don’t worry about this.

ref
E E
list

A I

10
Solution

• To insert a new node pointed to by newPtr


(E) before the node pointed to by cur (ref)
• E.next = ref;
• E.prev = ref.prev; // List not changed
• ref.prev = E;
• E.prev.next = E;

11

You might also like