More recursion

Some functions

String functions
Parsing Arithmetic expression evaluation
Backtracking All the Go on the whiteboard
Tower of Hanio A recursive algorithms

Another look at linked lists

This is another chance to revisit a class from the match calls lab and also get a preview of Chapter 16.

Use NetBeans project to create a Java application with a name of your choosing. For now leave the main method unmodified.

Add the following class to your project. The class has no method and public fields. Just pretend you are programming in C.

package edu.unca.cs.csci202 ;

class DigitNode {
   public byte value ;
   public DigitNode next ;

   public DigitNode() {
       value = 0 ;
       next = null ;
   }
   
   public DigitNode(byte value, DigitNode next) {
       this.value = value ;
       this.next = next ;
   }
}

The first step is to create the following method createLSBFirst.

    static DigitNode createLSBFirst(long n) {
        if (n<10) {
            return new DigitNode((byte)n, null) ;
        } else {
            DigitNode tail = createLSBFirst(n/10) ;
            return new DigitNode((byte)(n%10), tail) ;
        }
    }

Also, add the following line to main.

        DigitNode listNum = createLSBFirst(1123979876876124L) ;
        system.out.println(listNum) ;

Place one breakpoint at the first line of createLSBFirst and another at the system.out.println . Start to debug your project. When the program is stopped, hit F7 about ten times to step into the recursion. Be sure the Variables tab is on. You should see n being decreased.

Continue on for a while. Eventually, your program will step into the code for the constructor of NodeList. This is when the recursion starts to unroll.

Use F7 to step through another dozen or so steps, but stop at a point where you are on the return statement for createLSBFirst. At this time, you should see tail displayed in the variables. Press on the expander next to tail to see what it being generated.

At this time terminate your debugging session and remove the breakpoint in createLSBFirst. Now restart debugging. This should stop at the println where you should be able to exam the entire returned structure. Notice that it is in “reverse” order or least significant digit first.

Printing the number

Now write a method printNumber that takes one of these linked lists of digits and returns a string corresponding to printing it in the right order.

    static String printNumber(DigitNode d) 

Incrementing the number

Next write a method to add 1 to one of the lists of digits. This method should return the incremented number not modify the list holding the number. Test your methods out on a number similar to 12345678999999 to make sure you propagate the carry.

    static DigitNode plusplusNumber(DigitNode d) 

Adding numbers

Next write a method to add two numbers. This one is tricky because the numbers may not be the same length.

    static DigitNode addNumbers(DigitNode a, DigitNode b) 

MSB first

You might give some thought to returning the list with the most significant digit first and writing a method createMSBFirst. However, don’t spend too much time with this method. It really isn’t worth the effort.