You are on page 1of 16

Chapter 3

Lists

Dr Zeinab Eid

1

Dr Zeinab Eid 2 .3 Doubly-Linked Lists  It is a way of going both directions in a linked list. forward and reverse.  Many applications require a quick access to the predecessor node of some node in list.3.

Advantages over Singly-linked Lists  Quick update operations: such as: insertions. Dr Zeinab Eid 3 .  A node in a doubly-linked list store two references: • A next link. and also at the middle of the list. deletions at both ends (head and tail). that points to the previous node in the list. that points to the next node in the list. and • A prev link.

Doubly Linked List  A doubly linked list provides a natural implementation of the List ADT  Nodes implement Position and store: • element • link to the previous node • link to the next node prev next elem node  Special trailer and header nodes header nodes/positions trailer elements Dr Zeinab Eid 4 .

Dr Zeinab Eid 5 .Sentinel Nodes  To simplify programming.  Tail: trailer sentinel has a null-next reference (link). do not store any data elements.  Head and tail are dummy nodes.  Head: header sentinel has a null-prev reference (link). two special nodes have been added at both ends of the doubly-linked list. also called sentinels.

Size-counter that keeps track of the number of nodes in the list (excluding the two sentinels). Reference to sentinel head-node. Dr Zeinab Eid 6 .What we see from a Douby-linked List? A doubly-linked list object would need to store the following: 1. 2. Reference to sentinel tail-node. and 3.

instead. we have no nulllinks. and also is the last node: first node is head.Empty Doubly-Linked List: Using sentinels.next = tail tail.prev = head Singl Node List: Size = 1 first last header trailer header trailer This single node is the first node.next last node is tail.prev Dr Zeinab Eid 7 . we have: head.

next T. Dr Zeinab Eid 8 .next ← head.prev ← T {Order is important} head.Insertion into a Doubly-Linked List 1.next ← T Size++ This Algorithm is valid also in case of empty list.data ← y T. AddFirst Algorithm To add a new node as the first of a list: Algorithm addFirst() new(T) T.prev ← head head.next.

data ← y T.prev ← T Size++ This Algorithm is valid also in case of empty list.prev tail.Insertion into a Doubly-Linked List (Cont.) 2. Dr Zeinab Eid 9 . AddLast Algorithm To add a new node as the last of list: Algorithm addLast() new(T) T.prev ← tail.next ← tail T.prev.next ← T {Order is important} tail.

Removal from a Doubly-Linked List 3. If not. we will remove the last node in the list. Dr Zeinab Eid 10 . as shown in Figure above. RemoveLast Algorithm Notice that before removal. we must check for empty list.

in which case we’ll get an empty list. Dr Zeinab Eid 11 .prev ← T.next ← tail tail.prev delete(T) {garbage collector} size-return y } This algorithm is valid also in case of a single node.prev. size=1.prev y ← T.Algorithm removeLast() If size = 0 then output “error” else { T ← tail. Algorithm is one statement.data T.

which returns position q p A p A p A Dr Zeinab Eid B C B X q B X q C C 12 .Insertion  We visualize operation AddAfter(p. X).

setElement(e) v.setPrev(p) {link v to its predecessor} v.getNext()).setNext(v) {link p to its new successor.setPrev(v) {link p’s old successor to v} p. v} return v {the position for the element e} Dr Zeinab Eid 13 .e): Create a new node v v.Insertion Algorithm Algorithm insertAfter(p.getNext()) {link v to its successor} (p.setNext(p.

where p = last() p C C D A A B B p D A Dr Zeinab Eid B C 14 .Deletion  We visualize remove(p).

getPrev()) p.getNext()).setPrev(null) {invalidating the position p} p.getPrev()).setNext(null) return t Dr Zeinab Eid 15 .getNext()) {linking out p} (p.setPrev(p.Deletion Algorithm Algorithm remove(p): t = p.element {a temporary variable to hold the return value} (p.setNext(p.

Performance  In the implementation of the List ADT by means of a doubly linked list – The space used by a list with n elements is O(n) – The space used by each position of the list is O(1) – All the operations of the List ADT run in O(1) time – Operation element() of the Position ADT runs in O(1) time Dr Zeinab Eid 16 .