Question: Data Structures IN JAVA questions Code Review Section Exercise 1 : Given the factorial number sequence: 1 4 9 16 25 36 49 64 One

Data Structures IN JAVA questions

Code Review Section

Exercise 1 :

Given the factorial number sequence:

1 4 9 16 25 36 49 64

One of your friends took the recursive factorial functions and tried a different way using a pattern in the number sequence. The first term represents 1!, 2nd term is 2!, and so forth making the nth term representing n!.

After reviewing the code, you find there is one line of code with a problem. Show your friend your analysis by answering the following questions:

What is the code going to return when you call it with factorial(4)?

What line of code would you change to fix the function?

// Assume n > 0. Ignore calculating 0!

public int factorial(int n)

{

if (term == 1)

return 1;

return factorial(term 1) + (term + 2) - 1;

}

Exercise 2

A co-worker came up to you and said she developed an algorithm for the delete function of the circular doubly linked list like you did for programming assignment 1. Here is her insert code:

public void delete(int n)

{

Node temp = search(n);

if (temp != null)

{

if (currentSize <= 2)

current = null;

else

{

current = temp.next;

temp.prev.next = temp.next;

temp.next = temp.prev;

temp = null;

}

currentSize--;

return true;

}

return false;

}

In your infinite wisdom, you find error(s) in the code. Demonstrate your knowledge by explaining the following to her:

Draw a picture of what the CDLL looks like after inserting 5, then 4 behind 5 and then 3 behind 4. The current Node at this point should be pointing to 3 and all next/prev pointers with each Node are connected appropriately

Next, show what the CDLL will look like if you try delete(4) in this CDLL with this incorrect code and describe why it is wrong

Show the change(s) to this code to make the fix

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!