csc 364 001 data structures and algorithms lee weiner 572-6025 [email protected]

30
CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 [email protected]

Upload: brett-cunningham

Post on 20-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

CSC 364 001Data Structures and Algorithms

Lee Weiner572-6025

[email protected]

Page 2: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Student

- lastName : String

- firstName : String

- state : String

- major : String

+ createStudent()

+ getFirstName()

+ setFirstName()

+ getLastName()

+ setLastName()

+ getState()

+ setState()

+ getMajor()

+ setMajor()

+ getRate()

- isStateValid()

- isMajorValid()

Student

- lastName : String

- firstName : String

- state : String

- major : String

+ createStudent()

+ getFirstName()

+ setFirstName()

+ getLastName()

+ setLastName()

+ getState()

+ setState()

+ getMajor()

+ setMajor()

+ getRate()

- isStateValid()

- isMajorValid()

Student

- lastName : String

- firstName : String

- state : String

- major : String

+ createStudent()

+ getFirstName() : String

+ setFirstName() : void

+ getLastName() : String

+ setLastName() : void

+ getState() : String

+ setState() : void

+ getMajor() : String

+ setMajor() : void

+ getRate() : int

- isStateValid(): boolean

- isMajorValid() : boolean

Page 3: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Lee aaaa Dan cccc Joe ffff Bob null

abcd

head

abcd aaaa cccc ffff

Page 4: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Node

- data : Object

- link : Node

+ createNode()

+ getData() : Object

+ setData() : void

+ getLink() : Node

+ setLink() : void

Page 5: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Lee aaaa Dan cccc Joe ffff Bob null

abcd Barb cccc

abcd aaaa cccc ffff

xxxx

head

link = new Node( data, link );

Page 6: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Lee aaaa Dan xxxx Joe ffff Bob null

abcd Barb cccc

abcd aaaa cccc ffff

xxxx

head

Page 7: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Lee aaaa Dan cccc Joe ffff Bob null

abcd

abcd aaaa cccc ffff

head

link = link.link;

Page 8: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Lee aaaa Dan ffff Joe ffff Bob null

abcd

abcd aaaa cccc ffff

head

Page 9: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Node

- data : Object

- link : Node

+ createNode()

+ getData() : Object

+ setData() : void

+ getLink() : Node

+ setLink() : void

+ addNodeAfter() : void

+ removeNodeAfter() : void

Page 10: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Traversing a Linked ListTraversing a Linked List

Node current = head; while( current != null ) { System.out.println( current.getData() ); current = current.getLink(); }

Page 11: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Adding a Node to the End of Adding a Node to the End of the Listthe List

if( head == null ) head = new Node( data, null );else{ Node current = head; while( current.getLink() != null ) current = current.getLink(); current.addNodeAfter( data );}

Page 12: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

LinkedList

- head : Node

+ createLinkedList()

+ add(Object)

+ add(Object, int)

+ clear()

+ get(int) : Node

+ isEmpty() : boolean

+ size() : int

+ find(Object) : int

+ remove(Object)

+ remove(int)

- getReference(int) : Node

Page 13: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Node

- data : Object

- link : Node

+ createNode()

+ getData()

+ setData()

+ getLink()

+ setLink()

+ addNodeAfter()

+ removeNodeAfter()

0..* 1

LinkedList

- head : Node

+ createLinkedList()

+ add(Object)

+ add(Object, int)

+ clear()

+ get(int) : Node

+ isEmpty() : boolean

+ size() : int

+ find(Object) : int

+ remove(Object)

+ remove(int)

- getReference(int) : Node

UML “Has A” RelationshipUML “Has A” Relationship

Page 14: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Another Use of "Has A"Another Use of "Has A"

Employee

- home : HomeAddress

- job : JobInfo

- insurance : Insurance

- ssn : String

- lastName : String

- firstName : String

+ createEmployee()

+ getSSN() : String

+ setSSN()

+ getFirstName() : String

+ setFirstName()

+ getLastName() : String

+ setLastName()

HomeAddress

- street : String

- addr2 : String

- city : String

- state : String

- zip : String

+ getStreet() : String

+ setStreet()

+ getAddr2() : String

+ setString()

. . .

1 1

JobInfo

- jobCode : String

- title : String

- salary : String

- location : JobAddress

- startDate : Date

+ getJobCode() : String

+ setJobCode()

+ getTitle() : String

+ setTitle()

. . .

1 0..*

Page 15: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

StacksStacks

Page 16: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Stack

- stack : LinkedList

+ createStack()

+ push( Object )

+ pop() : Object

+ peek() : Object

+ isEmpty() : boolean

Page 17: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

QueuesQueues

Page 18: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Queue

- queue : LinkedList

+ createQueue()

+ enqueue( Object )

+ dequeue() : Object

+ size() : int

+ isEmpty() : boolean

Page 19: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Priority QueuePriority Queue

1

2

3

4

3

Page 20: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Priority QueuePriority Queue

1

2

3

4

2

Page 21: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Priority QueuePriority Queue

1

2

3

4

3

Page 22: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Priority QueuePriority Queue

1

2

3

4

1

Page 23: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Priority QueuePriority Queue

1

2

3

4

Page 24: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Priority QueuePriority Queue

1

2

3

4

1

Page 25: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Priority QueuePriority Queue

1

2

3

4

Page 26: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Priority QueuePriority Queue

1

2

3

4

Page 27: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Priority QueuePriority Queue

1

2

3

4

Page 28: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

Priority QueuePriority Queue

1

2

3

4

Page 29: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

4 Queue Methods for Priority 4 Queue Methods for Priority QueueQueue

enqueue(Object, int) - Add a node in the priority specified by the int

dequeue() - Loop through the priorities, dequeue a node from the highest priority that has one

size() - Loop through all priorities, summing and returning number of nodes.

isEmpty() - If all priorities are empty, return true

Page 30: CSC 364 001 Data Structures and Algorithms Lee Weiner 572-6025 weinerl@nku.edu

LinkedList

- head : Node

+ createLinkedList()

+ add(Object)

+ add(Object, int)

+ clear()

+ get(int) : Node

+ isEmpty() : boolean

+ size() : int

+ find(Object) : int

+ remove(Object)

+ remove(int)

- getReference(int) : Node

Stack

- stack : LinkedList

+ createStack()

+ push( Object )

+ pop() : Object

+ peek() : Object

+ isEmpty() : boolean

1 1