Computer Science Canada

concatenating 2 linkedlist

Author:  a22asin [ Sat Sep 20, 2014 10:29 am ]
Post subject:  concatenating 2 linkedlist

HI, so m trying to combine two linked list into a new linked list. LinkedList is a totally new concept for me so bar with me. I have 2 classes, one with IntNode and one called linkedlist. The IntNode has listCopy and listCopyWithTail, which are two classes i believe i need to use; however, im having trouble using them as they are static classes, so i do IntNode.listCopy(head1), but i keep getting and error that cannot make static reference to a non-static field head1. I tried to search this up but with no luck. Also, i kinda have no idea how im supposed to go through with the rest of method in order to combine the two linkedlist without effecting the original lists.

I have attached my classes for reference.

Author:  DemonWasp [ Sat Sep 20, 2014 11:28 am ]
Post subject:  RE:concatenating 2 linkedlist

The LinkedList.concatenate method is static (meaning attached to the class definition itself rather than to instances of that class; the opposite term is "instance"). Since it is static, it does not have access to instance members, like head1.

The error message is reasonably clear once you know how to read it:

LinkedList.java:62: error: non-static variable head1 cannot be referenced from a static context

At LinkedList.java, you do something wrong. You are attempting to reference (use) a non-static variable (head1) from a static context (meaning inside a static method).

Author:  a22asin [ Sat Sep 20, 2014 2:22 pm ]
Post subject:  Re: RE:concatenating 2 linkedlist

DemonWasp @ Sat Sep 20, 2014 11:28 am wrote:
The LinkedList.concatenate method is static (meaning attached to the class definition itself rather than to instances of that class; the opposite term is "instance"). Since it is static, it does not have access to instance members, like head1.

The error message is reasonably clear once you know how to read it:

LinkedList.java:62: error: non-static variable head1 cannot be referenced from a static context

At LinkedList.java, you do something wrong. You are attempting to reference (use) a non-static variable (head1) from a static context (meaning inside a static method).


ok... so how would i create the concatenate method if the copyList is static and my IntNode head1 isnt? Also, in order to do this correctly, i would need to do copyList head1 and then copyListWithTail head2 in order to create head3 right?

Author:  DemonWasp [ Sun Sep 21, 2014 12:51 am ]
Post subject:  RE:concatenating 2 linkedlist

I haven't looked into your code too deeply, so I won't answer the second question. To answer the first, though, you can access static contexts from non-static contexts (i.e. single objects can access what is effectively global data), but you cannot do the reverse.

Author:  a22asin [ Sun Sep 21, 2014 11:03 am ]
Post subject:  Re: RE:concatenating 2 linkedlist

DemonWasp @ Sun Sep 21, 2014 12:51 am wrote:
I haven't looked into your code too deeply, so I won't answer the second question. To answer the first, though, you can access static contexts from non-static contexts (i.e. single objects can access what is effectively global data), but you cannot do the reverse.


Could u how me a generic example please? im a visual learner so i need to see what u mean, sorry.

Author:  DemonWasp [ Sun Sep 21, 2014 11:58 am ]
Post subject:  RE:concatenating 2 linkedlist

[syntax="Java]
public class Example {
public static final int CONSTANT = 3; // static member
private int instanceVar = 0; // instance member

public void foo () {
// non-static context
instanceVar += CONSTANT; // ok: we can use both instance and static variables
bar(); // ok: we can call static methods from non-static contexts
}

public static void bar() {
// static context

//instanceVar += 1; // NO: cannot access instance variables / methods from static context
//foo(); // NO: same reason

System.out.println ( CONSTANT ); // OK: only uses static variables and methods
}
}

// in a different file:
public class Main {
public static void main ( String[] args ) {
System.out.println ( Example.CONSTANT ); // no instance needed
Example.bar(); // works: no instance needed

Example example = new Example();
example.foo(); // works: we called an instance method on an instance
example.bar(); // warning: we called a static method against an instance; Java knows to just call the static method, but this is probably not what we meant to write.
}
}
[/syntax]

Author:  a22asin [ Sun Sep 21, 2014 12:10 pm ]
Post subject:  RE:concatenating 2 linkedlist

oh ok, i figured it out. Thanks, now im just having trouble concatenating the two lists into a new one. I know i have to use the listCopy and listCopyWithTail (provided below), but how is what im having trouble figuring out.

code:

   public static IntNode listCopy(IntNode source)
   {
      IntNode copyHead;
      IntNode copyTail;
     
      // Handle the special case of the empty list.
      if (source == null)
         return null;
         
      // Make the first node for the newly created list.
      copyHead = new IntNode(source.data, null);
      copyTail = copyHead;
     
      // Make the rest of the nodes for the newly created list.
      while (source.link != null)
      {
         source = source.link;
         copyTail.addNodeAfter(source.data);
         copyTail = copyTail.link;
      }
 
      // Return the head reference for the new list.
      return copyHead;
   }
   

   public static IntNode[ ] listCopyWithTail(IntNode source)
   {
      IntNode copyHead;
      IntNode copyTail;
      IntNode[ ] answer = new IntNode[2];
     
      // Handle the special case of the empty list.   
      if (source == null)
         return answer; // The answer has two null references .
     
      // Make the first node for the newly created list.
      copyHead = new IntNode(source.data, null);
      copyTail = copyHead;
     
      // Make the rest of the nodes for the newly created list.
      while (source.link != null)
      {
         source = source.link;
         copyTail.addNodeAfter(source.data);
         copyTail = copyTail.link;
      }
     
      // Return the head and tail references.
      answer[0] = copyHead;
      answer[1] = copyTail;
      return answer;
   }

Author:  DemonWasp [ Sun Sep 21, 2014 12:41 pm ]
Post subject:  RE:concatenating 2 linkedlist

This is a part of your assignment and is on you to figure out. If you have specific questions, or specific pieces of code that you don't understand, then you can ask about them. If you are unclear on what the assignment is asking you to do, you can ask about that. However, you are required to figure out how to solve the assignment problem yourself.

Author:  a22asin [ Sun Sep 21, 2014 12:50 pm ]
Post subject:  RE:concatenating 2 linkedlist

ok could u atleast explain to me how id go about doing this? Im still not sure how LinkedList works.

Author:  DemonWasp [ Sun Sep 21, 2014 1:42 pm ]
Post subject:  RE:concatenating 2 linkedlist

You just need to link the tail of one list to the head of the other. You really should understand how the linked list works for this assignment. Try reading the wikipedia page: http://en.wikipedia.org/wiki/Linked_list

Author:  a22asin [ Sun Sep 21, 2014 2:07 pm ]
Post subject:  RE:concatenating 2 linkedlist

lol, i know i should, im missed class cuz i got sick, ive been going through my txtbook and wat not, but still didnt get it. Ok Thanks.


: