Title: Lists
1Lists
- In real life we make lists all the time
- A shopping list
- Milk
- Eggs
- Butter
- The nice thing about a list is that it has no
fixed size - I can add as many items to the list as I want,
and I can easily remove them as well
2Lists
- Computer Scientists look at Lists a little
differently than most people - A shopping list
- Milk
- Eggs
- Butter
- If I scratch out Milk, I still have a list
3Lists
- Computer Scientists look at Lists a little
differently than most people - A shopping list
- Milk
- Eggs
- Butter
- I still have a list if I scratch the Eggs out too
4Lists
- Computer Scientists look at Lists a little
differently than most people - A shopping list
- Milk
- Eggs
- Butter
- A list with no items is still a list
5Lists
- A shopping list
- Milk
- Eggs
- Butter
- Every list can be thought of as
- An item
- Followed by another (possibly empty) list
6Node
- In computer science, we build lists with "nodes"
- It has at least two pieces of data, the item in
the list and the next (possibly empty) node in
the list - class shopListNode
-
- String myItem
- shopListNode next
- shopListNode(String sItem,
- shopListNode nextNode)
- myItem sItem next nextNode
-
- It will be convenient to have a constructor as
well
7An empty list
- When we start a list, we always start from an
empty list - In Java we use null to represent an empty list
- From here, we add items by making new nodes
- new shopListNode(Butter,null)
- You can imagine the node this way
null
"Butter"
8Lists
- Butter is at the end of the list, because its
followed by an empty list - To add eggs to the list, we make a new list
that has eggs and is followed by the butter
list - new shopListNode(Eggs,
- new shopListNode(Butter,null))
null
"Butter"
"Eggs"
9Lists
- We can add items with no limit by repeating the
process - new shopListNode(Milk,
- new shopListNode(Eggs,
- new shopListNode(Butter,null)))
"Milk"
null
"Butter"
"Eggs"
10A Real Life Example
We of course are all avid collectors of Russian
Matryoska dolls--you know, those dolls that have
dolls in them, and so on, and so on?
Each doll has its own height, and might contain
another doll. But at some point, the chain comes
to an end.
11A Real-Life Example
We know that a matryoska doll is made of two
things
? ?
12A Real-Life Example
We know that a matryoska doll is made of two
things
?
height
13A Real-Life Example
We know that a matryoska doll is made of two
things
height
another doll
14A Real-Life Example
class matryoskaDoll int myHeight
matryoskaDoll nextDoll matryoskaDoll(int
nHeight, matryoskaDoll next)
myHeightnHeight nextDollnext
height
another doll
15A Real-Life Example
new matryoskaDoll( 10 , new
matryoskaDoll (9, new
matryoskaDoll(8, null)))
10
9
Another doll structure or symbol
16A Real-Life Example
new matryoskaDoll( 10 , new
matryoskaDoll (9, new
matryoskaDoll(8, null)))
10
9
8
null
We can visualize this another way, if that
helps. Looking at it this way, it may be easier
to see why they are sometimes called Linked
Lists.
17Using a variable to represent a list
matryoskaDoll head head new matryoskaDoll( 10
, new matryoskaDoll (9,
new matryoskaDoll(8, null)))
head
10
9
Another doll structure or symbol
18Using a variable to represent a list
System.out.println(head.myHeight) System.out.prin
tln(head.nextDoll.myHeight) System.out.println(he
ad.nextDoll.nextDoll.myHeight)
head
10
9
Another doll structure or symbol
19Using a variable to represent a list
System.out.println(head.myHeight) System.out.prin
tln(head.nextDoll.myHeight) System.out.println(he
ad.nextDoll.nextDoll.myHeight)
head
10
9
8
null
20Practice quiz question
- Using the following declaration for shopListNode,
create a list with the following items - Cheese
- Bread
- Peanut Butter
- Orange Juice
- Create a shopListNode variable called toBuy that
refers to this list - class shopListNode
-
- String myItem
- shopListNode next
- shopListNode(String sItem,
- shopListNode nextNode)
- myItem sItem next nextNode
-
21methods that operate on list nodes
- A profound idea
- The shape of a method is determined by the data
it operates on
22methods that operate on list nodes
- What would a method that takes a ListNode
argument look like? - . . . . someMethod (ListNode node)
-
- . . . . . . .
-
- We must start by looking at the data. . .
23methods that operate on list nodes
- class ListNode
-
- int myNum
- ListNode nextNumber
- ListNode(int nNum, ListNode next)
- myNum nNum nextNumber next
-
- A ListNode is either
- null (empty)
- or it has an int myNum and a ListNode nextNumber
24methods that operate on list nodes
- So, a method that takes a ListNode argument will
have two pieces of data to work with if the node
is not empty - . . . . someMethod (ListNode node)
-
- if (node null)
- . . .
- else
-
- . . .node.myNum. . .
- . . .node.nextNumber. . .
-
-
25methods that operate on list nodes
- Every ListNode is recursive, that is, it has
another ListNode inside it. - Just like matryoska dolls!
- class ListNode
-
- int myNum
- ListNode nextNumber
- ListNode(int nNum, ListNode next)
- myNum nNum nextNumber next
-
26methods that operate on list nodes
- Every ListNode is recursive, that is, it has
another ListNode inside it. - Just like matryoska dolls!
- class ListNode
-
- int myNum
- ListNode nextNumber
- ListNode(int nNum, ListNode next)
- myNum nNum nextNumber next
-
27methods that operate on list nodes
- So, every ListNode method should be recursive as
well - . . . . someMethod (ListNode node)
-
- if (node null)
- . . .
- else
-
- . . .node.myNum. . .
- . . .someMethod(node.nextNumber). . .
-
-
28methods that operate on list nodes
- So, every ListNode method should be recursive as
well - . . . . someMethod (ListNode node)
-
- if (node null)
- . . .
- else
-
- . . .node.myNum. . .
- . . .someMethod(node.nextNumber). . .
-
-
29methods that operate on list nodes
- We now have a fill-in-the-blank outline that we
can use for all methods with a ListNode argument - . . . . someMethod (ListNode node)
-
- if (node null)
- . . .
- else
-
- . . .node.myNum. . .
- . . .someMethod(node.nextNumber). . .
-
-
30Problem Write a method that displays all the
contents of a list
- Well start by using the fill in the blank
outline - . . . . someMethod (ListNode node)
-
- if (node null)
- . . .
- else
-
- . . .node.myNum. . .
- . . .someMethod(node.nextNumber). . .
-
-
31Problem Write a method that displays all the
contents of a list
- Then we'll give the method an appropriate name
and return type - void displayNodes (ListNode node)
-
- if (node null)
- . . .
- else
-
- . . .node.myNum. . .
- . . .displayNodes(node.nextNumber). . .
-
-
32Problem Write a method that displays all the
contents of a list
- void displayNodes (ListNode node)
-
- if (node null)
- . . .
- else
-
- . . .node.myNum. . .
- . . .displayNodes(node.nextNumber). . .
-
-
- What do we want to do if the list is empty?
33Problem Write a method that displays all the
contents of a list
- void displayNodes (ListNode node)
-
- if (node null)
- System.out.println("End")
- else
-
- . . .node.myNum. . .
- . . .displayNodes(node.nextNumber). . .
-
-
- Display an appropriate message
34Problem Write a method that displays all the
contents of a list
- void displayNodes (ListNode node)
-
- if (node null)
- System.out.println("End")
- else
-
- . . .node.myNum. . .
- . . .displayNodes(node.nextNumber). . .
-
-
- What do we want to do with myNum?
35Problem Write a method that displays all the
contents of a list
- void displayNodes (ListNode node)
-
- if (node null)
- System.out.println("End")
- else
-
- System.out.println(node.myNum)
- . . .displayNodes(node.nextNumber). . .
-
-
- Display it
36Problem Write a method that displays all the
contents of a list
- void displayNodes (ListNode node)
-
- if (node null)
- System.out.println("End")
- else
-
- System.out.println(node.myNum)
- displayNodes(node.nextNumber)
-
-
- That's it! We're done!
37- ListNode head new ListNode(4,
- new ListNode(13,null))
- displayNodes(head)
- / Sample output
- 4
- 13
- End
- Exit code 0
- No Errors /
- void displayNodes (ListNode node)
-
- if (node null)
- System.out.println("End")
- else
-
- System.out.println(node.myNum)
- displayNodes(node.nextNumber)
head
4
13
38Problem Write a method that counts all the nodes
in the list
- Well start by using the fill in the blank
outline
39Problem Write a method that counts all the nodes
in the list
- . . . . someMethod (ListNode node)
-
- if (node null)
- . . .
- else
-
- . . .node.myNum. . .
- . . .someMethod(node.nextNumber). . .
-
-
- Now, choose an appropriate name and return type
40Problem Write a method that counts all the nodes
in the list
- int count (ListNode node)
-
- if (node null)
- . . .
- else
-
- . . .node.myNum. . .
- . . .count(node.nextNumber). . .
-
-
- What's the count if the list is empty?
41Problem Write a method that counts all the nodes
in the list
- int count (ListNode node)
-
- if (node null)
- return 0
- else
-
- . . .node.myNum. . .
- . . .count(node.nextNumber). . .
-
-
- Now, how do we count myNum?
42Problem Write a method that counts all the nodes
in the list
- int count (ListNode node)
-
- if (node null)
- return 0
- else
-
- return 1
- . . .count(node.nextNumber). . .
-
-
- Now, add the count from the rest of the list
43Problem Write a method that counts all the nodes
in the list
- int count (ListNode node)
-
- if (node null)
- return 0
- else
-
- return 1
- count(node.nextNumber)
-
-
- That's it! We're done!
44What is the output of this program?
- public class ListNodeDemo extends Applet
-
- public void paint(Graphics g)
-
- ListNode head new ListNode(3,
- new ListNode(-2,
- new ListNode(17, null)))
- displayNodes(head)
- System.out.println("Count is "
count(head)) -
- void displayNodes (ListNode node)
-
- if (node null)
- System.out.println("End")
- else
-
- System.out.println(node.myNum)
- displayNodes(node.nextNumber)
-
45Problem Write a method that returns true if all
the elements are positive, false otherwise
- . . . . someMethod (ListNode node)
-
- if (node null)
- . . .
- else
-
- . . .node.myNum. . .
- . . .someMethod(node.nextNumber). . .
-
-
- Well start by using the fill in the blank
outline
46Problem Write a method that returns true if all
the elements are positive, false otherwise
- boolean isPositive (ListNode node)
-
- if (node null)
- . . .
- else
-
- . . .node.myNum. . .
- . . .isPositive(node.nextNumber). . .
-
-
- Now, choose an appropriate name and return type
47Problem Write a method that returns true if all
the elements are positive, false otherwise
- boolean isPositive (ListNode node)
-
- if (node null)
- . . .
- else
-
- . . .node.myNum. . .
- . . .isPositive(node.nextNumber). . .
-
-
- What should we return if the list is empty?
48Problem Write a method that returns true if all
the elements are positive, false otherwise
- boolean isPositive (ListNode node)
-
- if (node null)
- return true
- else
-
- . . .node.myNum. . .
- . . .isPositive(node.nextNumber). . .
-
-
- What should we return if the list is NOT empty?
49Problem Write a method that returns true if all
the elements are positive, false otherwise
- boolean isPositive (ListNode node)
-
- if (node null)
- return true
- else
-
- return (node.myNum gt 0)
- isPositive(node.nextNumber)
-
-
- That's it! We're done!