Title: Linked Lists
1Linked Lists
- A Linked List holds a collection of objects
- An Array can also hold a collection of objects
- You could have an array of Strings for example,
or a Linked List of Strings - While in some ways they are similar, there are
some important differences
2Linked Lists vs Arrays
- Think of an Array like the mailboxes in an
apartment buildinga series of identical boxes
identified by a number - Think of Linked Lists like a traina series of
box cars each one hooked up to the next
3Linked Lists vs Arrays
- When you create an array it has a fixed size. You
cannot add or remove "mail boxes" - A Linked List is more flexible. Just like a
train, you can add or remove as many "box cars"
as you want, anywhere in the train
4Linked Lists vs Arrays
- Arrays are fast and easy to accessif you know
the index of the "mail box" you want, you can
immediately access it. - Finding a particular "box car" in a linked list
isn't as easy. You have to board the first "box
car" in the list, and go through them in order to
find the one you are looking for.
5A Linked List of Strings
"one"
"three"
"two"
"end"
"head"
- Each "box car" holds a String, and a handle to
the next "box car" in the list - We call each "box car" a node
6A Linked List of Strings
"one"
"three"
"two"
"end"
"head"
- To make a list from scratch, we'd need a class to
represent each node - class Node
-
- ???
7A Linked List of Strings
"one"
"three"
"two"
"end"
"head"
- Each node has a String and a handle to the next
node - class Node
-
- String sWord
- Node next
8A Linked List of Strings
"one"
"three"
"two"
"end"
"head"
- We'll need a constructor, and good style says the
members should be public or private - class Node
-
- private String sWord
- private Node next
- public Node(String sW, Node n)
-
- sWord sW
- next n
-
9A Linked List of Strings
"one"
"three"
"two"
null
"head"
- The first node in a list is commonly called the
head - To indicate the end of a list, the handle of the
last node is null
10A Linked List of Strings
"one"
"three"
"two"
null
"head"
- To build my first list, I'll start with the end
node and work forwards - Node three new Node("three",null)
11A Linked List of Strings
"one"
"three"
"two"
null
"head"
- To build my first list, I'll start with the end
node and work forwards - Node three new Node("three",null)
- Node two new Node("two",three)
12A Linked List of Strings
"one"
"three"
"two"
null
"head"
- To build my first list, I'll start with the end
node and work forwards - Node three new Node("three",null)
- Node two new Node("two",three)
- Node head new Node("one",two)
13A Linked List of Strings
"one"
"three"
"two"
null
"head"
- Normally, we don't have a "name" for each node,
just the handle in the previous node - Node three new Node("three",null)
- Node two new Node("two",three)
- Node head new Node("one",two)
14A Linked List of Strings
"one"
"three"
"two"
null
"head"
- Normally, we don't have a "name" for each node,
just the handle in the previous node - Node three new Node("three",null)
- Node two new Node("two",three)
- Node head new Node("one",
- new Node("two",three))
15A Linked List of Strings
"one"
"three"
"two"
null
"head"
- Normally, we don't have a "name" for each node,
just the handle in the previous node - Node three new Node("three",null)
- Node two new Node("two",three)
- Node head new Node("one",
- new Node("two",
- new Node("three",null)))
16A Linked List of Strings
"one"
"three"
"two"
null
"head"
temp
- Now let's write some code that displays the
contents of all the nodes to the screen - Node head new Node("one",
- new Node("two",
- new Node("three",null)))
- Node temp head
- while(temp ! null)
-
- System.out.println(temp.sWord)
- temp temp.next
17A Linked List of Strings
"one"
"three"
"two"
null
"head"
temp
- Now let's write some code that displays all the
contents of all the nodes to the screen - Node head new Node("one",
- new Node("two",
- new Node("three",null)))
- Node temp head
- while(temp ! null)
-
- System.out.println(temp.sWord)
- temp temp.next
18A Linked List of Strings
"one"
"three"
"two"
null
"head"
temp
- Now let's write some code that displays all the
contents of all the nodes to the screen - Node head new Node("one",
- new Node("two",
- new Node("three",null)))
- Node temp head
- while(temp ! null)
-
- System.out.println(temp.sWord)
- temp temp.next
19A Linked List of Strings
"one"
"three"
"two"
null
"head"
temp
- The problem with the following code is that sWord
and next are private - Node temp head
- while(temp ! null)
-
- System.out.println(temp.sWord)
- temp temp.next
20A Linked List of Strings
- Node temp head
- while(temp ! null)
-
- System.out.println(temp.getWord())
- temp temp.getNext()
-
- . . .
- class Node
-
- private String sWord
- private Node next
- public Node(String sW, Node n)
-
- sWord sW
- next n
-
- public String getWord()return sWord
- public Node getNext()return next
21Why temp? What would be the output of the
following code?
- Node head new Node("one",
- new Node("two",
- new Node("three",null)))
- while(head ! null)
-
- System.out.println(head.getWord())
- head head.getNext()
-
- while(head ! null)
-
- System.out.println(head.getWord())
- head head.getNext()
22Fill in the blanks so that the following
application produces the indicated output
- public class LinkedListDemo
- public static void main(String args)
- Node head ____________________
- _________________________
- ___________________________
- ____________________________
- Node temp head
- while(________________)
-
- _____________________________
- _____________________________
-
-
-
- / sample output
- apple
- banana
- coconut
23Writing a Linked List class
- The code to create, display, and add and removes
nodes of a linked list is usually encapsulated in
a class - class LinkedList
-
- ???
24Writing a Linked List class
- Every list has a head, and we'll need a
constructor - class LinkedList
-
- private Node head
- public LinkedList()???
25Writing a Linked List class
- A new list should be empty
- class LinkedList
-
- private Node head
- LinkedList()head null
26Writing a Linked List class
head
null
- Since we're starting with an empty list, we'll
need a way to add Nodes. It's easiest to add
nodes at the Front of the list - class LinkedList
-
- private Node head
- LinkedList()head null
- public void addFront(???)
-
- ???
-
27Writing a Linked List class
head
null
"one"
- To add a node at the front, there are two steps
- The new nodes next handle is "hooked up" to the
same place as head - class LinkedList
-
- private Node head
- LinkedList()head null
- public void addFront(???)
-
- ???
-
28Writing a Linked List class
head
null
"one"
- To add a node at the front, there are two steps
- The new nodes next handle is "hooked up" to the
same place as head - Head is "hooked up" to the new node
- class LinkedList
-
- private Node head
- LinkedList()head null
- public void addFront(???)
-
- ???
-
29Writing a Linked List class
head
null
"one"
- To add a node at the front, there are two steps
- The new nodes next handle is "hooked up" to the
same place as head - Head is "hooked up" to the new node
- To get this to work, we'll need a setNext()
method in the Node class - class Node
-
- private String sWord
- private Node next
- public Node(String sW, Node n)
-
- sWord sW
- next n
-
- public String getWord()return sWord
- public Node getNext()return next
- public void setNext(Node n)next n
30Writing a Linked List class
head
null
"one"
- To add a node at the front, there are two steps
- The new nodes next handle is "hooked up" to the
same place as head - class LinkedList
-
- private Node head
- LinkedList()head null
- public void addFront(Node newNode)
-
- newNode.setNext(head)
-
31Writing a Linked List class
head
null
"one"
- To add a node at the front, there are two steps
- The new nodes next handle is "hooked up" to the
same place as head - Head is "hooked up" to the new node
- class LinkedList
-
- private Node head
- LinkedList()head null
- public void addFront(Node newNode)
-
- newNode.setNext(head)
- head newNode
-
32An alternative to passing the node as an
argument, is to just pass the data
- class LinkedList
-
- private Node head
- LinkedList()head null
- public void addFront(String sWord)
-
- Node newNode new Node(sWord,null)
- newNode.setNext(head)
- head newNode
-
-
33We can actually do this with one line
- class LinkedList
-
- private Node head
- LinkedList()head null
- public void addFront(String sWord)
-
- head new Node(sWord,head)
-
-
34Removing a Node
- Before we can remove a node, we have to find it
- We traverse the list, moving from one node to the
next until we find the one we want to delete - Then we rearrange the handles to "skip over" the
target node
35Removing a Node
"one"
"three"
"two"
null
"head"
- Let's say we want to delete node "three"
36Removing a Node
"one"
"three"
"two"
null
"head"
- Let's say we want to delete node "three"
- "two's" next handle should skip over "three"
37Removing a Node
"one"
"three"
"two"
null
"head"
- Let's say we want to delete node "three"
- "two's" next handle should skip over "three"
- So what we are really looking for is the node
whose next handle is "hooked up" to three
38Removing a Node
"one"
"three"
"two"
null
"head"
temp
- Let's say we want to delete node "three"
- "two's" next handle should skip over "three"
- Node temp head
- while(temp.getNext() ! null
- !temp.getNext().getWord().equals("three")
) - temp temp.getNext()
- if(temp.getNext() ! null)
- temp.setNext(temp.getNext().getNext())
39Removing a Node
"one"
"three"
"two"
null
"head"
temp
- Let's say we want to delete node "three"
- "two's" next handle should skip over "three"
- Node temp head
- while(temp.getNext() ! null
- !temp.getNext().getWord().equals("three")
) - temp temp.getNext()
- if(temp.getNext() ! null)
- temp.setNext(temp.getNext().getNext())
40Removing a Node
"one"
"three"
"two"
null
"head"
temp
- Let's say we want to delete node "three"
- "two's" next handle should skip over "three"
- Node temp head
- while(temp.getNext() ! null
- !temp.getNext().getWord().equals("three")
) - temp temp.getNext()
- if(temp.getNext() ! null)
- temp.setNext(temp.getNext().getNext())
41Removing a Node
null
"head"
- What if head is null?
- Node temp head
- while(temp.getNext() ! null
- !temp.getNext().getWord().equals("three")
) - temp temp.getNext()
- if(temp.getNext() ! null)
- temp.setNext(temp.getNext().getNext())
42Removing a Node
null
"head"
- What if head is null? What happens if you put a
null handle in front of a dot? - Node temp head
- while(temp.getNext() ! null
- !temp.getNext().getWord().equals("three")
) - temp temp.getNext()
- if(temp.getNext() ! null)
- temp.setNext(temp.getNext().getNext())
- An empty list is a special case
43Deleting a node at the front
- Deleting a node at the front is also a special
casewe need to change head - Luckily, it's pretty simple
- head head.getNext()
Before
"one"
"three"
"two"
null
"head"
After
"one"
"three"
"two"
null
"head"
44Writing a "remove" method
- When you write a "remove" method, you search the
list for a node whose next handle is "hooked up"
to the value to remove - There are three possibilities (special cases)
- The list is empty
- The node to remove is the first node in the list
- If neither case 1 or 2, you need to traverse the
list to see if the node is in the list
45Writing a "remove" method
- public void remove(String sTarget)
-
- //check if list is empty
- if(head null)
- ??
- //remove first node?
- else if(head.getWord().equals(sTarget))
- ??
- else
-
- //traverse the list
-
46Practice Quiz Question What is the output?
- public class LinkedListDemo
- public static void main(String args)
- LinkedList list new LinkedList()
- list.addFront(new Node("cat",null))
- list.addFront(new Node("dog",null))
- list.addFront(new Node("mouse",null))
- list.addFront(new Node("horse",null))
- list.display()
- list.mystery1()
- list.display()
- list.mystery2()
- list.display()
-
-
- class LinkedList
-
- private Node head
- public LinkedList()head null
- public void addFront(Node newNode)
47Real Life use of Linked Lists
- A computer programmer at Northrop "One of the
biggest lists was when our aircraft entered an
area where there were threats or
targets-of-opportunity. As threats were
encountered, eliminated, and passed-by, they were
added, and removed from the list."
48Real Life use of Linked Lists
- FAT - the File Allocation Table in MS-DOS used a
linked-list concept to keep track of which disk
sectors are occupied by a file. - An OS can track memory usage in "blocks" or
"pages" that are either allocated to a task and
linked together in a linked list, or are
available and appear in a linked-list containing
free blocks. - An UNDO stack in a graphics program is likely to
use something akin to a linked-list to keep track
of a long chain of undo-able events.
49Recursive List methods
- You've already seen how to traverse a list with a
loop - class LinkedList
-
- private Node head
- LinkedList()head null
- public void display()
-
- Node temp head
- while(temp ! null)
-
- System.out.println(temp.getWord())
- temp temp.getNext()
-
-
50Recursive List methods
- We can also traverse the list recursively using a
helper method - class LinkedList
-
- private Node head
- LinkedList()head null
- public void display()
-
- recurseDisplay(head)
-
- void recurseDisplay(Node node)
-
- if(node null)
- System.out.println("The End")
- else
-
- System.out.println(node.getWord())
- recurseDisplay(node.getNext())
-
-
51Nodes and Recursion
- If you look at it a certain way, Nodes are
recursive. - class Node
-
- private Object data
- private Node next
- // lots more Java
52Nodes and Recursion
- If you look at it a certain way, Nodes are
recursive. - class Node
-
- private Object data
- private Node next
- // lots more Java
- so recursive methods are well suited for
operating on Nodes
53A "fill in the blank" outline for a recursive
list method
- void someRecursiveMethod(Node node)
-
- if(node null)
- //______ end of list _________
- else
-
- //____not the end of list_____
- someRecursiveMethod(node.getNext())
-
54A recursive countNodes method
- int countNodes()
-
- return recursCount(head)
-
- int recursCount(Node node)
-
- if(node null)
- ?????
- else
-
- ?????
- recursCount(node.getNext())
-
-
55A recursive countNodes method
- int countNodes()
-
- return recursCount(head)
-
- int recursCount(Node node)
-
- if(node null)
- return 0
- else
-
- ?????
- recursCount(node.getNext())
-
-
56A recursive countNodes method
- int countNodes()
-
- return recursCount(head)
-
- int recursCount(Node node)
-
- if(node null)
- return 0
- else
-
- return 1
- recursCount(node.getNext())
-
-
57A recursive remove method
- public void remove(String sTarget)
- if(headnull)
- return
- else if(head.getWord().equals(sTarget))
- headhead.getNext()
- else
- recursiveRemove(head, sTarget)
-
-
- public void recursiveRemove(Node node, String
sTarget) -
- if(node null)
- ????
- else
- ????
- recursiveRemove(node.getNext(),sTarget)
-
-
58A recursive remove method
- public void remove(String sTarget)
- if(headnull)
- return
- else if(head.getWord().equals(sTarget))
- headhead.getNext()
- else
- recursiveRemove(head, sTarget)
-
-
- public void recursiveRemove(Node node, String
sTarget) -
- if(node null)
- return
- else
- ????
- recursiveRemove(node.getNext(),sTarget)
-
-
59A recursive remove method
- public void remove(String sTarget)
- if(headnull)
- return
- else if(head.getWord().equals(sTarget))
- headhead.getNext()
- else
- recursiveRemove(head, sTarget)
-
-
- public void recursiveRemove(Node node, String
sTarget) -
- if(node null)
- return
- else
- if(node.getNext().getWord().equals(sTarget))
- ???
- recursiveRemove(node.getNext(),sTarget)
-
-
60A recursive remove method
- public void remove(String sTarget)
- if(headnull)
- return
- else if(head.getWord().equals(sTarget))
- headhead.getNext()
- else
- recursiveRemove(head, sTarget)
-
-
- public void recursiveRemove(Node node, String
sTarget) -
- if(node null)
- return
- else
- if(node.getNext().getWord()
-
.equals(sTarget)) - ???
- recursiveRemove(node.getNext(),sTarget)
-
61A recursive remove method
- public void remove(String sTarget)
- if(headnull)
- return
- else if(head.getWord().equals(sTarget))
- headhead.getNext()
- else
- recursiveRemove(head, sTarget)
-
-
- public void recursiveRemove(Node node, String
sTarget) -
- if(node null if(node.getNext()null)
- return
- else if(node.getNext().getWord().equals(sTarget))
- ???
- recursiveRemove(node.getNext(),sTarget)
-
-
62A recursive remove method
- public void remove(String sTarget)
- if(headnull)
- return
- else if(head.getWord().equals(sTarget))
- headhead.getNext()
- else
- recursiveRemove(head, sTarget)
-
-
- public void recursiveRemove(Node node, String
sTarget) -
- if(node null if(node.getNext()null)
- return
- else if(node.getNext().getWord().equals(sTarget))
- node.setNext(node.getNext().getNext())
- else
- recursiveRemove(node.getNext(),sTarget)
-
63Practice Quiz Question What is the output?
- public class LinkedListDemo
- public static void main(String args)
- LinkedList list new LinkedList()
- list.addFront(new Node("Blaziken",null))
- list.addFront(new Node("Pichu",null))
- list.addFront(new Node("Meowth",null))
- list.addFront(new Node("Totodile",null))
- list.display()
- list.mystery1()
- list.mystery2()
- list.mystery3()
- list.mystery4()
- list.display()
-
-
- class LinkedList
- private Node head
- public LinkedList()head null
- public void addFront(Node newNode)
64A "simple" standard Java file input application
- import java.io.
- public class FileReaderTest
-
- public static void main(String args)
-
- File inFilenew File("inFile.txt")
- try
-
- FileReader frnew FileReader(inFile)
- BufferedReader in new BufferedReader(fr)
- String sLine
- do
-
- sLinein.readLine()//read one line
- if(sLine!null)
- System.out.println(sLine)
- while(sLine!null)
- in.close()
- catch(IOException ioe)
65The same program using the EasyReader class
- public class EasyReaderDemo
-
- public static void main(String args)
-
- String sFileName "inFile.txt"
- EasyReader inFile new EasyReader(sFileNam
e) - if (inFile.bad())
-
- System.err.println("Can't open "
sFileName) - System.exit(1)
-
- String sLine
- do
-
- sLine inFile.readLine())
- if(sLine ! null)
- System.out.println(sLine)
- while (sLine ! null)
- inFile.close()
66The same program using the EasyReader class
- public class EasyReaderDemo
-
- public static void main(String args)
-
- //Setup a new EasyReader
- String sFileName "inFile.txt"
- EasyReader inFile
- new EasyReader(sFileName)
- if (inFile.bad())
-
- System.err.println("Can't open "
sFileName) - System.exit(1)
-
- String sLine
- do
-
- sLine inFile.readLine())
- if(sLine ! null)
- System.out.println(sLine)
67The same program using the EasyReader class
- public class EasyReaderDemo
-
- public static void main(String args)
-
- String sFileName "inFile.txt"
- EasyReader inFile new EasyReader(sFileNam
e) - //Check to see if the file exists
- if (inFile.bad())
-
- System.err.println
- ("Can't open " sFileName)
- System.exit(1)
-
- String sLine
- do
-
- sLine inFile.readLine())
- if(sLine ! null)
- System.out.println(sLine)
68The same program using the EasyReader class
- public class EasyReaderDemo
-
- public static void main(String args)
-
- String sFileName "inFile.txt"
- EasyReader inFile new EasyReader(sFileNam
e) - //Check to see if the file exists
- if (inFile.bad())
-
- System.err.println
- ("Can't open " sFileName)
- System.exit(1)
-
- //Read data one line at a time
- String sLine
- do
-
- sLine inFile.readLine())
- if(sLine ! null)
69The same program using the EasyReader class
- public class EasyReaderDemo
-
- public static void main(String args)
-
- String sFileName "inFile.txt"
- EasyReader inFile new EasyReader(sFileNam
e) - //Check to see if the file exists
- if (inFile.bad())
-
- System.err.println
- ("Can't open " sFileName)
- System.exit(1)
-
- //Read data one line at a time
- String sLine
- do
-
- sLine inFile.readLine())
- if(sLine ! null)
70Assignment 2-3AB file20.txt
- The first number is the number of items, followed
by number pairs - The first in the pair is the "key", the second
"inventory" - 20
- 196 60
- 18618 64
- 2370 65
- 18410 56
- 18465 27
- 19967 45
- 17911 96
- 184 14
- 18871 69
- 14088 92
- 18061 3
- 206 31
- 13066 8
- 12705 14
- 15917 51
- 15814 60
71Assignment 2-3AB
- First we read in the number of items
- Then we insert the items in key order one at a
time - int nNumItems inFile.readInt()
- int nKey, nInventory
- for(int nCount 0 nCount lt nNumItems nCount)
-
- nKey inFile.readInt()
- nInventory inFile.readInt()
- System.out.println("Line " nCount
- " Key " nKey
- " Inventory " nInventory)
- theList.insertInOrder(nKey,nInventory)
- theList.startDisplay()
-
72Assignment 2-3AB
head
null
- 20
- 196 60
- 18618 64
- 2370 65
- 18410 56
- 18465 27
- 19967 45
- 17911 96
- 184 14
- 18871 69
- 14088 92
- 18061 3
- 206 31
- 13066 8
- 12705 14
- 15917 51
- 15814 60
- 15320 82
- 8303 90
73Assignment 2-3AB
head
196
60
- 20
- 196 60
- 18618 64
- 2370 65
- 18410 56
- 18465 27
- 19967 45
- 17911 96
- 184 14
- 18871 69
- 14088 92
- 18061 3
- 206 31
- 13066 8
- 12705 14
- 15917 51
- 15814 60
- 15320 82
- 8303 90
74Assignment 2-3AB
head
196
18618
60
64
- 20
- 196 60
- 18618 64
- 2370 65
- 18410 56
- 18465 27
- 19967 45
- 17911 96
- 184 14
- 18871 69
- 14088 92
- 18061 3
- 206 31
- 13066 8
- 12705 14
- 15917 51
- 15814 60
- 15320 82
- 8303 90
75Assignment 2-3AB
head
196
2370
18618
60
65
64
- 20
- 196 60
- 18618 64
- 2370 65
- 18410 56
- 18465 27
- 19967 45
- 17911 96
- 184 14
- 18871 69
- 14088 92
- 18061 3
- 206 31
- 13066 8
- 12705 14
- 15917 51
- 15814 60
- 15320 82
- 8303 90
76Assignment 2-3AB
head
196
2370
18410
18618
60
65
56
64
- 20
- 196 60
- 18618 64
- 2370 65
- 18410 56
- 18465 27
- 19967 45
- 17911 96
- 184 14
- 18871 69
- 14088 92
- 18061 3
- 206 31
- 13066 8
- 12705 14
- 15917 51
- 15814 60
- 15320 82
- 8303 90
77- class List
-
- private ListNode head
- // lots more java
- void insertInOrder(int nKey, int nInv)
-
- ??
-
78- class List
-
- private ListNode head
- // lots more java
- void insertInOrder(int nKey, int nInv)
-
- if(head null)
- ??
-
79- class List
-
- private ListNode head
- // lots more java
- void insertInOrder(int nKey, int nInv)
-
- if(head null)
- head new ListNode(nKey,nInv,null)
-
80- class List
-
- private ListNode head
- // lots more java
- void insertInOrder(int nKey, int nInv)
-
- if(head null)
- head new ListNode(nKey,nInv,null)
- else if(head.getKey() gt nKey)
- ??
-
81- class List
-
- private ListNode head
- // lots more java
- void insertInOrder(int nKey, int nInv)
-
- if(head null)
- head new ListNode(nKey,nInv,null)
- else if(head.getKey() gt nKey)
- head ??
-
82- class List
-
- private ListNode head
- // lots more java
- void insertInOrder(int nKey, int nInv)
-
- if(head null)
- head new ListNode(nKey,nInv,null)
- else if(head.getKey() gt nKey)
- head ??
- else
- ??
-
83- class List
-
- private ListNode head
- // lots more java
- void insertInOrder(int nKey, int nInv)
-
- if(head null)
- head new ListNode(nKey,nInv,null)
- else if(head.getKey() gt nKey)
- head ??
- else
-
- ListNode temp head
- //traverse the list and look for right
- //"spot" to insert new node
-