1.03k likes | 1.29k Views
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.
E N D
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. • Linked Lists provide advantages in terms of efficiency when certain types of actions need to be performed on the data.
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
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
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
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)
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)
What we Will Cover in This Lecture: • TPS Create Your Own Linked List Class • Java’s LinkedList Class • Doubly LL
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
The List Interface • Lets Re-focus on the List Interface and then we will look at a class, LinkedList, that implements the List interface
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)
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
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
The List also requires implementation of a ListIterator. • We will use this when we work with Java’s LinkedList class
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
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
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)
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)
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
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)
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)
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
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
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
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”
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
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
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:
“Head” of the Link is at OC1 ATLANTA #100 OC4 OCE KENNEDY #350 OC7 ORLANDO #500 NEWARK #300 NULL
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
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
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
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; }
public void setValue(Object theNewValue) { value = theNewValue; } public void setNext(ListNode theNewNext) { next = theNewNext; }
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
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
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
public class AirFlight { private String Destination; private int flightNum; public AirFlight(String s, int num) { Destination = s; flightNum = num; } public String getDestination() { return Destination; }
public int getFlightNum() { return flightNum; } public String toString() { return Destination + " flight #" + flightNum; } }
The flight class does not “KNOW” it is a part of a list and it only contains information about a specific airline flight
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
This provides a level of abstraction • In the next sections we will examine the SampleLinkedList code to see a LinkedList in action
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
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
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;
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
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…
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