Download presentation

Presentation is loading. Please wait.

Published byElaine Chinnock Modified over 2 years ago

1
Dr Zeinab Eid 1 Chapter 3 Lists

2
Dr Zeinab Eid 2 3.3 Doubly-Linked Lists It is a way of going both directions in a linked list, forward and reverse. 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. Many applications require a quick access to the predecessor node of some node in list.

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

4
Dr Zeinab Eid4 Doubly Linked List A doubly linked list provides a natural implementation of the List ADT A doubly linked list provides a natural implementation of the List ADT Nodes implement Position and store: Nodes implement Position and store: elementelement link to the previous nodelink to the previous node link to the next nodelink to the next node Special trailer and header nodes Special trailer and header nodes prevnext elem trailer header nodes/positions elements node

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

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

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

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

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

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

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

12
Dr Zeinab Eid12 Insertion Insertion We visualize operation AddAfter(p, X), which returns position q We visualize operation AddAfter(p, X), which returns position q ABXC ABC p ABC p X q pq

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

14
Dr Zeinab Eid14 Deletion We visualize remove(p), where p = last() We visualize remove(p), where p = last() ABCD p ABC D p ABC

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

16
Dr Zeinab Eid16 Performance In the implementation of the List ADT by means of a doubly linked list 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

17
Dr Zeinab Eid17 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure where a single object is stored The Position ADT models the notion of place within a data structure where a single object is stored It gives a unified view of diverse ways of storing data, such as It gives a unified view of diverse ways of storing data, such as –a cell of an array –a node of a linked list Just one method: Just one method: –object element(): returns the element stored at the position

18
Dr Zeinab Eid18 List ADT (§ 5.2.3) The List ADT models a sequence of positions storing arbitrary objects The List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions It establishes a before/after relation between positions Generic methods: Generic methods: –size(), isEmpty() Accessor methods (all return type Position): Accessor methods (all return type Position): –first(), last() –prev(p), next(p) Update methods: Update methods: –replace(p, e) –remove(p) –insertBefore(p, e) –insertAfter(p, e) –insertFirst(e) –insertLast(e)

19
Dr Zeinab Eid19 Implementing Vector ADT with List ADT Vector ADTs: Vector ADTs: –size() and isEmpty() –elemAtRank(integer r) –object replaceAtRank(integer r, object o) –insertAtRank(integer r, object o) –object removeAtRank(integer r) List ADT: List ADT: –size(), isEmpty() –first(), last() –prev(p), next(p) –replace(p, e) –remove(p) –insertBefore(p, e) –insertAfter(p, e) –insertFirst(e) –insertLast(e)

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google