Results 1 to 5 of 5
Thread: Linked List Question
- 12-10-2008, 02:16 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
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();
listA.insertAtFront("J");
listA.insertAtFront("F");
listA.insertAtFront("E");
listA.insertAtFront("D");
listA.insertAtFront("C");
listA.insertAtFront("B");
listA.insertAtFront("A");
//Prints listA from head to tail
listA.Print();
//Create a physcial copy of the first linked list but in backwards order
//called ListB
}
}
Any help would be appreciated
- 12-10-2008, 02:30 AM #2
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
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.
- 12-10-2008, 02:37 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
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
linkB.Print();
Any suggestions?
- 12-10-2008, 06:48 AM #4
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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.Neil Coffey
Javamex - Java tutorials and performance info
- 12-10-2008, 06:56 PM #5
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
Ok,
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
-
Help with Doubly linked list
By Dr Gonzo in forum New To JavaReplies: 5Last Post: 12-06-2008, 07:45 AM -
Linked List Manipulation
By chrisdb89 in forum New To JavaReplies: 3Last Post: 11-21-2008, 06:07 PM -
Linked List help
By neobie in forum New To JavaReplies: 8Last Post: 12-22-2007, 03:15 AM -
Linked List
By rnavarro9 in forum New To JavaReplies: 0Last Post: 11-29-2007, 03:42 AM -
Help with linked list
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks