Chapter 33 Slides - PowerPoint PPT Presentation

1 / 72
About This Presentation
Title:

Chapter 33 Slides

Description:

Chapter 33 Slides. Exposure Java. Linked List I. Chapter XXXIII Topics. Chapter ... Java's ArrayList class allows dynamic resizing during program execution. ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 73
Provided by: johns379
Category:
Tags: chapter | javas | slides

less

Transcript and Presenter's Notes

Title: Chapter 33 Slides


1
Exposure Java-AB 2007
Chapter 33 Slides
Linked Lists I
PowerPoint Presentation created by Mr. John L.
M. Schram
From Materials Created by Mr. Leon Schram
2
ArrayList Warning
Java's ArrayList class allows dynamic resizing
during program execution. An ArrayList object
may have some extra capacity for additional new
elements. If the capacity is not sufficient to
allow adding new elements, a new block of memory
is allocated, data is shifted from the old block
to the new block, and the old block is returned
to available memory for future use. This process
can take considerable execution penalty, if the
array has many elements, each storing large
quantities of data.
3
The
LinkedList
Class
4
// Java3301.java // This program reviews the
ltLinkedListgt class with methods // ltaddFirstgt,
ltaddLastgt, ltgetFirstgt and ltgetLastgt. import
java.util.LinkedList public class
Java3301 public static void main(String
args) System.out.println("JAVA3301.JAVA\n")
LinkedList family new LinkedList() family
.addFirst("John") family.addLast("Greg") fam
ily.addLast("Maria") family.addLast("Heidi")
System.out.println(family.getFirst()) System.o
ut.println(family.getLast()) System.out.println
() System.out.println("family "
family) System.out.println()
5
// Java3302.java // This program creates a linked
list with four nodes and displays each node
starting with // the first node to the last node
using the ltgetFirstgt and ltremoveFirstgt
methods. import java.util.LinkedList public
class Java3302 public static void main(String
args) System.out.println("JAVA3302.JAVA\n")
LinkedList family new LinkedList() family
.addFirst("John") family.addLast("Greg") fam
ily.addLast("Maria") family.addLast("Heidi")
for (int k 1 k lt 4 k) System.out.pr
intln(family.getFirst()) family.removeFirst()
System.out.println()
6
// Java3303.java // This program creates a linked
list with four nodes and displays each node
starting with // the last node to the first node
using the ltgetLastgt and ltremoveLastgt
methods. import java.util.LinkedList public
class Java3303 public static void main(String
args) System.out.println("JAVA3303.JAVA\n")
LinkedList family new LinkedList() family
.addFirst("John") family.addLast("Greg") fam
ily.addLast("Maria") family.addLast("Heidi")
for (int k 1 k lt 4 k) System.out.pr
intln(family.getLast()) family.removeLast(
) System.out.println()
7
Linked List Methods
8
The
ListIterator
Class
9
// Java3304.java // This program demonstrates how
to traverse a linked list using the ltnextgt //
method of the ltListIteratorgt class. import
java.util.LinkedList import java.util.ListIterato
r public class Java3304 public static void
main(String args) System.out.println("JAVA3
304.JAVA\n") LinkedList family new
LinkedList() family.addFirst("John") fami
ly.addLast("Greg") family.addLast("Maria") f
amily.addLast("Heidi") ListIterator iter
family.listIterator() for (int k 1 k lt
4 k) System.out.println(iter.next()) Sy
stem.out.println()
10
Constructing aListIterator Object
LinkedList family new LinkedList() ListIterato
r iter family.listIterator() The listIterator
method of the LinkedList class instantiates an
iter object of the ListIterator class.
11
Iterator Method next
System.out.print(hAccess.next()" ") Method
next moves the iterator to the next element,
and then returns it.
12
// Java3305.java // This program demonstrates how
to traverse a linked list until the // end is
found, using the lthasNextgt method. import
java.util.LinkedList import java.util.ListIterato
r public class Java3305 public static void
main(String args) System.out.println("JAVA3
305.JAVA\n") LinkedList family new
LinkedList() family.addFirst("John") famil
y.addLast("Greg") family.addLast("Maria") fa
mily.addLast("Heidi") ListIterator iter
family.listIterator() while (iter.hasNext())
System.out.println(iter.next()) System.out.pri
ntln()
13
ListIterator Method hasNext
while (iter.hasNext()) Method hasNext returns
true if an element remains in the Collection
object and returns false otherwise.
14
ListIterator Method remove
iter.remove() Method remove removes the current
item pointed to by the ListIterator.
15
// Java3306.java // This program demonstrates how
to use the ltListIteratorgt class to access //
elements in a ltLinkedListgt object. The
ltListIteratorgt adds the ltaddgt // and ltsetgt method
to the three ltIteratorgt methods. import
java.util. public class Java3306 public
static void main(String args) System.out.pr
intln("\nJAVA3306.JAVA\n") LinkedList list
new LinkedList() for (int k 1000 k lt
5000 k 1000) list.add(new
Integer(k)) System.out.println(list) System.
out.println() ListIterator iter
list.listIterator() while (iter.hasNext())
iter.next() iter.set(new
Integer(9999)) iter.add(new
Integer(1111)) System.out.println(list)
System.out.println()
16
ListIterator Method add
iter.add(new Integer(1111)) Method add inserts
a new element between the current item pointed to
by the ListIterator and the next element.
17
ListIterator Method set
iter.set(new Integer(9999)) Method set replaces
the current item pointed to by the Listiterator
with the provided argument.
18
// Java3307.java // This program reviews
implementing a queue with the ltArrayListgt
class. import java.util.ArrayList public class
Java3307 public static void main (String
args) System.out.println("JAVA3307.JAVA\n")
MyQueue students new MyQueue() students
.enQueue("Luke Watts") students.enQueue("Brian
Sims") students.enQueue("Mike
Lewis") students.enQueue("Jamie
Singbush") System.out.println("There are "
students.getSize() " students stored on the
queue") System.out.println() if
(students.isEmpty()) System.out.println("The
queue is empty") else System.out.println(
"Queue front contains " students.peekFront())
System.out.println() while
(!students.isEmpty()) String student
(String) students.deQueue() System.out.println
(student) System.out.println()

19
class MyQueue private ArrayList data //
stores queue data private final int front //
keeps index of the queue front public
MyQueue() // Initializes an empty array object
with references of private variable data
objects. data new ArrayList() front
0 public boolean isEmpty() return
data.size() 0 // Returns true if data is
empty, false otherwise public void enQueue
(Object x) data.add(x) // Adds variable x
to the back of the queue public Object
deQueue() return data.remove(front) //
Returns and removes the front element from the
queue public Object peekFront() return
data.get(front) // Returns the front element
from the queue without removal public int
getSize() return data.size() // returns
the number of queue elements
20
// Java3308.java // This program demonstrates
implementing a queue with the ltLinkedListgt
class. import java.util.LinkedList public class
Java3308 public static void main (String
args) System.out.println("JAVA3308.JAVA\n")
MyQueue students new MyQueue() students
.enQueue("Luke Watts") students.enQueue("Brian
Sims") students.enQueue("Mike
Lewis") students.enQueue("Jamie
Singbush") System.out.println("There are "
students.getSize() " students stored on the
queue") System.out.println() if
(students.isEmpty()) System.out.println("The
queue is empty") else System.out.println(
"Queue front contains " students.peekFront())
System.out.println() while
(!students.isEmpty()) String student
(String) students.deQueue() System.out.println
(student) System.out.println()

21
class MyQueue private LinkedList data //
stores queue data public MyQueue() data
new LinkedList() // Initializes an empty
array object with references of private variable
data objects. public boolean isEmpty()
return data.size() 0 // Returns true if
data is empty, false otherwise public void
enQueue (Object x) data.addLast(x) //
Adds variable x to the back of the
queue public Object deQueue() return
data.removeFirst() // Returns and removes
the front element from the queue public Object
peekFront() return data.getFirst() //
Returns the front element from the queue without
removal public int getSize() return
data.size() // returns the number of queue
elements
22
Old Fashion
Pre-OOP
LinkedLists
23
What is this
class ListNode public Object value public
ListNode next
This is a "Self-Referencing Declaration". It is
used in the creation of Linked Lists.
24
Self-Referencing Declaration
A linked list data structure is made up of
self-referential objects. If the object is of
ListNode type then one - or more - fields
references ListNode, like the example
below. class ListNode public Object
value public ListNode next
25
// Java3309.java // This program demonstrates
creating a linked list in an old-fashioned
pre-oop // style that uses a poorly designed
ltListNodegt class. // Note that the ltListNodegt
class contains only data and no methods at
all.
26
// Java3309.java // This program demonstrates
creating a linked list in an old-fashioned
pre-oop // style that uses a poorly designed
ltListNodegt class. // Note that the ltListNodegt
class contains only data and no methods at
all.
p ? ?
27
// Java3309.java // This program demonstrates
creating a linked list in an old-fashioned
pre-oop // style that uses a poorly designed
ltListNodegt class. // Note that the ltListNodegt
class contains only data and no methods at
all.
p ? ? temp
28
// Java3309.java // This program demonstrates
creating a linked list in an old-fashioned
pre-oop // style that uses a poorly designed
ltListNodegt class. // Note that the ltListNodegt
class contains only data and no methods at
all.
temp p ?

29
// Java3309.java // This program demonstrates
creating a linked list in an old-fashioned
pre-oop // style that uses a poorly designed
ltListNodegt class. // Note that the ltListNodegt
class contains only data and no methods at
all.
temp p ?
John
30
// Java3309.java // This program demonstrates
creating a linked list in an old-fashioned
pre-oop // style that uses a poorly designed
ltListNodegt class. // Note that the ltListNodegt
class contains only data and no methods at
all.
temp p ?
John
31
// Java3309.java // This program demonstrates
creating a linked list in an old-fashioned
pre-oop // style that uses a poorly designed
ltListNodegt class. // Note that the ltListNodegt
class contains only data and no methods at
all.
p ?
temp ?
John
32
// Java3309.java // This program demonstrates
creating a linked list in an old-fashioned
pre-oop // style that uses a poorly designed
ltListNodegt class. // Note that the ltListNodegt
class contains only data and no methods at
all.
p ? temp ?
John
33
// Java3309.java // This program demonstrates
creating a linked list in an old-fashioned
pre-oop // style that uses a poorly designed
ltListNodegt class. // Note that the ltListNodegt
class contains only data and no methods at
all.
p ? temp ?
John
Greg
34
// Java3309.java // This program demonstrates
creating a linked list in an old-fashioned
pre-oop // style that uses a poorly designed
ltListNodegt class. // Note that the ltListNodegt
class contains only data and no methods at
all.
p ? temp ?
John
Greg
35
// Java3309.java // This program demonstrates
creating a linked list in an old-fashioned
pre-oop // style that uses a poorly designed
ltListNodegt class. // Note that the ltListNodegt
class contains only data and no methods at
all.
p ? temp ?
John
Greg
36
// Java3309.java // This program demonstrates
creating a linked list in an old-fashioned
pre-oop // style that uses a poorly designed
ltListNodegt class. // Note that the ltListNodegt
class contains only data and no methods at
all.
p ? temp ?
John
Greg
37
// Java3309.java // This program demonstrates
creating a linked list in an old-fashioned
pre-oop // style that uses a poorly designed
ltListNodegt class. // Note that the ltListNodegt
class contains only data and no methods at
all.
p ? temp ?
John
Greg
David
38
// Java3309.java // This program demonstrates
creating a linked list in an old-fashioned
pre-oop // style that uses a poorly designed
ltListNodegt class. // Note that the ltListNodegt
class contains only data and no methods at
all.
p ? temp ?
John
Greg
David
39
// Java3309.java // This program demonstrates
creating a linked list in an old-fashioned
pre-oop // style that uses a poorly designed
ltListNodegt class. // Note that the ltListNodegt
class contains only data and no methods at
all.
p ? temp ?
John
Greg
David
40
Next Reference Rule
The next data field is one-directional. This
linking reference value is only capable of
linking to one other node. In other words, if
Node A links to node B, the two nodes are linked
but it is not possible to traverse from node B
back to node A with the link A B.
41
Modern
OOP
LinkedLists
42
OOP ListNode Improvements
43
// Java3310.java // This program demonstrates how
to create a linked list of four nodes with // a
well-designed ltListNodegt class. This linked list
behaves like a stack. public class
Java3310 public static void main (String
args) System.out.println("\nJAVA3310.JAVA\n
") ListNode p null ListNode temp
null System.out.println("Constructing a node
with Isolde") p new ListNode("Isolde",temp)
temp p System.out.println("Constructing a
node with Maria") p new ListNode("Maria",temp
) temp p System.out.println("Constructing
a node with Heidi") p new ListNode("Heidi",te
mp) temp p System.out.println("Constructin
g a node with Diana\n") p new
ListNode("Diana",temp) temp p while (p !
null) System.out.println(p.getValue())
p p.getNext() System.out.println()
ListNode class on next slide
44
class ListNode 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 private Object value private
ListNode next
The same ListNode class will be used for the next
few programs.
45
// Java3311.java // This program demonstrates how
to create a linked list of four nodes with // a
well-designed ltListNodegt class. This linked list
behaves like a queue. public class
Java3311 public static void main (String
args) System.out.println("\nJAVA3311.JAVA\n
") ListNode p null ListNode front
null ListNode temp null System.out.println("
Constructing a node with Isolde") p new
ListNode("Isolde",null) front p temp
p System.out.println("Constructing a node with
Maria") p new ListNode("Maria",null) temp.
setNext(p) temp p System.out.println("Const
ructing a node with Heidi") p new
ListNode("Heidi",null) temp.setNext(p) temp
p System.out.println("Constructing a node
with Diana\n") p new ListNode("Diana",null)
temp.setNext(p) temp p p front while
(p ! null) System.out.println(p.getValue()
) p p.getNext() System.out.println()

46

47
// Java3312.java // This program demonstrates how
to create a linked list of integers. // The
linked list is created like a stack. public
class Java3312 public static void main (String
args) System.out.println("\nJAVA3312.JAVA\n
") ListNode p null ListNode temp
null for (int k 100 k lt 110
k) System.out.println("Constructing a
node with " k) p new ListNode(new
Integer(k),temp) temp p System.out.pr
intln() while (p ! null) System.ou
t.print(p.getValue() " ") p
p.getNext() System.out.println("\n\n")

48
// Java3313.java // This program demonstrates how
to create a linked list of integers. // The
linked list is created like a queue. public
class Java3313 public static void main (String
args) System.out.println("\nJAVA3313.JAVA\n
") ListNode p null ListNode temp null
ListNode front null System.out.println("Const
ructing a node with 100") front new
ListNode(new Integer(100),null) temp
front for (int k 101 k lt 110
k) System.out.println("Constructing a
node with " k) p new ListNode(new
Integer(k),null) temp.setNext(p) temp
p System.out.println() p
front while (p ! null) System.out.print
(p.getValue() " ") p p.getNext() S
ystem.out.println("\n\n")
49
Deleting
From a
LinkedLists
50
Deleting from a Linked ListStep 1
front
Meg is the element to be deleted.
51
Deleting from a Linked ListStep 2
front
52
Deleting from a Linked ListStep 3
front
53
Deleting from a Linked ListStep 4
front
54
AP Exam Alert
Students with a firm understanding of many
computer science concepts often lose points on
their free response section of the exam, because
they do not consider all the possible cases.
55
// Java3314.java // This program demonstrates how
to delete a node from a linked list. // The
ltdeleteNodegt method of the ltListgt class does not
work in all cases. // For operating convenience
the ltListNodegt class uses the ltintgt type for its
data ltvaluegt field. public class
Java3314 public static void main (String
args) System.out.println("\nJAVA3314.JAVA\n
") List list new List(10) list.displayList
() list.deleteNode(5) // delete from the
middle list.displayList() list.deleteNode(1)
// delete from the front list.displayList()
list.deleteNode(10) // delete from the
end list.displayList() list.deleteNode(25)
// delete non-existing node System.out.println()
class ListNode public ListNode (int
initValue, ListNode initNext) value
initValue next initNext public int
getValue () return value public
ListNode getNext () return next public
void setValue (int theNewValue) value
theNewValue public void setNext (ListNode
theNewNext) next theNewNext private int
value private ListNode next
This program does not work for the case of
deleting the front node.
List class on next slide
56

57

58
Possible Cases to Considerto Delete a Node
1. The list is empty. 2. Delete the first node.
(same as one-node list) 3. Delete a middle
node. (same as last node) 4. The requested node
does not exist in the list.
59
Inserting
Into a
LinkedLists
60
Inserting into a Linked ListStep 1
first
last
Meg will now be inserted.
61
Inserting into a Linked ListStep 2
first
last
62
Inserting into a Linked ListStep 3
first
last
63
Inserting into a Linked ListStep 4
first
last
64
Inserting into a Linked ListStep 5
first
last
65

66
Possible Cases to Considerto Insert a Node
1. Insert into a completely empty
list. 2. Insert in front of an existing
list. 3. Insert in the middle of a list.
(same as at the end)
67
LinkedLists
and
Memory
68
Linked Lists and Memory
ltltlt null
69
GfxNode
Class Demo
70

71
Initial Output
This program demonstrates the use of the GfxNode
methods, but does not construct the Linked List
in the proper fashion.
Completed Output
72
// GfxNode constructor instantiates an object and
stores its Top-Left coordinate (tlx,tly)
information, as well as the // length and width
of the node object. A node object with two
fields is drawn at the specified
coordinate. public GfxNode(Graphics g, int tlx,
int tly, char ltr, int clr, int dt) // Method
getColor a private helper method to make it
easier to use colors in a graphics
program. private Color getColor(int clr) //
Method getX returns the top-left X-coordinate of
a linked list node. public int getx() // Method
getY returns the top-left Y-coordinate of a
linked list node. public int gety() // Method
drawPointer draws a vertical pointer down to an
existing node. The first pointer to a node //
uses OffSet value 1 and the second pointer to the
same node uses OffSet value 2. The result is
that // the second pointer is moved farther to
the right. public void drawPointer(Graphics g,
char ltr, int offSet, int clr) // Method
enterData draws a letter in the Data field of the
GfxNode. void enterData(Graphics g, char ltr, int
clr) // Method drawLink draws a link from the
current sourceNode to the endNode in the
specified color (clr). public void
drawLink(Graphics g, GfxNode endNode, int clr)
// Method drawNull draws a diagonal g.drawLine
in the Next field of a list node, to indicate a
NULL value. public void drawNull(Graphics g, int
clr) // Method drawLetter upper-case Letter
characters. The characters are drawn in a 9x9
pixel box. The (x,y) // parameters indicate the
coordinate of the top-left corner of the box.
Only capital letters and numbers are
drawn. public void drawLetter(Graphics g, char
ltr, int x, int y) // Method delay allows
viewing the sequence in which the linked lists
are drawn/ private void delay(double n)
Write a Comment
User Comments (0)
About PowerShow.com