COSC 2007 Data Structures II Dr Dave Goforth FA 377 [email protected] (705)675-1151x2316 .
-
Upload
lester-reed -
Category
Documents
-
view
223 -
download
0
Transcript of COSC 2007 Data Structures II Dr Dave Goforth FA 377 [email protected] (705)675-1151x2316 .
COSC 2007 Data Structures II
Dr Dave GoforthFA [email protected](705)675-1151x2316http://www.cs.laurentian.ca/dgoforth/cosc2007/outline.html
(local)
Course Objectives
•Abstract Data Types (ADTs) for collections of Objects
•JAVA 5.0 generic Collections classes
•Non-linear data structures that implement ADTs
•Design of applications using ADTs
Course Organization
•6 programming assignments 36%
due Thursday, 10:30 AM
every two weeks
•1 midterm examination (2 hours) 24%
Tuesday June 26 – 10:00 – 11:55 AM
•1 final examination (3 hours) 40%
• Tutorial? 12:00 – 12:30 after lecture?
Review
•linked structures
•object-oriented implementation concepts
•linear data structures
•recursion
Linked lists - references
•variables of an object type contain addresses or references to objects
Point p;
p = new Point(5,-2);
p
p
xy
5-2
Linked lists - nodes
•nodes are objects that contain references to objects of their own type
class Node{ int data; Node next;}
Node n1, n2;
n1 = new Node(3,null);
n2 = new Node(5,n1);
n2 datanext
5 datanext
3
n1
Doubly-linked lists
Why?– bi-directional movement in list
Effect on implementation– maintain one reference to ‘current’ – no need for ‘previous’ reference
BUT…– insertion and deletion more complex
Nodes for doubly-linked lists
• prev and next Node references
• current may be only reference into list
data
next
-5
prev
data
next
90
prev
data
next
34
prev
data
next
prev
data
next
prev
current
Nodes for doubly-linked lists
• head and tail of list identified by null references
data
next
-5
prev
data
next
90
prev
data
next
34
prev
current
(head) (tail)
Inserting new Node• before or after current?• four links to manage
data
next
-5
prev
data
next
90
prev
data
next
34
prev
current
data
next
14
prev
Deleting a node
data
next
-5
prev
data
next
90
prev
data
next
34
prev
data
next
prev
data
next
prev
current
• current Node or before or after?• four links to manage
? ??
• on empty list
• on list with one Node
• at one end of list
Operations: Special cases
datanext
-8
prev
datanext
23
prev
datanext
91
prev
operations at current node-get data, update data-insert node, delete nodeoperations on current reference-move forward, backward, head, tail
operations on list-iterate
Object-oriented designDoubly-linked List methods
Assignment 1 - text editor
•linked structures
•recursion
•object-oriented implementation concepts
•linear data structures
•testing
Assignment 1 - text editor
Evalulation
•code
•javadoc documentation
•testing
Submission
•web-based automated submission
•May 10, 10:30 AM
• value 6%
emai
l
The editor – ed, the line editor
• text is stored in a doubly-linked list of Strings (no wrap-around from line to line)
• changes to text occur at current line by commands at console
• three kinds of operation:1. movement to another line
2. changes to text in list of Strings
3. file management
The editor – ed, updated version
• text is displayed in a JFrame• display is updated as changes occur (still
at the console)• file management is by JFileChooserAssignment 1
(local)
Recursion
• problem decomposition– rewrite problem in terms of a reduced
version of itself
• method structure– base case– recursive case
Recursion example
• problem decomposition– tired old example: factorial
• method structurepublic static int F(int n){ if (n<0) throw new IllegalArgumentException(); if (n>1) return n * F(n-1); return 1;}
)1(.
1.2)...2).(1.()(
nFn
nnnnF
Recursion in assignment
• no iterative structures can be used in this assignment– no “for”– no “while”– no “do…while”
• use recursive methods instead
Recursion in assignment - examplepublic Node getHead(){ Node at = this; while (at.prev != null) at = at.prev; return at;}
current = current.getHead();
public class Node
{ int data;
Node next,
prev;
}
Recursion in assignment - examplepublic Node getHead(){ if (prev == null) return this; return prev.getHead();}
public class Node
{ int data;
Node next,
prev;
}
current = current.getHead();
Static and instance methods
Problem: case when current is null
Static version:
current = current.getHead();
current = Node.getHead(current);