Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: copy() to copy the values of singly linked list1 to another linked list

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Thanked 0 Times in 0 Posts

    Default copy() to copy the values of singly linked list1 to another linked list

    I have written copy() method to copy the data of one linked list to another. However, i am not able to get the output i really need help on this. i really need to know where the error is as the issue is its not displaying the output:

    public class Link

    public int iData; // data item

    public Link next; // next link in list
    // -------------------------------------------------------------
    public Link(int id) // constructor
    iData = id;

    // -------------------------------------------------------------
    public void displayLink() // display ourself
    System.out.print("{" + iData +"}");

    } // end of class

    public class LinkList

    private Link first; // reference to first link on list
    private Link last; // REFERENCE TO LAST LINK ON LIST
    // -------------------------------------------------------------
    public LinkList() // constructor
    first = null; // no items on list yet
    // -------------------------------------------------------------
    public boolean isEmpty() // true if list is empty
    return (first==null);
    // -------------------------------------------------------------
    // insert at start of list
    public void insertFirst(int id)
    Link newLink = new Link(id);
    newLink.next = first; // newLink --> old first
    first = newLink; // first --> newLink
    // -------------------------------------------------------------
    public LinkList copy()
    LinkList L5 =new LinkList();
    Link current= first;

    int x;

    while(current!=null) // i think something is wrong here

    return L5;

    public static void main(String[] args)
    LinkList L1 = new LinkList(); // make new list
    L1.insertFirst(22); // insert four items
    L1.displayList(); // display list

    LinkList Lcopy=new LinkList();
    System.out.println("-----------------Copied data in new list is:------------");

    the output is :

    List (first-->last): {99}{88}{66}{44}{22}
    -----------------Copied data in new list------------
    List (first-->last):

    the values are not getting copied to another linked list.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,465 Times in 2,416 Posts

    Default Re: copy() to copy the values of singly linked list1 to another linked list

    How are you trying to debug the code? Add some print statements in the copy method that print out the contents of variables as the method is executed.

    Please edit your post and wrap your code with code tags:


    to get highlighting and preserve formatting.

    the values are not getting copied to another linked list.
    How does the copy method know what to copy?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with singly linked list.
    By jackzorz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 29th, 2014, 09:02 PM
  2. Remove at index in singly linked list?
    By EDale in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 3rd, 2013, 09:47 PM
  3. Singly Linked List
    By koala711 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2012, 02:15 PM
  4. Singly-Linked list structure
    By blaster in forum Algorithms & Recursion
    Replies: 24
    Last Post: March 11th, 2012, 03:42 PM
  5. Singly Circular Linked List Error
    By clydefrog in forum Collections and Generics
    Replies: 7
    Last Post: March 5th, 2012, 08:17 PM