Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

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, & end up south of where I started How does he do it?

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, & end up south of where I started How does he do it? Note: He (anyone) doesn't live near the north pole

3 Question of the Day

4

5  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 elemnextelemnextelemnextelemnext Node head size  4 SLinkedList

6 Node s Are Not Enough  Node s 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

7 Node s Are Not Enough

8

9  Node s 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

10 Node s Are Not Enough

11 T removeFirst()

12 head size  4 head size  3

13 T removeMiddle(Node prev) head size  4 head size  3

14 Traversing a List  Often need to go through items in a list  Return element stored by the n th 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

15 void addLast(T el) head size  4 head size  5

16  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

17  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

18  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 DNode s

19  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

20  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 header trailer size  4 DList

21 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

22  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 head size  4

23 Your Turn  Get into your groups and complete activity

24 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 #6 weekly assignment available now  Since no school Tues., assignment due Wednesday  Programming assignment #1 delayed until Wed.  If you all agree, project would be much cooler & fun  Pulls everything together & sees how much you know


Download ppt "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,"

Similar presentations


Ads by Google