Problem Understanding Recall Pseudocode 👍 The ‘Lifecycle’ of a Solution 2 Examples Notes are available on my webpage now. Problem Understanding Design Pseudocode Coding Testing
Outline What is a Data Structure Very Fundamental Data Structures Arrays What Arrays are good for What Arrays do poorly Linked Lists
What is a Data Structure Arrangement or organization of data [in memory or anywhere] The arrangement is to make something possible or make it easer, faster, or more effecent. www.youtube.com/watch?v=EYq-VtyAd2s
Arrays Array is a sequence of n items of the same data type that are stored contiguously in computer memory and made accessible by specifying a value of the index Each and every element of an array can be accessed in the same constant amount of time regardless of where in the array the element in question is located. Length is set in ‘declaration’
Arrays An array can store primitive elements, such as characters An array can also store references to objects
Arrays and dynamic scenarios To add an entry e into array board at index i, we need to make room for it by shifting forward the n - i entries
Arrays and dynamic scenarios To remove the entry e at index i, we need to fill the hole left by e by shifting backward the n - i - 1 elements
Dynamic scenarios So what is good for a changing structure ? Lists
Singly Linked List A singly linked list is a concrete data structure consisting of a sequence of nodes, starting from a head pointer Each node stores element link to the next node next node element head A B C D
A Nested Node Class
Accessor Methods
Inserting at the Head Allocate new node Insert new element Have new node point to old head Update head to point to new node
Inserting at the Tail Allocate a new node Insert new element Have new node point to null Have old last node point to new node Update tail to point to new node
Java Methods
Update head to point to next node in the list Removing at the Head Update head to point to next node in the list Allow garbage collector to reclaim the former first node
Java Method
Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node
A doubly linked list can be traversed forward and backward Nodes store: element link to the previous node link to the next node Special trailer and header nodes prev next element node trailer header nodes/positions elements
p A B C p q A B C X p q A B X C Insertion Insert a new node, q, between p and its successor. p A B C p q A B C X p q A B X C
A B C D p A B C p D A B C Deletion Remove a node, p, from a doubly linked list. A B C D p A B C p D A B C
Doubly-Linked List in Java © 2014 Goodrich, Tamassia, Goldwasser
Doubly-Linked List in Java, 2 © 2014 Goodrich, Tamassia, Goldwasser
Doubly-Linked List in Java, 3 © 2014 Goodrich, Tamassia, Goldwasser
Doubly-Linked List in Java, 4 © 2014 Goodrich, Tamassia, Goldwasser
Reading [G] Section 3.1 [G] Section 3.2, 3.3, and 3.4