We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,397 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

printing a list backward

hi, i am trying out past year questions for a resit and am stuck with the question below.
Question 1

In the following you may assume the existence of the ListIterator<e> interface and LinkedList<E> class with the following methods

public interface ListIterator<E>
{
  E next();

  boolean hasNext();

}




public class LinkedList<E>
{

  public void addLast(E obj){..}

  public int size(){..}

  public ListIterator<E> listIterator(){...}

}

Finished the design of the printBackward method given below using the methods listed above in the ListIterator<E> interface and LinkedList<E> class. You should not introduce any new variable tinto the method. In your answer, do not copy the whole method. Write contents of the Initialisation 1, Initialisation 2, Block 1, Block 2, Block 3 . The printBackward method should be written recursive in a singly list backward. The parameter n specifies the size of the list.

public class MyLinkedList<E> extends LinkedList<E>
{

           public void printBackward(int n)
           {

             if(n > 0){

              ListIterator<E> itr = /**Initialisation 1**/ list1.listIterator();

              int count = /**Initialisation 2**/  0;

              E item;

              while(itr.hasNext())
              {
                /**Block 1**/  addLast(list1); printBackward(); count --;

              }

                /**Block 2**/ E.next;
             }else

             /**Block 3**/ return;
           }
         }
         }

I have inserted my answers next to the /** ..**/ but am unsure if they are correct. It would be much appreciated if someone could help me correct my mistakes

5
Contributors
10
Replies
1 Day
Discussion Span
8 Months Ago
Last Updated
11
Views
scarletfire
Light Poster
27 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

What happens when you compile and execute the code?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

The printBackward method should be written recursive in a singly list backward.

That's the keyword, recursive. :) Your implementation is not recursive but rather iterative. Do you know what recursive is?

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
 public void printBackward(int n)
     ...
     addLast(list1); printBackward(); count --;

Despite the missing parameter that looks like an attempt at recursion to me :)

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

I think I did not grasp the basics of recursion properly, I could write simple recursion code but when it is anything to do with linkedList, I am just no good...

scarletfire
Light Poster
27 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

am cramming for an exam tomorrow

Ooops! Probably better to spend your time on a few other topics because you could burn a lot of hours trying to get this one straight.

ps: Anyone else know a good answer to this one? I can't see any sensible way to solve it within all the contraints.

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

I am sorry that your recursive call is actually creating an infinite loop. From what I see there, my first thought about printBackward() method is that it would return a string. But then I saw addLast(), I believe that you would need to reverse the list before you manipulate with the list. Below is what I think what should happen...

ListIterator<E> itr = /**Initialisation 1**/ list1.listIterator();
LinkedList<E> backwardList = /**Initialisation 2**/ new LinkedList<E>();
createBackwardList(itr, backwardList);
// now the backwardList is containing backward linked list data

private void createBackwardList(ListIterator<E> itr, LinkedList<E> list) {
  if (itr.hasNext()) {    // has next item
    E item = itr.next();  // extract the item
    printBackward(itr, list);  // keep looking for the last item
    list.addLast(item);   // add back
  }
}

Look at the method createBackwardList(), it is a simple recursive method accepting the original list iterator and an empty list at first.

First, it check whether the iterator still contains more item, if so, get the item from the iterator and keep calling until it finds nothing in the list. Once it find nothing, each of the item that is extracted from the list will be added back to the empty list one by one from the last one it visited. Once it is done, the original empty list will be filled with backward list. Hope this help you understanding recursive...

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

"You should not introduce any new variable tinto the method. In your answer,", and it also imples no changes to the signature of the method, otherwize your solution is the (only) one I was considering

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

cool i don't even understand what it is. You are such a great person you have understand this computer language. :)

sevilleaudrey1
Newbie Poster
1 post since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Hmm... I don't know if what the code portion he gave was the original... I mean I would rather want to see the empty shell of the code to work with. :) But well, that's one way I would solve the problem. :)

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

Duplicate post http://stackoverflow.com/questions/12110190/print-backward-method-for-a-singly-linked-list-using-recursion
Daniweb member rules include "Do not post articles that you have already published on another website"
@scarletfire: This is just a warning. Do not do it again!

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0931 seconds using 2.78MB