COSC 2007 Data Structures II Dr Dave Goforth FA 377 [email protected] (705)675-1151x2316 .

23
COSC 2007 Data Structures II Dr Dave Goforth FA 377 [email protected] (705)675-1151x2316 http://www.cs.laurentian.ca/dgoforth/cosc20 07/outline.html (local)

Transcript of COSC 2007 Data Structures II Dr Dave Goforth FA 377 [email protected] (705)675-1151x2316 .

Page 1: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (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)

Page 2: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

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

Page 3: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

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?

Page 4: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

Review

•linked structures

•object-oriented implementation concepts

•linear data structures

•recursion

Page 5: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

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

Page 6: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

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

Page 7: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

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

Page 8: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

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

Page 9: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

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)

Page 10: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

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

Page 11: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

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

? ??

Page 12: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

• 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

Page 13: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

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

Page 14: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

Assignment 1 - text editor

•linked structures

•recursion

•object-oriented implementation concepts

•linear data structures

•testing

Page 15: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

Assignment 1 - text editor

Evalulation

•code

•javadoc documentation

•testing

Submission

•web-based automated submission

•May 10, 10:30 AM

• value 6%

emai

l

Page 16: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

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

Page 17: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

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)

Page 18: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

Recursion

• problem decomposition– rewrite problem in terms of a reduced

version of itself

• method structure– base case– recursive case

Page 19: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

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

Page 20: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

Recursion in assignment

• no iterative structures can be used in this assignment– no “for”– no “while”– no “do…while”

• use recursive methods instead

Page 21: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

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;

}

Page 22: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

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();

Page 23: COSC 2007 Data Structures II Dr Dave Goforth FA 377 dgoforth@cs.laurentian.ca (705)675-1151x2316 .

Static and instance methods

Problem: case when current is null

Static version:

current = current.getHead();

current = Node.getHead(current);