1 / 51

Big-Picture & Chapter 5

Big-Picture & Chapter 5. Why are things so complicated? & Linked Structures. Big Picture. List (Objects) <interface>. List (Batters) <array-based implementation>. Linked-list based <implements List>. Array-based List <implements List>. List (Pitchers) <array-based implementation >. ….

petec
Télécharger la présentation

Big-Picture & Chapter 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Big-Picture & Chapter 5 Why are things so complicated? & Linked Structures

  2. Big Picture List (Objects)<interface> List (Batters)<array-based implementation> Linked-list based<implements List> Array-based List<implements List> List (Pitchers)<array-based implementation > … List (Batters)<linked-list implementation> … … List (Pitchers)<linked-list implementation > List (Coaches)<linked-list implementation > List (Coaches)<array-based implementation >

  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 List (Listable)<interface> Listable<interface> Linked-list based<implements List> Array-based List<implements List> Batter<implements Listable> … Pitcher<implements Listable> … … Coach<implements Listable > Fruitcake<implements Listable> Cockroach<implements Listable>

  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 List (Player)<interface> Player<base-class> Linked-list based<implements List> Array-based List<implements List> Batter<extends Player> Pitcher<extends Player>

  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. Abstract View Internal View Results of Stack Operations Using StackNode (Cont’d)

  16. Internal View Abstract View Results of Stack Operations Using StackNode (Cont’d)

  17. Internal View Abstract View Results of Stack Operations Using StackNode (Cont’d)

  18. Internal View Abstract View Results of Stack Operations Using StackNode (Cont’d)

  19. The push Operation

  20. Results of push Operation

  21. Results of push Operation (Cont’d)

  22. Results of push Operation (Cont’d)

  23. Results of push Operation (Cont’d)

  24. The Code for the push Method

  25. The Code for our pop Method

  26. The top Method

  27. Linked Structures Implementing a Queue

  28. A Linked Queue Representation

  29. The Enqueue Operation

  30. The enqueue Method

  31. The Dequeue Operation

  32. The dequeue Method

  33. Alternate Queue Implementations

  34. A Basic Linked List Sorted and Unsorted

  35. List with Three Items

  36. The isFull and lengthIs Methods

  37. The reset and getNextItem Methods

  38. getNextItem()

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

  40. The retrieve Method

  41. myList.retrieve(7)

  42. Retrieving an Item from a List

  43. The delete Method

  44. Big-O Comparison of Unsorted List Operations

More Related