Creating A Reverse List - Integer - Using Recursion

Jan 2, 2015

I created this code, seems like it should be working properly and the code is right, but the output is not what it should be.The action gets a List<Integer> and should make it reversed.

As example: 1 2 3 4 5 ---> 5 4 3 2 1

The Nodes in the new list (lst2) should be in a reversed position.The code does compile with no errors.I do have Node<T> and List<T> classes

Java Code:

public class NodeReverse
{
public static void reverse (List<Integer> lst, Node<Integer> node, int counter, List<Integer> lst2, Node<Integer> pos) // Should make lst = lst2 while lst2 is the opposite to lst
{
node = node.getNext();
counter++;
if(node.getNext()!=null)
reverse(lst, node, counter, lst2, pos);

[code]....

View Replies


ADVERTISEMENT

Creating Palindromes Using Recursion?

Oct 20, 2014

I just started studying recursion and I wanted to know how to create a palindrome number going up from 1 to n then back to 1 like this: "12345...n...54321".

I've done one going downwards and then upwards like this: "n...4321234...n".

Here's my code:

Java Code: import java.util.*;
public class PalindromeTest
{
public static void downPalindrome(int n)

[Code].....

View Replies View Related

Simple Recursion Method - Accept Integer Parameter N And Return Sum Of First N Reciprocals

Nov 1, 2014

I am practicing some basic recursion and I was trying to solve this problem

Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words:

sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n

For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should throw an IllegalArgumentException if passed a value less than 0.

This is my attempt to do it , however my output is always a 0.0 , and i do not understand why :

public static double sumTo(int n ){
if(n<0){
throw new IllegalArgumentException();
}
else if (n==0){
return 0.0;

[Code] .....

View Replies View Related

Creating A Method That Reverse Digits Of Numbers

Feb 23, 2015

I am trying to create a method that reverses the digits of a number.

import java.util.*;
public class KDowlingWeek7 {
static Scanner console = new Scanner (System.in);
 //Scanner input = new Scanner(System.in);
public static void main(String[] args) {

[Code] .....

The error I get is :

Error: cannot find symbol System.out.print(reverseDigit + " ");
Symbol: variable reverseDigit
Location: class KDowlingWeek7

View Replies View Related

List All Possible Paths From Point A To B Using Recursion?

Apr 12, 2014

From a two-dimensional grid 5x5 that looks like this:

(0,0)(0,1)(0,2)(0,3)(0,4)
(1,0)(1,1)(1,2)(1,3)(1,4)
(2,0)(2,1)(2,2)(2,3)(2,4)
(3,0)(3,1)(3,2)(3,3)(3,4)
(4,0)(4,1)(4,2)(4,3)(4,4)

We have Starting point that is (3,0) and an ending point is (1,3). We can only move up and right to get to the ending point by using recursion. We have to list all possible paths from (3,0) to (1,3)

Example: paths:(3,0)(2,0)(1,0)(1,1)(1,2)(1,3)
(3,0)(2,0)(2,1)(1,1)(1,2)(1,3)
etc...

I was able to get from (3,0) to (1,3) but how to list the other paths. This is my code so far

public class Program7 {
public static void main(String[] args){

int size = 5;

int x1 = 3;
int y1 = 0;
int x2 = 1;
int y2 = 3;

System.out.println(x1+" "+y1);
System.out.println(x2+" "+y2);

int [][] path = new int[size][size];
grid(path,x1,y1,x2,y2);

[code].....

View Replies View Related

How To List All Possible Paths From Point A To B By Using Recursion

Apr 12, 2014

From a two-dimensional grid 5x5 that looks like this:

(0,0)(0,1)(0,2)(0,3)(0,4)
(1,0)(1,1)(1,2)(1,3)(1,4)
(2,0)(2,1)(2,2)(2,3)(2,4)
(3,0)(3,1)(3,2)(3,3)(3,4)
(4,0)(4,1)(4,2)(4,3)(4,4)

We have Starting point that is (3,0) and an ending point is (1,3). We can only move up and right to get to the ending point by using recursion. We have to list all possible paths from (3,0) to (1,3)

Example: paths:(3,0)(2,0)(1,0)(1,1)(1,2)(1,3)
(3,0)(2,0)(2,1)(1,1)(1,2)(1,3)
etc...

I was able to get from (3,0) to (1,3) but how to list the other paths. This is my code so far

public class Program7 {
public static void main(String[] args){
int size = 5;
int x1 = 3;
int y1 = 0;
int x2 = 1;
int y2 = 3;
System.out.println(x1+" "+y1);
System.out.println(x2+" "+y2);

[Code] ....

View Replies View Related

Recursively Reverse Linked List?

Oct 17, 2014

i tried everything but its giving me errors. i tried the for loop but its giving me something else.

this is what i have to do Write a recursive method that prints out the data elements of a linked list in reverse order.

Your method should take in as a parameter the head reference to a linked list. Then, write a recursive method that returns a count of the number of elements greater than a given threshold. You may assume that the data elements in the linked lists are ints. The parameters of your method are a reference to the linked list and a int value representing the threshold.

public class recursion3
{
public static void main(String [] args) {
char a [] = {'A', 'B','C','D','E'};
System.out.println(a);
}
public static String reverseString(String s) {
if (s.length() <= 1) {

[code]....

View Replies View Related

Java Linked List Reverse

Apr 12, 2014

I am creating a recursive method to reverse a linked list in java. It works the first time when I call it, but I want it to work where I call it a second time and it reverses it to the original state. How can I get that to work with this implementation?

public void reverseList() {
System.out.printf("%-16s%-3s%n", "Name", "Score");
System.out.println("--------------- -----");
reverseList(first);
} public void reverseList(Node aNode) {
if (aNode != null) {
reverseList(aNode.next);
System.out.printf("%-15s%6s%n" , aNode.name , aNode.score);
}
}

View Replies View Related

Implement And Compare Reverse Methods From List

Oct 5, 2014

What approach Collections.reverse() uses to reverse list? My task is to implement and compare reverse methods from list and i already did reveres with Recursion, Swap, Reading backward with creating reversed copy.

Does Collections.reverse() use different approach from those i already did?

View Replies View Related

Finding And Printing Median Node In Linked List Using Recursion

Nov 3, 2014

The only problem I have now is getting a method to return the median element of a LinkedList without using loops of any kind or by using a global counter anywhere.

I've pretty easily figured out how to get the index value for the median number (there is some lee way allowed. If the list has an even size, any of the middle values are accepted) but I can't figure out how to print it without loops.

I'm sure I need to make a method that finds an element at the given index value, but I don't know how to do it without loops.

Here's all of my code. Inside is my Assignment3 class I use for testing, StudentList which contains the LinkedList head and other List methods, and StudentNode which is obviously, the Node class. Also I've attached the first test1.txt file as well.

import java.io.FileNotFoundException;
import java.util.*;
public class Assignment3 {
public static void main (String []args){
StudentList<StudentNode> myList = new StudentList<StudentNode>();

[Code] .....

I tried making a method that basically counts up the list recursively then a second method that counts down recursively and is supposed to stop once it hits the middle number, then print that node.

Attached File(s) : test1.txt (106bytes)

View Replies View Related

Reverse Double Linked List In Java - Descending Order

Sep 12, 2014

I have the following double linked list and I'm supposed to order it descending (reverse) using the printInReverse() method; since the list orders itself ascending when the numbers are added, how could I order it descending in this method? Here's the code without implementing descending/reversing methods:

package test;
import java.util.Scanner;
public class MyList {
private Scanner userInput;
private Integer selectedOption;
private MyListElement firstElement = null;
private boolean exitRequested = false;

[Code] ....

I tried to declare a previousElement variable but I didn't figure out how I'd do that.

View Replies View Related

Convert Integer List To Int Array?

Sep 24, 2008

is there a way, to convert a List of Integers to Array of ints (not integer). Something like List<Integer> to int []?

View Replies View Related

Removing (Integer) From A Generic List?

Oct 3, 2014

I am working on a java program that is called OrderedVector which is basically a storage or list that grows and shrinks depending on the amount of data is put in. Most of the methods in my code are correct and working, the only real issue I have lies with either the remove(E obj) method or remove(int index) method. This is the driver I am currently using to test my remove method,

public class Tester {
public static void main(String[] args) {
OrderedListADT<Integer> v;
v = new OrderedVector<Integer>();
for(int i = 0 ; i <= 9; i++){
v.insert(i);

[code]....

the output I am receiving is

Removing 0
Size of data structure is 9
Removing 1
Size of data structure is 8
Removing 2
Size of data structure is 7

[code]....

As you can see, when I am calling the second for loop, none of the elements are being removed by my methods but the first for loop is working just fine.

Here is my code for the OrderedVector

package data_structures;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class OrderedVector<E> implements OrderedListADT<E>{
private int currentSize, maxSize;
private E[] storage;
public OrderedVector(){
currentSize = 0;

[code]....

So overall, my remove method should implement binary search and remove elements using either an index or the object value type.

View Replies View Related

Read String To List (Integer) And Back

Feb 14, 2014

I am looking for a good and reliable library to read a string to construct a list of Integers, Doubles, Booleans, etc. This library should be robust enough to handle faulty input from an inexperienced user.

Example:

input: "1, 2, 3, 4"
output List<Integer> [1, 2, 3, 4]

input: "1, 2, 3.6, 4"
output List<Double> [1.0, 2.0, 3.6, 4.0]

input: "true, true, false"
output List<Boolean> [true, true, false]

input: "[1, 2, 3]"
output List<Integer> [1, 2, 3]

input: "(1, 2, 3)"
output List<Integer> [1, 2, 3]

It would be really nice if such a library would already exist.

View Replies View Related

Searching Though Linked List Given And Displaying It Given Integer

Nov 1, 2014

I need to display an object, which is in a linked list, given a vin number. It is the display_info method. Here is what i have so far:

import java.util.*;
public class Car implements Comparable<Car>, Cloneable
{

private String model;
private String color;
private int year;
private int vin_number;
private double price;

[code]....

View Replies View Related

Creating A Score List And High Score List?

Jan 7, 2015

I am creating a program called "Mad Math Machine". This program is to generate random arithmetic questions, with integers ranging from -12 to 12, for the user to answer. The user has three lives. Once they get more than three questions wrong, they run out of lives, and their "score" (the number of questions they answered correctly) is taken.

I have the large chunk of the program (the arithmetic questions and answers) completed. The only part I am stuck on is the scoring system. I believe that arrays would be useful for this task, but I have little idea of where to start. Here is what the scoring list should be able to do:

- It should print out the top ten recent players and their scores.
- A separate list should show the top-two highest scores of all time.

*I didn't think it was relevant, but I can include the program code. Up until this point, I have created methods to keep track of the score, but I have simply left them commented out so I could build the rest of the program.*

View Replies View Related

Swing/AWT/SWT :: Creating Non Hard Coded ComboBox List?

Sep 17, 2014

I need to display a list of environment id's out of my xml file into a JComboBox. I understand fine how to make lists and what not but examples are always hard coded.

View Replies View Related

Accept List Of Integers As Parameter And Return Number Of Times Most Frequently Occurring Integer

Nov 20, 2014

Write a method maxOccurrences that accepts a list of integers as a parameter and returns the number of times the most frequently occurring integer (the “mode”) occurs in the list. Solve this problem using a map as auxiliary storage.

If the list is empty, return 0.

View Replies View Related

Can't Seem To Get Value For Integer 1 And Integer 2 To Pass With SetValue Method

Aug 10, 2014

public class MyInteger {
private int value;
public MyInteger(int number){
value = number;
System.out.println("Constructor created with value of " + value);

[code]....

I can't seem to get the value for integer1 and integer2 to pass with the setValue method. I get a runtime error stating the I need to have an int value for these two integers.

View Replies View Related

Recursion On Initialization

Apr 29, 2014

I am trying to make a game, for some reason i have begun to get a java.lang.StackOverflowError.

I am not exactly sure how i can fix it. only removing line 14 from infopannel1 (and everything that used that class.) seems to work. im not sure what else i can do to fix it. or why its resulting in stack overflow for that matter.

I am putting in a link for the files i wrote this using bluej (several classes have no relevance, errorv2, demonstration, folderreadertest, ReadWithScanner, saveloadtest, menutest,rannum, and menutestTester. are all irrelivent to my problem.)

[URL] .....

View Replies View Related

How Does Recursion Work

Dec 13, 2014

how does recursion works, I don't understand why it prints al numbers going up again?This is the code

void print2(int n){
if (n<0){
out.printf(" %d",-1);
return;
}
out.printf(" %d", n);
print2(n-1)
out.printf(" %d", n);
}

this should be the output if n is 6: 6 5 4 3 2 1 0 -1 0 1 2 3 4 5 6.

View Replies View Related

Sum Of Array By Recursion

Mar 25, 2015

How to add the sum of an array with a recursion, but I don't understand how to use recursion. I just understand that it calls back the method. I am nearly done with the code.

import java.util.Scanner;
class Question1{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
int size, sum;
System.out.println("Please input how many numbers will be used");
size=s.nextInt();

[Code] .....

View Replies View Related

Recursion And Lists?

Aug 6, 2014

public void myFunc(MyNode n, ArrayList<MyNode> path) {
boolean hasChildren = false;
path.add(n);
int index = path.indexOf(n);
ArrayList<MyNode> statefulPath = new ArrayList<MyNode>();

[Code] ....

I have similar code that I stepped through in a debugger. After running the code I found that it built the desired tree, in this case a root node H with left child L and right child P. I want list of lists to contain all paths from root to leaf. I expected [H, L] and [H, P]. I discovered that statefulPath is not stateful; after a recursive stack frame pops, statefulPath still contains n! But that stack frame just popped! I expected to see statefulPath be [H] in the debugger and it was [H, L]! So I later have a list [H,L,P] which I don't want. How do I make the statefulPath list I want for my algorithm?

View Replies View Related

Increment Counter By Recursion

Mar 22, 2015

I have a question related to the code below, that I do not understand. The aim is to count all files and subdirectories in an ArrayList full of files and subdirectories. So I have to count every file and every subdirectory.

The code concerning counting files is clear to me - every time d is of the type file I have to increment n by one. However I thought that I have to do the same thing in case d is a directory, so I would have written the same code for directories.

So what does "n += ((Directory) d).countAllFiles();" mean? In my understanding the method countAllFiles() is applied again on the object Directory ( as Directory is the class that contains this method), but how is n incremented by this? I thought n should be incremented by one as we did with files.

public int countAllFiles() {
int n = 0;
for(SystemFile d : content) {
if(d instanceof File) {
n++;

[Code] ....

View Replies View Related

How Recursion Calls Are Made

Jun 5, 2014

How the recursion works. I tried to figure out writing down low, mid, high at each recursive call. But I seem to be making a mistake somehow. I don't understand where the values are returned to in

if(leftmax>rightmax){
return leftmax;}
else
return rightmax;

Here's the code:

public class Maximum{
public static int max(int[] a,int low,int high){
int mid,leftmax,rightmax;
if(low==high)
return a[low];

[Code] ....

firstly leftmax=max(a,0,4)

Then what is the next line executed?Is it rightmax=max(a,5,8).

After this is it leftmax=max(a,5,6)

rightmax=max(a,7,8)

I tried to understand what these recursion calls by writing them down.But I somehow make a mistake.

View Replies View Related

Printing Pattern Using Recursion

Jul 16, 2014

The program I'm working on is supposed to read input from a file and using recursion, print the pattern of asterisks for each value until the value is either < 0 or > 25.For example, if the value was 4, the pattern would look like this

*
* *
* * *
* * * *
* * *
* *
*

The values are stored in a file entitled prog3.dat which looks like this

4
3
15
26
0

I've never used recursion before and haven't been able to find anything showing how it would work with this particular type of problem.Here is what I've been able to come up with so far, but I'm having problems still which I will show following the code.

import java.util.Scanner;
import java.io.*;
public class Program3 {
public static void main(String[] args) throws Exception {
int num = 0;
java.io.File file = new java.io.File("../instr/prog3.dat");
Scanner fin = new Scanner(file);

[code]...

It appears to be reading the file correctly, but is only printing the top half of the pattern. Also, like I said, I'm not very familiar with recursion, so am not sure if this is actually recursion?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved