Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem Understanding

Similar presentations


Presentation on theme: "Problem Understanding"— Presentation transcript:

1 Problem Understanding
Recall Pseudocode 👍 The ‘Lifecycle’ of a Solution 2 Examples Solution is available on my webpage now. Problem Understanding Design Pseudocode Coding Testing

2 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

3 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. Quipu

4 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’

5 Arrays An array can store primitive elements, such as characters
An array can also store references to objects

6 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

7 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

8 So what is good for a changing structure ?
Dynamic scenarios So what is good for a changing structure ? Lists

9 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

10 A Nested Node Class

11 Methods

12 Inserting at the Head Allocate new node Insert new element
Have new node point to old head Update head to point to new node

13 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

14 Java Methods

15 Removing at the Head Update head to point to next node in the list Allow garbage collector to reclaim the former first node

16 Java Method

17 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

18 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; }

19 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

20 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

21 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

22 Doubly-Linked List in Java

23 Doubly-Linked List in Java, 2

24 Doubly-Linked List in Java, 3

25 Doubly-Linked List in Java, 4

26 قَالَ رسول الله صلى الله عليه وسلم لشداد بن أوس:
Break قَالَ رسول الله صلى الله عليه وسلم لشداد بن أوس: يَا شَدَّادُ ، إِذَا رَأَيْتَ النَّاسَ يَكْنِزُونَ الذَّهَبَ وَالْفِضَّةَ ، فَاكْنِزْ هَؤُلَاءِ الْكَلِمَاتِ:  اللَّهُمَّ إِنِّي أَسْأَلُكَ الثَّبَاتَ فِي الأَمْرِ، وَالعَزِيمَةَ على الرُّشْدِ، وَأَسْأَلُكَ مُوجِبَاتِ رَحمَتِك وَعَزَائِمَ مَغْفِرَتِك، وَأَسْأَلُكَ شُكْرَ نِعْمَتِكَ وَحُسْنَ عِبَادَتِكَ ، وَأَسْأَلُكَ قَلْبًا سَلِيمًا وَلِسَانًا صَادِقًا، وَأَسْأَلُكَ مِنْ خَيْرِ مَا تَعْلَمُ ، وَأَعُوذُ بِكَ مِنْ شَرِّ مَا تَعْلَمُ ، وَأَسْتَغْفِرُكَ لِمَا تَعْلَمُ إِنَّكَ أَنْتَ عَلَّامُ الْغُيُوبِ 

27 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.

28 Singly Linked List equals method

29 Cloning Data Structures
backup = data; shallow copy: backup = data.clone( ); But for references:

30 deep copy of a two-dimensional array

31 deep copy of a singly linked list

32 Cloning Data Structures
shallow copy <deep> copy ?! How deep do you want to go ?

33 Reading [G] all Chapter 3


Download ppt "Problem Understanding"

Similar presentations


Ads by Google