Problem Understanding Recall Pseudocode 👍 The ‘Lifecycle’ of a Solution 2 Examples Solution is 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 Singly linked list Doubly linked list Equivalence Testing Cloning a data structure
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 efficient. www.youtube.com/watch?v=EYq-VtyAd2s Quipu
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
So what is good for a changing structure ? 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
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
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 quick way to update the tail to point to the previous node
Finding the second to last node Very Important technique public Node<E> penultimate( ) { Node<E> walk = head; while (walk.next != tail) walk = walk.next; return walk; }
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
Doubly-Linked List in Java, 2
Doubly-Linked List in Java, 3
Doubly-Linked List in Java, 4
قَالَ رسول الله صلى الله عليه وسلم لشداد بن أوس: Break قَالَ رسول الله صلى الله عليه وسلم لشداد بن أوس: يَا شَدَّادُ ، إِذَا رَأَيْتَ النَّاسَ يَكْنِزُونَ الذَّهَبَ وَالْفِضَّةَ ، فَاكْنِزْ هَؤُلَاءِ الْكَلِمَاتِ: اللَّهُمَّ إِنِّي أَسْأَلُكَ الثَّبَاتَ فِي الأَمْرِ، وَالعَزِيمَةَ على الرُّشْدِ، وَأَسْأَلُكَ مُوجِبَاتِ رَحمَتِك وَعَزَائِمَ مَغْفِرَتِك، وَأَسْأَلُكَ شُكْرَ نِعْمَتِكَ وَحُسْنَ عِبَادَتِكَ ، وَأَسْأَلُكَ قَلْبًا سَلِيمًا وَلِسَانًا صَادِقًا، وَأَسْأَلُكَ مِنْ خَيْرِ مَا تَعْلَمُ ، وَأَعُوذُ بِكَ مِنْ شَرِّ مَا تَعْلَمُ ، وَأَسْتَغْفِرُكَ لِمَا تَعْلَمُ إِنَّكَ أَنْتَ عَلَّامُ الْغُيُوبِ
Equivalence Testing Object. equals With Arrays a == b a.equals(b) Arrays.equals(a,b) With Nested Arrays Arrays.deepEquals(a,b) See Book for LL details.
Singly Linked List equals method
Cloning Data Structures backup = data; shallow copy: backup = data.clone( ); But for references:
deep copy of a two-dimensional array
deep copy of a singly linked list
Cloning Data Structures shallow copy <deep> copy ?! How deep do you want to go ?
Reading [G] all Chapter 3