2 changed files with 44 additions and 29 deletions
@ -0,0 +1,2 @@ |
|||
*.class |
|||
/meghanadaFormatter.xml |
|||
@ -1,46 +1,59 @@ |
|||
// The RecursiveMethods class that implements several recursive solutions |
|||
// Your name here |
|||
|
|||
public class RecursiveMethods |
|||
{ |
|||
// This method calls the sumSquareRec method and returns the sum of the |
|||
public class RecursiveMethods { |
|||
// This method calls the sumSquareRec method and returns the sum of the |
|||
// squares of the elements in the array. |
|||
public int sumSquares(int[] A) |
|||
{ |
|||
public int sumSquares(int[] A) { |
|||
// Do not make any changes to this method! |
|||
return sumSquaresRec(A, 0); |
|||
} |
|||
} |
|||
|
|||
// This method takes an integer array as well as an integer (the starting |
|||
// This method takes an integer array as well as an integer (the starting |
|||
// index) and returns the sum of the squares of the elements in the array. |
|||
public int sumSquaresRec(int[] A, int pos) |
|||
{ |
|||
// TODO: implement this method |
|||
|
|||
return -1; // replace this statement with your own return |
|||
} |
|||
public int sumSquaresRec(int[] A, int pos) { |
|||
|
|||
int current_int = A[pos]; |
|||
if (pos + 1 == A.length) |
|||
return current_int * current_int; |
|||
return current_int * current_int + sumSquaresRec(A, pos + 1); |
|||
} |
|||
|
|||
// This method takes a character stack and converts all lower case letters |
|||
// to upper case ones. |
|||
public void upperStackRec(CharStack s) |
|||
{ |
|||
// TODO: implement this method |
|||
public void upperStackRec(CharStack s) { |
|||
if (s.isEmpty()) |
|||
return; |
|||
else { |
|||
char top = Character.toUpperCase(s.pop()); |
|||
upperStackRec(s); |
|||
s.push(top); |
|||
} |
|||
} |
|||
|
|||
|
|||
// This method reads a string and returns the string in the reversed order. |
|||
public String reverseStringRec(String s) |
|||
{ |
|||
// TODO: implement this method |
|||
|
|||
return "dummy string"; // replace this statement with your own return |
|||
public String reverseStringRec(String s) { |
|||
|
|||
if (s.equals("")) |
|||
return s; |
|||
return reverseStringRec(s.substring(1)) + s.substring(0, 1); |
|||
|
|||
} |
|||
|
|||
|
|||
// This method takes a reference to the head of a linked list. |
|||
// It returns the reference to the head of the linked list in the reversed order. |
|||
public LNode reverseListRec(LNode head) |
|||
{ |
|||
// TODO: implement this method |
|||
|
|||
return new LNode(-1); // replace this statement with your own return |
|||
public LNode reverseListRec(LNode head) { |
|||
if (head == null) |
|||
return head; |
|||
if (head.getLink() == null) |
|||
return head; |
|||
|
|||
LNode holder = reverseListRec(head.getLink()); |
|||
LNode next = head.getLink(); |
|||
|
|||
next.setLink(head); |
|||
head.setLink(null); |
|||
return holder; |
|||
|
|||
} |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue