Nonrecursive list methods - PowerPoint PPT Presentation

1 / 70
About This Presentation
Title:

Nonrecursive list methods

Description:

Traversing a list. Traversing means 'visiting' each node in order. You can traverse recursively, or with a loop, but never both ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 71
Provided by: paulc1
Category:

less

Transcript and Presenter's Notes

Title: Nonrecursive list methods


1
Non-recursive list methods
  • There's a different way to display all the nodes
    in a list like this
  • class ListNode
  • int myNum
  • ListNode nextNumber
  • ListNode(int nNum, ListNode next)
  • myNum nNum nextNumber next
  • ListNode head new ListNode(6,
  • new ListNode(4,
  • new ListNode(2,null)))

2
Non-recursive list methods
  • Instead of recursion, we can use a loop
  • class ListNode
  • int myNum
  • ListNode nextNumber
  • ListNode(int nNum, ListNode next)
  • myNum nNum nextNumber next
  • ListNode head new ListNode(6,
  • new ListNode(4,
  • new ListNode(2,null)))
  • ListNode temp head //IMPORTANT!
  • while(temp ! null)
  • System.out.println(temp.myNum)
  • temp temp.nextNumber

3
Non-recursive list methods
  • Here it is again with a for loop
  • class ListNode
  • int myNum
  • ListNode nextNumber
  • ListNode(int nNum, ListNode next)
  • myNum nNum nextNumber next
  • ListNode head new ListNode(6,
  • new ListNode(4,
  • new ListNode(2,null)))
  • for(ListNode temp head
  • temp ! null
  • temp temp.nextNumber)
  • System.out.println(temp.myNum)

4
Non-recursive list methods
  • Notice both loops have the same parts
  • ListNode temp head
  • while(temp ! null)
  • System.out.println(temp.myNum)
  • temp temp.nextNumber
  • for(ListNode temp head
  • temp ! null
  • temp temp.nextNumber)
  • System.out.println(temp.myNum)

5
Traversing a list
  • Traversing means "visiting" each node in order
  • You can traverse recursively, or with a loop, but
    never both
  • Don't mess up the head, it will turn the list to
    garbage
  • ListNode temp head //IMPORTANT!
  • Make a copy of the head instead
  • while(temp ! null)
  • System.out.println(temp.myNum)
  • temp temp.nextNumber

6
Problem write a non-recursive method that returns
the sum of all the nodes
  • int findSum(ListNode head)
  • ListNode temp head
  • int nSum 0
  • while(temp ! null)
  • nSum temp.myNum
  • temp temp.nextNumber
  • return nSum

7
Inserting list nodes
  • We can add nodes at the front, at the end or
    somewhere in the middle.
  • Lets start with the easiest example adding a
    node to the front of the list
  • class ListNode
  • int myNum
  • ListNode nextNumber
  • ListNode(int nNum, ListNode next)
  • myNum nNum nextNumber next

8
Inserting a node at the front
  • Let's add a node with the value 4 to the front of
    this list
  • ListNode head
  • head new ListNode(6,
  • new ListNode(2,
  • new ListNode(3, null)))

9
Inserting a node at the front
Before
head
6
2
3
After
4
head
6
2
3
3
10
Inserting a node at the front
  • You can see that head will no longer point to 6
  • It will point to a new ListNode with value 4
  • The new ListNode's nextNumber will point to what
    used to be the head

Before
head
6
2
3
After
4
head
6
2
3
11
Inserting a node at the front
  • adding a node with the value 4 to the front of
    this list
  • ListNode head
  • head new ListNode(6,
  • new ListNode(2,
  • new ListNode(3, null)))
  • head new ListNode(4,head)
  • That's all there is to it!

12
Inserting a node at the end
  • This time, let's add a node with the value 4 to
    the end of this list
  • ListNode head
  • head new ListNode(6,
  • new ListNode(2,
  • new ListNode(3, null)))

13
Inserting a node at the end
Before
head
6
2
3
After
6
head
2
3
3
4
14
Inserting a node at the end
  • You can see that head is unchanged
  • The next number of the next number of the next
    number of head will point to a new ListNode

Before
head
6
2
3
After
6
head
2
3
4
15
Inserting a node at the end
  • ListNode head
  • head new ListNode(6,
  • new ListNode(2,
  • new ListNode(3, null)))
  • head.nextNumber.nextNumber.nextNumber
  • new ListNode(4,null)
  • This works, but it's not exactly ideal

Before
head
6
2
3
After
6
head
2
3
4
16
Inserting a node at the end
  • A better idea is to traverse the list until we
    get to the last nextNumber, the one that is null
  • Then "hook" that last nextNumber up to a new
    ListNode
  • ListNode head
  • head new ListNode(6,
  • new ListNode(2,
  • new ListNode(3, null)))
  • ListNode temp head
  • while(temp.nextNumber ! null)
  • temp temp.nextNumber
  • temp.nextNumber new ListNode(4,null)

17
What is the output of this program?
  • ListNode head
  • head new ListNode(-2,null)
  • head new ListNode(-3,head)
  • head.nextNumber
  • new ListNode(7,head.nextNumber)
  • head.nextNumber.nextNumber.nextNumber
  • new ListNode(5,null)
  • displayNodes(head)

18
Inserting a node in the middle of the list
  • Now, let's add a node with the value 4 before the
    node with value 3
  • ListNode head
  • head new ListNode(6,
  • new ListNode(2,
  • new ListNode(3, null)))

19
Inserting a node in the middle of the list
Before
head
6
2
3
After
6
head
2
4
3
3
20
Inserting a node in the middle of the list
  • Again, you can see that head is unchanged
  • The next number of the next number of head will
    point to a new ListNode
  • The nextNumber of the new ListNode will point to 3

head
Before
6
2
3
After
6
head
2
4
3
21
Inserting a node in the middle of the list
  • head.nextNumber.nextNumber
  • new ListNode(4,
  • head.nextNumber.nextNumber)
  • This works, but it's not ideal

head
Before
6
2
3
After
6
head
2
4
3
22
Inserting a node in the middle of the list
  • ListNode head
  • head new ListNode(6,
  • new ListNode(2,
  • new ListNode(3,null)))
  • //traverse the list until we find the
  • //nextNumber that points to 3
  • ListNode temp head
  • while(temp.nextNumber ! null
  • temp.nextNumber.myNum !3)
  • temp temp.nextNumber
  • temp.nextNumber new ListNode(4,
  • temp.nextNumber)

23
Deleting a node
  • Deleting a node means "rerouting" a pointer past
    the node to be deleted.
  • Let's say we want to delete the node with value 2

head
Before
6
2
3
After
6
head
2
3
24
Deleting a node
  • head.nextNumber head.nextNumber.nextNumber
  • This works, but it's better to traverse the list

head
Before
6
2
3
After
6
head
2
3
25
Deleting a node
  • ListNode temp head
  • while(temp.nextNumber ! null
  • temp.nextNumber.myNum ! 2)
  • temp temp.nextNumber
  • temp.nextNumber temp.nextNumber.nextNumber

head
Before
6
2
3
After
6
head
2
3
26
Deleting a node at the front
  • Deleting a node at the front is a special casewe
    need to change head
  • Luckily, it's pretty simple
  • head head.nextNumber

head
Before
6
2
3
After
6
head
2
3
27
Writing a Linked List class
  • A Linked List class needs
  • A head variable
  • A constructor
  • An insert method
  • A delete method
  • A method that searches the list for a particular
    value

28
The head variable
  • class List
  • private ListNode head

29
The constructor
  • class List
  • private ListNode head
  • public List()
  • ???

30
The constructor
  • class List
  • private ListNode head
  • public List()
  • head null

31
A method that inserts a new node at the front of
the list
  • class List
  • private ListNode head
  • public ListNode()
  • head null
  • public void insertAtFront(int nNum)
  • ???

32
A method that inserts a new node at the front of
the list
  • class List
  • private ListNode head
  • public ListNode()
  • head null
  • public void insertAtFront(int nNum)
  • head new ListNode(nNum,head)

33
A method that displays all the nodes in the list
  • class List
  • private ListNode head
  • public ListNode()
  • head null
  • public void insertAtFront(int nNum)
  • head new ListNode(nNum,head)
  • void displayNodes (ListNode node)
  • if (node null)
  • System.out.println("End")
  • else
  • System.out.println(node.myNum)
  • displayNodes(node.nextNumber)

34
Using the List class what's wrong with this code?
  • List theList new List()
  • theList.insertAtFront(1)
  • theList.insertAtFront(2)
  • theList.insertAtFront(3)
  • theList.displayNodes(head)

