Results 1 to 5 of 5
  1. #1
    CirKuT is offline Member
    Join Date
    Sep 2008
    Rep Power

    Default Linked List Question

    Hello Everyone,

    Im learning about linked lists and have been practicing a bit from a book. I have created a linked list and now I want to make a copy of this linked list but in backwards other.

    When I create a copy of listA and name it listB I need to be able to print them like this. My book does not cover copying linked lists.

    linked list A: 1,2,3,4,5
    linked list B: 5,4,3,2,1

    Below is one class in my three class package. The very bottom deals with added data to linked list A from there is where I need to create a copy. If you need me to post the other 2 classes I can but they just contain methods to insert and delete from the front or back of the list etc etc etc

    package LinkedList;

    * @author CirKuT
    public class ListTest {

    public static void main (String args[]){

    //Creates a Linked List
    List listA = new List();


    //Prints listA from head to tail

    //Create a physcial copy of the first linked list but in backwards order
    //called ListB



    Any help would be appreciated

  2. #2
    Rajesh_J2EE is offline Member
    Join Date
    Dec 2008
    Rep Power


    Since you had already wrote the code for push operation, now write the code for pop, while doing the pop operation just copy each element into second list, or you can write a method to reversing a linked list, call that method and assign it new list.

  3. #3
    CirKuT is offline Member
    Join Date
    Sep 2008
    Rep Power


    I would prefer to use a method but how exactly do I go about doing this? Im still rather new to Java so I still have problems writing all my own code.

    Since my first linked list is named linkA like shown below,

    Link linkA = new Link();

    To created the second list I just used this line

    Link linkB = linkA;

    Now I just need to reverse it before I use this method to print it


    Any suggestions?

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Rep Power


    Two simple solutions, depending on how you've created your list class:
    (1) make your class implement a DOUBLY linked list, where each item in the list "points" to both the next and *previous* item, and where the list always contains a reference to both the first and last items -- then it's trivial to just start at the end of the list, and follow the "pointers" backwards to get the items in reverse order;
    (2) if your pointers only go forwards, then copy all the items into an array, then copy the items out of the array from end to beginning to get them into a new list in reverse order.

  5. #5
    CirKuT is offline Member
    Join Date
    Sep 2008
    Rep Power



    So can I copy to an array the same as I created the listB?

    All I did was take listA and do this

    List listB = listA;

    So if I had an array named arrayC could I just go like this

    arrayC = listA; ???? would that work??

    If so then how would I copy each element of the new array into the new linked list listB??

    I really could use an example on how to do this in the form of syntax.

    Thanks everyone for your help

Similar Threads

  1. Help with Doubly linked list
    By Dr Gonzo in forum New To Java
    Replies: 5
    Last Post: 12-06-2008, 07:45 AM
  2. Linked List Manipulation
    By chrisdb89 in forum New To Java
    Replies: 3
    Last Post: 11-21-2008, 06:07 PM
  3. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 03:15 AM
  4. Linked List
    By rnavarro9 in forum New To Java
    Replies: 0
    Last Post: 11-29-2007, 03:42 AM
  5. Help with linked list
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts