Chapter 3 Lists Dr Zeinab Eid.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Linked Lists Chapter 4.
Queues and Linked Lists
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
Doubly Linked List This problem can be easily solved by using the double linked list. - Ed. 2 and 3.: Chapter 4 - Ed. 4: Chapter 3.
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
6/7/2014 8:24 AMSequences1 Lists and Sequences. 6/7/2014 8:24 AMSequences2 Outline and Reading Singly linked list Position ADT and List ADT (§5.2.1) Doubly.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
3 May Linked Lists CSE 2011 Winter Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of.
Review Learn about linked lists
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
Chapter 6: Sequences : vectors and lists COL 106 Shweta Agrawal.
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 3: Arrays, Linked Lists, and Recursion
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
Iterators, Lists, and Sequences Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
CS 221 Analysis of Algorithms Data Structures Vectors, Lists.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Sequences1 Vectors Positions Lists General Sequences Bubble Sort Algorithm.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
LINKED LISTS.
Linked List, Stacks Queues
Lecture 6 of Computer Science II
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Linked List Stacks, Linked List Queues, Dequeues
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
EEL 4854 IT Data Structures Linked Lists
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
LINKED LISTS CSCD Linked Lists.
Sequences and Iterators
Vectors 11/29/2018 6:45 PM Vectors 11/29/2018 6:45 PM Vectors.
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
CS212D: Data Structures Week 5-6 Linked List.
Vectors, Lists and Sequences
Linked Lists & Iterators
Header and Trailer Sentinels
Vectors, Lists, and Sequences
Vectors 4/26/2019 8:32 AM Vectors 4/26/2019 8:32 AM Vectors.
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Presentation transcript:

Chapter 3 Lists Dr Zeinab Eid

3.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. Dr Zeinab Eid

Advantages over Singly-linked Lists 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 next link; that points to the next node in the list, and A prev link; that points to the previous node in the list. Dr Zeinab Eid

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 Special trailer and header nodes prev next elem node trailer header nodes/positions elements Dr Zeinab Eid

Sentinel Nodes 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: header sentinel has a null-prev reference (link). Tail: trailer sentinel has a null-next reference (link). Dr Zeinab Eid

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

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 header trailer first last header trailer Dr Zeinab Eid

Insertion into a Doubly-Linked List 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. Dr Zeinab Eid

Insertion into a Doubly-Linked List (Cont.) AddLast Algorithm To add a new node as the last of list: 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. Dr Zeinab Eid

Removal from a Doubly-Linked List 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. Dr Zeinab Eid

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

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

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 p’s old successor to v} p.setNext(v) {link p to its new successor, v} return v {the position for the element e} Dr Zeinab Eid

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

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 Dr Zeinab Eid

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

Position ADT (§ 5.2.2) 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 a cell of an array a node of a linked list Just one method: object element(): returns the element stored at the position Dr Zeinab Eid

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

Implementing Vector ADT with 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) Vector ADTs: size() and isEmpty() elemAtRank(integer r) object replaceAtRank(integer r, object o) insertAtRank(integer r, object o) object removeAtRank(integer r) Dr Zeinab Eid