Been working on this for 2 hours non stop.

Can't seem to figure out exactly what's the problem. Perhaps I'm taking a wrong approach to this problem.

Right, I need to copy a singly linked list to another new list. Can use any method EXCEPT RECURSION.

Here's the code so far:

public class copyList
{
    public static void main (String args[])
    {
        new copyList ();
    }


    String x;
    private node head1 = new node ();
    private node tail1 = head1;
    private node head2 = new node ();
    private node tail2 = head2;

    public copyList ()
    {
        //LIST 1
        head1.w = "Apples";

        tail1.next = new node ();
        tail1 = tail1.next;

        tail1.w = "Chips";

        tail1.next = new node ();
        tail1 = tail1.next;

        tail1.w = "Milk";

        tail1.next = new node ();
        tail1 = tail1.next;

        tail1.w = "Paper";

        tail1.next = new node ();
        tail1 = tail1.next;

        tail1.w = "Pens";

        tail1.next = new node ();
        tail1 = tail1.next;
        //----------------------------------------------------------------
        //keep popping into new list
        node current2 = head2;
        int count = 0;
        while (current2.next != null)
        {
            x = "";
            //so it adds the first value to head when count = 0; but after incrementing, it should add to the tail.
            if (count == 0)
            {
                pop ();
                addToHeadNew ();
                count++;
                current2 = current2.next;
            }
            else
            {
                pop ();
                addToTailNew ();
                current2 = current2.next;
            }

        }
        //----------------------------------------------------------------
        //Print both lists
        print (head1);
        System.out.println ("");
        print (head2);

    }


    //add to the head of the list. Only for the first value.
    public void addToHeadNew ()
    {
        node temp = new node ();
        temp.w = x;
        temp.next = head2;
        head2 = temp;
    }


    //add to the tail of the list
    public void addToTailNew ()
    {

        tail2.w = x; //the value stored in pop
        tail2.next = new node ();
        tail2 = tail2.next;
    }


    //store the value to be copied
    public void pop ()
    {
        //check value but dont delete link.
        node temp = head1;
        if (temp.w != null)
        {
            temp = head1;
            x = temp.w; //to be used later
            temp = temp.next;
        }
    }


    //print
    public void print (node head)
    {
        node current = head; //keeps track of our current position. like a counter.

        int count1 = 0;
        while (current.next != null)
        {
            System.out.println (count1 + 1 + ". " + current.w);
            current = current.next;
            count1++;
        }
    }
}

Any help is appreciated. I just want this headache to go away...

Edited 6 Years Ago by insanely_sane: n/a

1)What does your 'node' class look like?
2)Because you use both 'head' and 'tail' while you don't completely understand linked list, you make a big mistake when you create a linked list.
3)You are not supposed to create a linked list inside its class! Do the creation outside the class. Don't be lazy...
4)Most of your implementation are wrong...
-> The addToHead() is supposed to take an argument of the new node!
-> The addToHead() is supposed to check for null value of head!
-> The addToTail() is supposed to take an argument of the new node!
-> The addToTail() is supposed to check for null value of head!
-> The print() is not printing out the last node!
-> The pop() function is supposed to destroy the last item from the link list. NEVER keep it to use later! You will make the list even more complicated to maintain!

public static void main (String args[]) {
    copyList list1 = new copyList();
    copyList list2 = new copyList();
    list1.addToTail("Apples");
    list1.addToTail("Chips");
    list1.addToTail("Milk");
    list2.copy(list1);  // one way to implement the copy
  }

  private node head;
  private node tail;

  // constructor
  public copyList () {
    head = null;
    tail = null;
  }

  public void addToHead(String item) {
    if (head==null) {
      head = new node();
      head.w = item;
      tail = head;
    }
    else {
      node n = new node();
      n.w = item;
      n.next = head;
      head = n;
    }
  }

  public void addToTail(String item) {
    // similar to head,
    // you need to check whether or not head is null
    // if it is null, same thing as addToHead()
    // if it is not null, then you need to create and hook the new node with tail
    // but remember that the new node must be the last node!
  }

  public void pop() {
    // check if tail is null
    // if tail is null, do nothing
    // else if head==tail, assign null to both
    // else iterate through head until the one before the tail
    // move the tail reference to the one before the tail
  }

  // need to get head for copying
  public node getHead() {
    return head;
  }

  public void copy(copyList list) {
    if (list.getHead!=null) {
      // delete all old list of this own variable before copying
      // or you could let garbage collector do the job later by doing nothing (memory is released later)

      // create new node here
      // copy the value of the new node for head, and assign tail to head (same node)
      // iterate through each node of incoming list starting from the one next to the head
      // each iteration, add the new item to own class's tail (can use addToTail(item))
      // Once the iteration is done, the linked list is copied
    }
  }

  // If you are going to use System.out.println(listVariable),
  // then implement toString() to print out for you!
  // This method could replace your print()
  public String toString() {
    String out = "";
    node current = head;  // head is a class variable, so no need to pass it in
    int count = 1;
    while (current!=null) {  // node is not null, not the next of the node is null
      out += count + "." + current.w;
      current= current.next;
      count++;
    }

    return out;
  }

Edited 6 Years Ago by Taywin: n/a

uhhh because Im been taught this way :/ Im just in 12th you know :/ Thanks for the tips anyways. And my node class is:

public class node
{
    String w;
    node next;
}

The reason I don't delete the links in the original list is because I want to print it out as well. Now I very well can't do that if everything is destroyed.
As for the printing... it works fine for me :/ prints out everything! And Yes I don't COMPLETELY understand linked lists but that is the only way I know to create one.
And if you think it's a huge mistake, teach me the right way.
Like I said, I might be approaching this wrongly and I'll try to implement whatever suggestions you have made.
Thanks for replying. Marking this as solved. Don't think anyone else would be replying.

EDIT: I don't like your way. It's not for beginners in linked list. Next time.. please do explain the "one way to implement the copy" since I for one was not familiar with the .copy method. Nor do I understand why you use copyList List1 and so on :/

Edited 6 Years Ago by insanely_sane: n/a

Sorry for the tone in my reply. Anyway, I would expect you to have read about linked list before you attempt to implement one. There are also some tutorial or sample of linked list for Java on the Internet. It should give you some basic of how to implement a linked list.

Also, I assume that you know that an application can be implemented in many different ways but still give the same result. The 'one way to implement the copy' is just one way to call copy. You could do it another way that you just call a copy() from the list1 and return a new list to assign to list 2. That's what 'one way ...' is.

This question has already been answered. Start a new discussion instead.