Question of the Day A friend tells the truth when saying: A road near my house runs directly...

24
Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile, & end up south of where I started How does he do it?

Transcript of Question of the Day A friend tells the truth when saying: A road near my house runs directly...

Page 1: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Question of the Day

A friend tells the truth when saying:

A road near my house runs directly north-south; I get on the road facing north, drive for a mile, & end up south of where I started

How does he do it?

Page 2: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Question of the Day

A friend tells the truth when saying:

A road near my house runs directly north-south; I get on the road facing north, drive for a mile, & end up south of where I started

How does he do it?

Note: He (anyone) doesn't live near the north pole

Page 3: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Question of the Day

A friend tells the truth when saying:

A road near my house runs directly north-south; I get on the road facing north, drive for a mile, & end up south of where I started

How does he do it?

Note: He (anyone) doesn't live near the north pole

Your friend drives in reverse!

Page 4: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

LECTURE 18:TRAVERSING A LIST & MORE LINKED-LISTS

Page 5: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Are created using linear sequence of nodes

Each Node contains: Reference to data (element) stored in Node Link to next Node in linked list

Singly Linked List

Page 6: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Nodes Are Not Enough

Nodes are workers making linked lists possible Necessary for the smooth running of linked

list Treated like other jobs smoothing

things out Added & removed as needed Used only to keep the list running

Page 7: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Nodes Are Not Enough

Page 8: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Nodes Are Not Enough

Page 9: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Nodes Are Not Enough

Nodes are workers making linked lists possible Necessary for the smooth running of linked

list Treated like other jobs smoothing

things out Added & removed as needed Used only to keep the list running Never allowed outside of the linked

list

Page 10: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Nodes Are Not Enough

Nodes are workers making linked lists possible Necessary for the smooth running of linked

list Treated like other jobs smoothing

things out Added & removed as needed Used only to keep the list running Never allowed outside of the linked list Has to teach CSC212 every year

Page 11: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

T removeFirst()

Page 12: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

T removeFirst()SLinkedListhead

size 4

SLinkedListhead

size 3

Page 13: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

T removeMiddle(Node<T> prev)SLinkedListhead

size 4

SLinkedListhead

size 3

Page 14: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Traversing a List

Often need to go through items in a list Return element stored by the nth Node Search and find if element is in list Count elements larger/smaller/equal than

some value Add an element at list’s end

Cannot do this directly with linked lists Arrays have brackets, but linked lists use

objects Must traverse list by travelling from Node to Node

Relies heavily on understanding how objects work

Page 15: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

void addLast(T el)SLinkedListhead

size 4

SLinkedListhead

size 5

Page 16: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Link to previous node in list also in each node

Each DNode contains: Element (data) reference Link to next DNode Prev(ious) DNode also linked

Doubly Linked List

elem

DNode Instance

nextprev

Page 17: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Link to previous node in list also in each node

Each DNode contains: Element (data) reference Link to next DNode Prev(ious) DNode also linked

Doubly Linked List

Doubly Linked List With 4 Elements

Ø

Page 18: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Link to previous node in list also in each node

Each DNode contains: Element (data) reference Link to next DNode Prev(ious) DNode also linked

Doubly Linked List

Sequence of 4 DNodes

Ø

Page 19: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Link to previous node in list also in each node

Each DNode contains: Element (data) reference Link to next DNode Prev(ious) DNode also linked

Doubly Linked List

Doubly Linked List With 4 Elements & Sentinels

Ø Ø

Page 20: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Link to previous node in list also in each node

Each DNode contains: Element (data) reference Link to next DNode Prev(ious) DNode also linked

Doubly Linked List

Doubly Linked List With 4 Elements & Sentinels

DListheadtailsize 4

Ø Ø

Page 21: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Doubly Linked List

DNode could extend Node next & elem fields are needed by both

classes Only difference is prev field added by DNode

DList is not subclass of SList Both classes define identical methods… …are entirely different when implemented

1

Page 22: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Identical nodes as singly or doubly linked list This example shows singly linked variety

Reference to first node stored in last node

Notice this becomes a big circle Now you know idea behind name

Circular Linked List

B C D

head

A

Page 23: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

Your Turn

Get into your groups and complete activity

Page 24: Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,

For Next Lecture

Read GT3.5 for Wednesday What is recursion? Why does recursion rock so much? How do you write a recursive method?

Week #7 weekly assignment available now

Angel also has programming assignment #1 Pulls everything together and shows off

your stuff