35
head is a private member of the List class. We
don't have access to head outside of the List
class
  • List theList new List()
  • theList.insertAtFront(1)
  • theList.insertAtFront(2)
  • theList.insertAtFront(3)
  • theList.displayNodes(head)

36
A "helper" method that "gets the display started"
  • class List
  • private ListNode head
  • public ListNode()
  • head null
  • public void insertAtFront(int nNum)
  • head new ListNode(nNum,head)
  • void display()displayNodes(head)
  • void displayNodes (ListNode node)
  • if (node null)
  • System.out.println("End")
  • else

37
Using the List class Now it works correctly
  • List theList new List()
  • theList.insertAtFront(1)
  • theList.insertAtFront(2)
  • theList.insertAtFront(3)
  • theList.display()
  • Note recursive member methods often use helpers
    to "get them started"

38
Writing a "remove" method
  • When you write a "remove" method, you search the
    list for a particular value to remove
  • There are three possibilities
  • The list is empty
  • The node to remove is the first node in the list
  • You need to traverse the list to see if the node
    is in the list

39
Writing a "remove" method
  • class List
  • private ListNode head
  • //lots more Java
  • public void remove(int nNum)
  • ??

40
Writing a "remove" method
  • public void remove(int nNum)
  • //check if list is empty

41
Writing a "remove" method
  • public void remove(int nNum)
  • //check if list is empty
  • if(head null)
  • ??

42
Writing a "remove" method
  • public void remove(int nNum)
  • //check if list is empty
  • if(head null)
  • ??
  • //remove first node?

43
Writing a "remove" method
  • public void remove(int nNum)
  • //check if list is empty
  • if(head null)
  • ??
  • //remove first node?
  • else if(head.myNum nNum)
  • ??

44
Writing a "remove" method
  • public void remove(int nNum)
  • //check if list is empty
  • if(head null)
  • ??
  • //remove first node?
  • else if(head.myNum nNum)
  • ??
  • else
  • //traverse the list

45
Applications
  • Assignments 13.5 and 14 are applications
  • They have a main method
  • //A simple application
  • public class SimpleApp
  • public static void main(String args)
  • System.out.println("hello")

46
file input and output
  • Applets have security restrictions they are not
    allowed access to your hard drive
  • If we want a program to read data from a file (or
    save data to a file), we need to write an
    application
  • The standard file input and output classes in
    java are Ugly (and are not on the AP)
  • A simpler alternative is the EasyReader class

47
A "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)

48
The 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()

49
The 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)

50
The 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)

51
The 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)

52
The 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)

53
Head recursion vs. Tail recursion
  • If the last line of a method is a recursive call,
    the method is said to be tail recursive
  • void displayNodes (ListNode node)
  • if (node null)
  • System.out.println("End")
  • else
  • System.out.println(node.myNum)
  • displayNodes(node.nextNumber)

54
Head recursion vs. Tail recursion
  • If the last line of a method is a recursive call,
    the method is said to be tail recursive
  • void displayNodes (ListNode node)
  • if (node null)
  • System.out.println("End")
  • else
  • System.out.println(node.myNum)
  • displayNodes(node.nextNumber)

55
Head recursion vs. Tail recursion
  • If we change the order, the method is called head
    recursive
  • void displayNodes (ListNode node)
  • if (node null)
  • System.out.println("End")
  • else
  • displayNodes(node.nextNumber)
  • System.out.println(node.myNum)

56
Head recursion vs. Tail recursion
  • In assignment 14, experiment with the display
    method using head and then tail recursion and see
    what happens
  • void displayNodes (ListNode node)
  • if (node null)
  • System.out.println("End")
  • else
  • displayNodes(node.nextNumber)
  • System.out.println(node.myNum)

57
Assignment 14 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

58
Assignment 14
  • 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()/

59
Assignment 14
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

60
Assignment 14
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

61
Assignment 14
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

62
Assignment 14
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

63
Assignment 14
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

64
  • class List
  • private ListNode head
  • // lots more java
  • void insertInOrder(int nKey, int nInv)
  • ??

65
  • class List
  • private ListNode head
  • // lots more java
  • void insertInOrder(int nKey, int nInv)
  • if(head null)
  • ??

66
  • class List
  • private ListNode head
  • // lots more java
  • void insertInOrder(int nKey, int nInv)
  • if(head null)
  • head new ListNode(nKey,nInv,null)

67
  • 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)
  • ??

68
  • 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 ??

69
  • 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
  • ??

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