recitation 8 recursion and linked lists n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Recitation 8 Recursion and Linked Lists PowerPoint Presentation
Download Presentation
Recitation 8 Recursion and Linked Lists

Loading in 2 Seconds...

play fullscreen
1 / 7

Recitation 8 Recursion and Linked Lists - PowerPoint PPT Presentation


  • 90 Views
  • Uploaded on

Recitation 8 Recursion and Linked Lists. Ben Stoddard Tabitha Peck March 7, 2014. Covered in this recitation. Recursion Linked Lists Recursion + Linked Lists. Why recursion?. Many of these methods would work better with loops.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Recitation 8 Recursion and Linked Lists' - peri


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
recitation 8 recursion and linked lists

Recitation 8Recursion and Linked Lists

Ben Stoddard

Tabitha Peck

March 7, 2014

covered in this recitation
Covered in this recitation
  • Recursion
  • Linked Lists
  • Recursion + Linked Lists
why recursion
Why recursion?
  • Many of these methods would work better with loops.
  • Recursion will be very useful to you later (trees, midterms, finals) though, so it’s better to practice now.
  • These problems are another chance to think about how recursion works. What are your base cases? How do you simplify the problem?
unique to this recitation
Unique to this recitation
  • Private Variables
    • Using Getter/Setter methods
  • Private Helper Methods
    • User calls the public method for the answer, and the public method uses the private helper methods
      • In this recitation, the helper methods will be recursive (see next slide)
add to tail
Add to tail

Non-Recursive

Recursive

public void addToTail(int value){

   if(head == null) {

       head = new IntListNode(value);

   } else {

addToTail(value, head);

   }

       size++;

}

private void addToTail(int value, IntListNode n) {

   if(n.getNext() == null) {

n.setNext(new IntListNode(value));

   } else {

addToTail(value, n.getNext());

   }

}

public void addToTailNoRecursion(int   value) {

   if(head == null) {

       head = new IntListNode(value);

   } else {

IntListNode current = head;

       while(current.getNext() != null) {

           current = current.getNext();

       }

current.setNext(new IntListNode(value));

   }

   size++;

}

← Base Case: Empty List

← Base Case: Empty List

← Call recursive

helper

← Loop to advance

↑ Add new

← Base Case: No next

↑ Recursive case: call with next node

(loop advance)

what you will do
What you will do
  • Complete the recursive private helper functions
    • Contains: Check for a value.
    • countOccurences: Counts a value.
    • sum: Sums the list.
    • sumEven: Sums only the even values in the list.
    • reverseList: Reurns the same list, but in reverse order.
    • mergeLists: Merges this list with another and returns the resulting list.
      • *Assumes both lists are sorted. Result should be sorted as well.
go for it
Go for it!
  • You shouldn’t have to change any given code.
  • You should be able to pass unit tests if you are done.
  • These slides are online for your reference.
  • Please do as much as you can and ask UTAs for help.

Submit before March 21 (your next recitation).