Download presentation
Presentation is loading. Please wait.
Published byRuby White Modified over 8 years ago
1
Big-Picture & Chapter 5 Why are things so complicated? & Linked Structures
2
Big Picture List (Batters) List (Pitchers) List (Objects) Array-based List List (Batters) List (Pitchers) List (Coaches) Linked-list based … … …
3
Why Interfaces? It defines a standard way that a data structure is used. –Tells you what methods are available –Method name –Parameters –etc. Programmers can switch to a completely different underlying implementation and they only have to change one line of code.
4
Big Picture Listable Batter List (Listable) Array-based List Pitcher Coach Fruitcake Cockroach Linked-list based … … …
5
Why the Listable interface? Wasn’t a list of Objects good enough? Isn’t everything in Java an Object? Yes, but the java.lang.Object does not have methods for comparing Objects Thus, your list can never compare Objects to see if they are in the right order. Thus, you can never have a sorted list.
6
Why the Listable interface? The Listable Interface just provides an abstract method called compare, which can be implemented by any Class. Thus, any Class that implements Listable must implement the compare function. Thus, our List can maintain a sorted list without even knowing any details about the objects stored in the list.
7
Big Picture Player Batter List (Player) Array-based List Pitcher Linked-list based
8
Can we use inheritance? Yes, but here are the problems: List (Players) can only call functions that are implemented in the Player base-class. Players are sorted based on Rating Rating is based on the 10 statistical categories, which are different for batters vs pitchers. Thus, Rating can not be implmented inside of the Player base class.
9
Can we use inheritance? We can create a sorted list of Batters or Pitchers But we can’t put both Batters and Pitchers in a sorted list of Players. Inheritance is really only useful in consolidating fields and methods from a set of classes that are similar. Inheritance does NOT help us create generic data structures.
10
Can we use inheritance? This is why Dr. B thinks that inheritance is not so important Its just a useful way of organizing classes into hierarchies Interfaces are much more useful in terms of recycling code and making generic data structures.
11
Special Classes Self-referential class A class that includes an instance variable or variables that can hold a reference to an object of the same class Inner class A class defined inside of another class. The outer class can access the private variables of the inner class.
12
Chapter 5 Linked Structures
13
StackNode
14
Results of Stack Operations Using StackNode
15
Results of Stack Operations Using StackNode (Cont’d) Internal View Abstract View
16
Results of Stack Operations Using StackNode (Cont’d) Internal View Abstract View
17
Results of Stack Operations Using StackNode (Cont’d) Internal View Abstract View
18
Results of Stack Operations Using StackNode (Cont’d) Internal View Abstract View
20
The push Operation
21
Results of push Operation
22
Results of push Operation (Cont’d)
25
The Code for the push Method
27
The Code for our pop Method
28
The top Method
29
Linked Structures Implementing a Queue
31
A Linked Queue Representation
32
The Enqueue Operation
33
The enqueue Method
34
The Dequeue Operation
35
The dequeue Method
36
Alternate Queue Implementations
37
A Basic Linked List Sorted and Unsorted
38
List with Three Items
40
The isFull and lengthIs Methods
41
The reset and getNextItem Methods
42
getNextItem()
43
The Remaining Methods We define isThere and insert as abstract methods, leaving their implementation to the concrete classes. We implement both retrieve and delete with our abstract LinkedList class.
44
The retrieve Method
46
myList.retrieve(7)
47
Retrieving an Item from a List
48
The delete Method
50
Big-O Comparison of Unsorted List Operations
51
More slides to come… Linked Lists (unsorted first) (then sorted)
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.