Lists - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

Lists

Description:

We know that a matryoska doll is made of two things: 12. A 'Real-Life' Example ... Just like matryoska dolls! class ListNode. int myNum; ListNode nextNumber; ... – PowerPoint PPT presentation

Number of Views:106
Avg rating:3.0/5.0
Slides: 50
Provided by: paulc1
Category:
Tags: lists

less

Transcript and Presenter's Notes

Title: Lists


1
Lists
  • 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

2
Lists
  • 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

3
Lists
  • 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

4
Lists
  • 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

5
Lists
  • A shopping list
  • Milk
  • Eggs
  • Butter
  • Every list can be thought of as
  • An item
  • Followed by another (possibly empty) list

6
Node
  • 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

7
An 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"
8
Lists
  • 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"
9
Lists
  • 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"
10
A 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.
11
A Real-Life Example
We know that a matryoska doll is made of two
things
? ?
12
A Real-Life Example
We know that a matryoska doll is made of two
things
?
height
13
A Real-Life Example
We know that a matryoska doll is made of two
things
height
another doll
14
A Real-Life Example
class matryoskaDoll int myHeight
matryoskaDoll nextDoll matryoskaDoll(int
nHeight, matryoskaDoll next)
myHeightnHeight nextDollnext
height
another doll
15
A Real-Life Example
new matryoskaDoll( 10 , new
matryoskaDoll (9, new
matryoskaDoll(8, null)))
10
9
Another doll structure or symbol
16
A 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.
17
Using 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
18
Using 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
19
Using 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
20
Practice 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

21
methods that operate on list nodes
  • A profound idea
  • The shape of a method is determined by the data
    it operates on

22
methods 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. . .

23
methods 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

24
methods 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. . .

25
methods 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

26
methods 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

27
methods 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). . .

28
methods 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). . .

29
methods 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). . .

30
Problem 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). . .

31
Problem 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). . .

32
Problem 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?

33
Problem 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

34
Problem 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?

35
Problem 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

36
Problem 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
38
Problem Write a method that counts all the nodes
in the list
  • Well start by using the fill in the blank
    outline

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

40
Problem 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?

41
Problem 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?

42
Problem 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

43
Problem 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!

44
What 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)

45
Problem 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

46
Problem 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

47
Problem 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?

48
Problem 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?

49
Problem 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!
Write a Comment
User Comments (0)
About PowerShow.com