Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implementing Singly Linked

Similar presentations


Presentation on theme: "Implementing Singly Linked"— Presentation transcript:

1 Implementing Singly Linked
Pre-Pointers Insertion Sort Headers Sorted Lists Implementing Singly Linked Lists Riley Ch. 5 (& 3) Handouts SMD181 Lecture 15 Multiple Iterators MasterMind – Code – Iterator Integrity

2 Lecture 15 - Håkan Jonsson
News Book a time slot for the design review on Wednesday, Feb 28 ASAP. You may work in pairs on the design. You still have to write your own program and have that graded. Lecture 15 - Håkan Jonsson

3 Lecture 15 - Håkan Jonsson

4 Lecture 15 - Håkan Jonsson
Headers The implementation of a singly linked listed we saw last lecture can be improved in two ways. 1. Always having a “dummy” node at the beginning of the internal node list. This node is never used to store data. It makes operations on the list simpler. Called a header. Pictures… (Ch. 5.8 in Riley) Lecture 15 - Håkan Jonsson

5 Lecture 15 - Håkan Jonsson
Figures Fig. 5.29, no header node p. 227, same as 5.29 but with a header node. Fig. 5.30, two operations Lecture 15 - Håkan Jonsson

6 Lecture 15 - Håkan Jonsson
Pre-Pointers 2. Another improvement: Let the internal iterator reference variable refer to the node immediately before the iterators current position(!) Is called a pre-pointer. Possible since we have a header (and therefore always one node before another). Makes the implementation of the iterator simpler. Pictures… Lecture 15 - Håkan Jonsson

7 Lecture 15 - Håkan Jonsson
Figures Fig. 5.32, without and with a pre-pointer p , operations removeAT and insertBefore Lecture 15 - Håkan Jonsson

8 Lecture 15 - Håkan Jonsson
Sorted Lists (Riley 6.1) A class for lists in which the elements are ordered (automatically, it seems). ADT in Fig. 6.2. Different from an ordinary list. Stores objects that are Comparable. Comparable is an interface that specifies a method public int compareTo(Object o) The method insert keeps the list ordered. Lecture 15 - Håkan Jonsson

9 Lecture 15 - Håkan Jonsson
(Sorted Lists) Implementation: Has a (private) object of type SimpleSingleListPlus. Fig. 6.1 The method insert works like insert in Insertion Sort. Lecture 15 - Håkan Jonsson

10 Lecture 15 - Håkan Jonsson
Figures Fig. 6.1 Fig. 6.2 Lecture 15 - Håkan Jonsson

11 Lecture 15 - Håkan Jonsson
Insertion Sort The Sorting Problem: Input: n comparable elements in an array. Output: An array with the n input elements in sorted order. Insertion sort: Inserts elements from the input array, one at a time, into a sorted array while it keeps the array sorted. Like when you play cards, pick up one card at a time, and find its proper place. Lecture 15 - Håkan Jonsson

12 Copyright © The McGraw-Hill Companies, Inc.
Lecture 15 - Håkan Jonsson

13 Lecture 15 - Håkan Jonsson
MasterMind Last lecture I handed out an example of a design of MasterMind, a board game. I managed to find also a (simple) implementation! Lecture 15 - Håkan Jonsson

14 Lecture 15 - Håkan Jonsson
MasterMind To note: The class Command has a static method build that produces commands. This is a Command-factory. Each command class inherits Command. So, they all must have a method exec. In the Game class there is a loop in which user input is read and translated into a command, which is then executed. (Small difference: There is no interface Pegs.) Lecture 15 - Håkan Jonsson

15 Lecture 15 - Håkan Jonsson
Multiple Iterators We have seen examples where containers (simple singly linked lists) have one iterator each. This limits what can be done with the container. Fig. 6.6, using two iterators. Better: Separate the list (the container) from the iterator(s). Fig. 6.5 Lecture 15 - Håkan Jonsson

16 Lecture 15 - Håkan Jonsson
A Dilemma A common problem is when two or more classes need access to variables in a third class. Making the variables public means they are accessible also to other classes, which is usually bad (dangerous). A solution is to make one class an inner class of the other! The inner class has full access to the declarations of outer class, also private things. This is how multiple iterators are implemented in the course book. Fig. 6.8. Lecture 15 - Håkan Jonsson

17 Lecture 15 - Håkan Jonsson
Figures Fig. 6.6 (Removing duplicates using two iterators) Fig. 6.5 (UML-diagram of the list and its iterator) Fig (Final solution) Lecture 15 - Håkan Jonsson


Download ppt "Implementing Singly Linked"

Similar presentations


Ads by Google