1 / 102

Linked Lists

Linked Lists. Starring: Singly Linked List ListNode Doubly Linked List Iterator Co-Starring: ListIterator . Purpose: In this lecture series we will learn about Linked Lists Linked Lists are another type of Data Structure based off of the List Abstract Data Type.

aaralyn
Télécharger la présentation

Linked Lists

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. Linked Lists Starring: Singly Linked List ListNode Doubly Linked List Iterator Co-Starring: ListIterator

  2. Purpose: • In this lecture series we will learn about Linked Lists • Linked Lists are another type of Data Structure based off of the List Abstract Data Type. • Linked Lists provide advantages in terms of efficiency when certain types of actions need to be performed on the data.

  3. Resources: • Java Methods AB Data Structures Chapter 2 p.34 • Java Essentials Chapter 19 p.737 • Java Essentials Study Guide Chapter 16 p.263 • Barrons Chapter 8 p.244 & Chapter 11 p.361 • Big Java Chapter Chapter 19 p.737 • Lambert Comprehensive Units 4 & 5 (chs 12-16) • Taft Notes p.12 (old) & also in last summers notes • Taft Notes 2003

  4. Handouts: 1. Java Classes And Interfaces: List Linked List Iterator ListIterator 2. ListNode Class 3. Sample Code (UsingLinkedList.java & SampleLinkedList.java) 4. ListIterator Illustration JESG p.266-268 & p.269-280

  5. What we Will Cover in This Lecture: • The List Interface as it pertains to the ArrayList and Linked List Data Structures • Advantages and Drawbacks of the ArrayList Data Structure

  6. What we Will Cover in This Lecture: • The Need for an Alternate method of Data Storage • The Concept of the Linked List • The ListNode Class (Singly LL)

  7. What we Will Cover in This Lecture: • Behaviors of a Linked List: Create, Insert (Add) , Remove, Traverse (Iterate) • Singly, Doubly, Circular LL & LL with a Tail • Review Of Sample Code (Instructor)

  8. What we Will Cover in This Lecture: • TPS Create Your Own Linked List Class • Java’s LinkedList Class • Doubly LL

  9. What we Will Cover in This Lecture: • Iterator and ListIterator Classes • Iterating Thru a LinkedList (Java Class) • Big-O of a Linked List (compared to ArrayList) • The AP AB Requirements

  10. The List Interface • Lets Re-focus on the List Interface and then we will look at a class, LinkedList, that implements the List interface

  11. This is merely a structure or a set of criteria that can be implemented different ways • We can have a LinkedList implementation of the Abstract List Structure. All we need to do is make sure we provide for the BEHAVIORS as specified in the Criteria for a List (add, remove, get, size, modify)

  12. Each implementation must be evaluated against their relative “costs” when deciding which one to use for a particular system • The List interface merely identifies the behaviors that MUST exist in a List

  13. The actual implementation of which is left to the specific data structure • As for the Iterator and ListIterator Interfaces, we will discuss these shortly • The List Interface requires you implement a Size and an Add Behavior

  14. The List also requires implementation of a ListIterator. • We will use this when we work with Java’s LinkedList class

  15. The ArrayList Data Structure’s Advantages and Drawbacks • Since Lists can be implemented as either an ArrayList or a LinkedList we need to discuss when we would select one over the other as each has strengths as well as limitations

  16. ArrayLists are good at direct access of the n-th element --- O(1) • This makes arrays excellent for a Binary Search as it requires direct access to the middle of an Array of elements

  17. LinkedLists, by their nature, do not provide direct access to specific Nodes because each element only knows about its previous or next Node. So access to a specific Node is a Linear operation --- O(n)

  18. Arrays are not good at inserting elements at the top or near the middle and it is also not best used when the size of the list varies widely--- O(n)

  19. LinkedLists are best at insertion and removal of specific nodes as we merely have to rearrange the links --- O(1) • They are also good in situations where the size of the list is unknown as nodes are only allocated as needed, leading to no wasted memory

  20. You do not need to RESIZE a LinkedList

  21. Therefore, when the data being stored is static and its size is known an Array is a better choice because its weaknesses are minimized and its strengths compliment the dominant process of searching the data --- Log(n)

  22. In situations where the size and content of the data vary to a point where these operations become the dominant process a LinkedList is preferred • This is because the insertion and removal are efficient , O(1), and we can live with sporadic searches --- O(n)

  23. Alternate List Implementation (Linked List) • We can think of an Array or an ArrayList as a book • A Book is a contiguous set of elements indexed by page numbers • We can read through the book page by page or we can open it directly to any part of the book • That’s what a book is best at doing

  24. However, we can not easily insert a new page into an existing book as we would have to rearrange the pages that follow the insert • Also, if the book becomes too big for its bindings, we would need to transfer the pages to a Bigger one

  25. We can think of a LinkedList as a magazine article • The article appears “in parts” scattered throughout the book • We need to know where the first “part” of the article is

  26. Then at the end of each “part” we are told where the next “part” is • We only know when the article is completed when it says “END” at the end of the last “part”

  27. Since the article is spread out, it is not a problem to insert a new piece to the story • It is also easy to remove a part of the story • However, we can only get to a specific “part” of the story by traversing the entire article from the beginning

  28. The Conceptual Linked List • More formally, an element of a Linked List is a called a “node” • Each node contains information plus a pointer to the next node • The nodes provide REFERENCES to another node

  29. We know a Linked List ends when its last node points to nothing (NULL) • ILLUSTRATION: Given a class called AirFLight that contains a flight destination and a flight number we could construct the following Data Link:

  30. “Head” of the Link is at OC1 ATLANTA #100 OC4 OCE KENNEDY #350 OC7 ORLANDO #500 NEWARK #300 NULL

  31. The ListNode Implementation (for AB Exam) • The previous illustrates a Singly Linked List • This type of LinkedList contains a Head node and each node only knows about its next node

  32. A Singly Linked List is best used in cases where we insert new nodes in the beginning (not in the middle or the end) of the list and where searches in the list are minimized • Go to the handout that displays the ListNode class • For us to create our own LinkedList we need to create a “node” class

  33. This class must contain two class level attributes, one to hold an actual data element and one to hold the reference to the next node in the list • There are also methods to set these values

  34. public class ListNode { private Object value; private ListNode next; public ListNode(Object initValue, ListNode initNext) { value = initValue; next = initNext; } public Object getValue() { return value; } public ListNode getNext() { return next; }

  35. public void setValue(Object theNewValue) { value = theNewValue; } public void setNext(ListNode theNewNext) { next = theNewNext; }

  36. This ListNode is self-referential as it refers to the ListNode data type inside the ListNode datatype • When we implement our own LinkedList we will use this class as our standard node class • The ListNode class, as currently constructed, reflects a Singly LinkedList

  37. Furthermore, the ListNode class is going to be used on the AP exam for questions on any LinkedList question • Also this class would be used as a base for any LinkedList Free Response question • What is missing from this class is the actual data that is to be maintained at each node

  38. The attribute private Object value; needs to hold some value • It can be ANY object, but we will use in our examples an object that maintains flight information

  39. public class AirFlight { private String Destination; private int flightNum; public AirFlight(String s, int num) { Destination = s; flightNum = num; } public String getDestination() { return Destination; }

  40. public int getFlightNum() { return flightNum; } public String toString() { return Destination + " flight #" + flightNum; } }

  41. The flight class does not “KNOW” it is a part of a list and it only contains information about a specific airline flight

  42. The ListNode class does not care about the type of object at each node, it only cares about maintaining a REFERENCE to an object and the LOCATION of the next node

  43. This provides a level of abstraction • In the next sections we will examine the SampleLinkedList code to see a LinkedList in action

  44. Linked List Behaviors • Like any other List Data Structure a LinkedList must provide for the following behaviors: Create a new list Insert (Add) a new node to the list Remove a node from the list Traverse (Iterate) the nodes in the list

  45. To begin working with your own LinkedList (not java’s LinkedList class) you need to create a class that will hold the information you wish to store • You then need to Create an initial Node in the list and make this node the head of the list

  46. Lets work with our flight class and create an initial instance of it: AirFlight f1 = new AirFlight("Atlanta", 100); • Then create an initial node to maintain the flight data: ListNode node = new ListNode(f1, null); // A • Then establish this as the start of the list: ListNode head = null; head = node;

  47. We can now add or insert nodes to the front of the list • We can do this 2 ways, we can use the ListNodes setNext method to point the initial node to the next node in the list: head.setNext(new ListNode(f2, null)); // A O

  48. Or we can use the ListNodes constructor to establish a new node and reset the head head = new ListNode(f4,head); // N K A O head = new ListNode(f5,head); // U N K A O head = new ListNode(f6,head); // H U N K A O • Lets Draw out the resulting linked list on the Board…

  49. We can print out the contents of the first node: System.out.println("The first Node is " + ((AirFlight)head.getValue()).toString()); • Note that AirFlight class has a toString method

More Related