Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31

Slides:



Advertisements
Similar presentations
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Advertisements

© 2004 Goodrich, Tamassia Sequences and Iterators1.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
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.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
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.
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Dynamic Arrays and Stacks CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
Data Structures Using C++1 Chapter 5 Linked Lists.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
Parasol Lab, Dept. CSE, Texas A&M University
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Linked List, Stacks Queues
Elementary Data Structures
Lecture 6 of Computer Science II
Unit – I Lists.
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Data Structure By Amee Trivedi.
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.
Ch7. List and Iterator ADTs
Lectures linked lists Chapter 6 of textbook
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Doubly Linked Lists 6/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Review Deleting an Element from a Linked List Deletion involves:
Linked List Stacks, Linked List Queues, Dequeues
COMP9024: Data Structures and Algorithms
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
EEL 4854 IT Data Structures Linked Lists
Presentation by Marty Krogel
LINKED LISTS CSCD Linked Lists.
Array Lists, Node Lists & Sequences
Elementary Data Structures
Ch7. List and Iterator ADTs
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Arrays and Linked Lists
Vectors 11/23/2018 1:03 PM Growing Arrays Vectors.
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Stacks.
" A list is only as strong as its weakest link. " - Donald Knuth
Array-Based Sequences
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Doubly Linked Lists or Two-way Linked Lists
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
CS212D: Data Structures Week 5-6 Linked List.
CS2013 Lecture 4 John Hurley Cal State LA.
Lists CSE 373 Data Structures.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Problem Understanding
Recall What is a Data Structure Very Fundamental Data Structures
LINKED LISTS.
Linked Lists & Iterators
Lists CSE 373 Data Structures.
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Vectors and Array Lists
Chapter 9 Linked Lists.
Stacks and Linked Lists
Problem Understanding
Presentation transcript:

Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31

Singly Linked Lists (§ 3.2) Sequences 07/25/16 10:31 Singly Linked Lists (§ 3.2) A singly linked list is a sequence of nodes. Each node stores: an element, a link to the next node. head points to first node. next node elem head  A B C D Singly Linked Lists 2

Inserting at the Head Allocate new node. Insert element. Sequences 07/25/16 10:31 Inserting at the Head Allocate new node. Insert element. Make new node point to old head. Update head to point to new node. Singly Linked Lists 3

Removing the Head Make head point to its next node. Sequences 07/25/16 10:31 Removing the Head Make head point to its next node. Free old first node. Singly Linked Lists 4

Inserting at the Tail Use tail pointer. Allocate new node. Sequences 07/25/16 10:31 Inserting at the Tail Use tail pointer. Allocate new node. Insert element. Set next to null. Make tail node point to new node. Update tail. Singly Linked Lists 5

Sequences 07/25/16 10:31 Removing the Tail No constant-time way to update the tail to point to the previous node. Singly Linked Lists 6

Doubly Linked Lists (§ 3.3) Sequences 07/25/16 10:31 Doubly Linked Lists (§ 3.3) Node stores pointers to prev and next nodes. Special trailer and header nodes. prev next elem node trailer header nodes/positions elements Doubly Linked Lists 7

Insertion Insert x before p. p a b c p a b c q x q p a b x c Sequences 07/25/16 10:31 Insertion Insert x before p. p a b c p a b c q x q p a b x c Doubly Linked Lists 8

Insertion Algorithm Algorithm insert(p, e): {insert e before p} Sequences 07/25/16 10:31 Insertion Algorithm Algorithm insert(p, e): {insert e before p} Create a new node v velement = e u = pprev vnext = p; pprev = v {link in v before p} vprev = u; unext = v {link in v after u} Doubly Linked Lists 9

Deletion Delete node p. p a b c d a b c p d a b c Doubly Linked Lists Sequences 07/25/16 10:31 Deletion Delete node p. p a b c d a b c p d a b c Doubly Linked Lists 10

Deletion Algorithm Algorithm delete(p): u = pprev w = pnext Sequences 07/25/16 10:31 Deletion Algorithm Algorithm delete(p): u = pprev w = pnext unext = w {linking out p} wprev = u Doubly Linked Lists 11

Performance A list of n elements takes O(n) space. Sequences 07/25/16 10:31 Performance A list of n elements takes O(n) space. Inserting at a position takes O(1) time. Deleting at a position takes O(1) time. Finding an element takes O(n) time. Doubly Linked Lists 12

Array Implementation of Lists Sequences 07/25/16 10:31 Array Implementation of Lists Represent a list with an array A of size N. Store the size of the list in a variable n. We will handle overflow soon. A 1 2 i n N Array Lists 13

Sequences 07/25/16 10:31 Insertion Make room for new element i by shifting forward the n  i elements A[i], …, A[n  1]. In the worst case (i  0), this takes O(n) time. A 1 2 i n A 1 2 i n A o 1 2 i n Array Lists 14

Sequences 07/25/16 10:31 Removal Fill the hole left by element i by shifting backward the n i  1 elements A[i  1], …, A[n  1]. In the worst case (i  0), this takes O(n) time. A 1 2 n o i A 1 2 n i A 1 2 n i Array Lists 15

Dynamic Array List Insert at the head, which is element t of array S. Sequences 07/25/16 10:31 Dynamic Array List Insert at the head, which is element t of array S. When S is full, copy it to a larger array then insert. How large? Incremental strategy: increase the size by a constant c. Doubling strategy: double the size. Algorithm insert(o) if t = S.length  1 A  new array of size … for i  0 to n1 do A[i]  S[i] S  A n  n + 1 S[n1]  o Array Lists 16

Comparison of the Strategies Sequences 07/25/16 10:31 Comparison of the Strategies We compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n insertions. We start with an empty list represented by an array of size 1. The amortized time of an insertion is the average over the n operations, T(n)/n. Array Lists 17

Incremental Strategy Analysis Sequences 07/25/16 10:31 Incremental Strategy Analysis We replace the array k = n/c times. The total time T(n) of a series of n insert operations is proportional to n + c + 2c + 3c + 4c + … + kc = n + c(1 + 2 + 3 + … + k) = n + ck(k + 1)/2. Since c is a constant, T(n) is O(n + k2) = O(n2). The amortized time of an insertion is O(n). Array Lists 18

Doubling Strategy Analysis Sequences 07/25/16 10:31 Doubling Strategy Analysis We replace the array k = log2 n times. The total time T(n) of n insertions is proportional to n + 1 + 2 + 4 + 8 + …+ 2k = n  2k + 1 1 = 3n 1. T(n) is O(n). The amortized time is O(1). Array Lists 19