4 Replies - 516 Views - Last Post: 28 December 2012 - 12:11 PM Rate Topic: -----

#1 RourkeOkeffe  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 89
  • Joined: 27-October 12

LinkedList in reverse order recursion

Posted 28 December 2012 - 09:50 AM

Hey Guys I'm revising for a test.The question asks as follows to complete the method.That prints a singly linked list backwards.Using the printBackward method which takes the length of the list as a parameter.
Once you have completed the method create a simple program to print the list forward and then in reverse order using the
printBackward method

I am not sure how to implement this is what I have so far any help would be greatly appreciated.


import java.util.LinkedList;
import java.util.ListIterator;

@SuppressWarnings("serial")
public class MyLinkedList<E> extends LinkedList<E> {

	public void printBackward(Integer n) {

		if (n > 0) {
			ListIterator<E> itr = listIterator();
			int count = 0;

			while (itr.hasNext()) {
				//Block 1

			}
			//

		} else {
			//Block 3
		}

	}// end method

	public static void main(String[] args) {
		LinkedList<Integer> myList = new LinkedList<Integer>();
		myList.add(34);
		myList.add(65);
		myList.add(27);
		myList.add(89);
		myList.add(12);
		
			System.out.println("myList: "+myList);
		

		//printBackward(0);

	}
}// end class



Is This A Good Question/Topic? 0
  • +

Replies To: LinkedList in reverse order recursion

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10195
  • View blog
  • Posts: 37,654
  • Joined: 27-December 08

Re: LinkedList in reverse order recursion

Posted 28 December 2012 - 10:27 AM

See the ListIterator documentation. You could iterate n spaces forward, then go backwards and print out the elements. Or you could just iterate forward and store the elements on something like a Stack.

Also, use primitives instead of the wrappers whenever possible, as it is more efficient.
Was This Post Helpful? 0
  • +
  • -

#3 RourkeOkeffe  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 89
  • Joined: 27-October 12

Re: LinkedList in reverse order recursion

Posted 28 December 2012 - 10:32 AM

It says I must have a dimension to make a linkedlist using int which i don't know why it says this.
Was This Post Helpful? 0
  • +
  • -

#4 RourkeOkeffe  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 89
  • Joined: 27-October 12

Re: LinkedList in reverse order recursion

Posted 28 December 2012 - 11:05 AM

View Postmacosxnerd101, on 28 December 2012 - 10:27 AM, said:

See the ListIterator documentation. You could iterate n spaces forward, then go backwards and print out the elements. Or you could just iterate forward and store the elements on something like a Stack.

Also, use primitives instead of the wrappers whenever possible, as it is more efficient.

How could I adapt what i have to go the end then traverse back
public class MyLinkedList<E> extends LinkedList<E> {

	public void printBackward(Integer n) {

		if (n > 0) {
			ListIterator<E> itr = listIterator();
			int count =n;
			
			E item;

			while (itr.hasNext()) {
				//Block 1
				

			}
			//

		} else {
		System.out.println("List is empty or end of list reached");
		}

	}// end method

	public static void main(String[] args) {
		myList<int> = new LinkedList<E>();
		myList.add(34);
		myList.add(65);
		myList.add(27); 
		myList.add(89);
		myList.add(12);
		System.out.print(myList.printBackward(myList.size()));
		
			System.out.println("myList: "+myList);
		

		//printBackward(0);

	}
}// end class


This post has been edited by macosxnerd101: 28 December 2012 - 12:51 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: LinkedList in reverse order recursion

Posted 28 December 2012 - 12:11 PM

Seems to me overcomplicated for nothing

   void printBackward(int n) {
    	if(n < (size() - 1))
    		printBackward(n + 1);
    	System.out.println(get(n));
    }


should do the trick. Just call printBackward(0);

This post has been edited by pbl: 28 December 2012 - 01:38 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1