Tutorial #9 Agenda 1.00 4/22/2001

advertisement
Tutorial #9
1.00
4/22/2001
Agenda
•
•
•
•
Quiz 2
Linked Lists
Tree (review)
PS #8
1
Quiz 2
•
•
•
•
Mean: 76
Standard deviation: 22
Some students still to take the make-up
Probably available after Tuesday in 5-334
(you will need to ask a TA for your quiz)
Linked Lists
•
•
•
•
Simple way to collect a series of objects
Each object (or link) refers to the next
Example 1: separate link class
Example 2: customized link class
2
Linked Lists: General
SLink
SLink
SLink
next
obj
next
obj
next
obj
Object 1
Object 2
Object 3
class SLinkedList {
SLink head;
// add, delete, isEmpty, etc.;
}
null
class SLink {
SLink next;
Object obj;
}
Customized Linked Lists
• Link and data are combined into one class
• Less general, but easier to program
student
student
student
next
name
...
next
name
...
next
name
...
class ListOfStudents {
JavaStudent head;
// add, delete, etc.
}
null
class JavaStudent {
JavaStudent next;
String name;
int[] quizGrades;
int[] probSetGrades[];
}
3
Tutorial Exercise
• "Check" class
• Similar in structure to PS#8
Trees
• See Tutorial notes for definitions
• preorder traversal
– root, left subtree, right subtree
• inorder traversal
– left subtree, root, right subtree
• postorder traversal
– left subtree, right subtree, root
4
PS #8
• Use a linked list to represent a polynomial
• Each node is a term:
• 3x4 - 0.3x2 + 2.9x - 5
• How can we represent this as a linked list?
PS #8
• You are given PolyTestMain.java
• Fill in code that is missing
• You can add your Node & Polynomial
classes to this file
• Suggestions
– make Node class very simple
– Polynomial class should be a custom linked
list
5
PS#8: Node class
• What member variables do you need?
• What methods?
PS#8: Polynomial class
In class exercise:
• What should these
take as arguments?
• What should they
return?
• Should they be static?
•
•
•
•
•
•
•
•
addTerm()
removeTerm()
addPoly()
evalPoly()
multScalar()
multPoly()
derivPoly()
printPoly()
6
PS#8:Tolerance
final public static TOL = 1E-6;
• When do we use this tolerance?
• How do we use it?
7
Download