It is necessary to read two lists from two files. And create a third list that contains elements that are in the first, but not in the second and which are in the second, but not in the first list. Here is my code, although I have so far just tried to bring these elements to the console, and not insert them into the list. I can not understand what the error is.

public class Element { public int value; public Element next; } public static void main(String[] args) throws FileNotFoundException { Element head1 = null, head2 = null, temp1 = null, temp2 = null; Scanner sc = new Scanner(new File("C://Users//1//Desktop//12w.txt")); if (sc.hasNextInt()) { head1 = new Element(); //голова списка int m = sc.nextInt(); head1.value = m; // вставляю первый элемент в голову temp1 = head1; // указатель, который на данный момент указывает на голову while (sc.hasNextInt()) { // заполняю список m = sc.nextInt(); temp1.next = new Element(); temp1 = temp1.next; // перемещаю на следующее место temp1.value = m; } } sc = new Scanner(new File("C://Users//1//Desktop//12.txt")); // аналогично заполняю второй список if (sc.hasNextInt()) { head2 = new Element(); int m = sc.nextInt(); head2.value = m; temp2 = head2; while (sc.hasNextInt()) { m = sc.nextInt(); temp2.next = new Element(); temp2 = temp2.next; temp2.value = m; } } int s; int m1; int m2; temp1 = head1; //cсылаю указатель на голову while (temp1.next != null) { m1 = temp1.value; // делаю до тех пор пока указатель не ссылается на null s = 1; while (temp2.next != null) { m2 = temp2.value; // аналогично if (m1 == m2) { s = 0; break; // если два элемента при первом проходе равны } temp2 = temp2.next; // ссылаю указатель на след элемент } temp1 = temp1.next; if (s == 1) { System.out.println(temp1.value); } temp2 = head2; // указатель ссылаю на голову } } 
  • and what's the mistake?) - Gorets
  • let's say I have two lists 1 3 5 7 9 11 1 2 3 5 7 9 Expected result 2 11 Current result 3 5 7 9 11 - Ksenia
  • Here I do not see the construction of the 3rd list. @Ksenia, format the code normally and add more top level comments before the code blocks. Type: // build the first list from a file. // add to the result elements of the first list that are not in the second, etc. - avp
  • the whole problem is solved by 3 arrays and one method =) - Gorets
  • @Gorets, it’s quite obvious that the author needs not to find the difference between the union and the intersection of 2 sets, but to master the technique of working with simply connected lists. The desire not to use standard collections and methods is welcome. - avp

3 answers 3

try this:

  1. read your lists (as it suits you, here I will not advise you, because I don’t see the file format, etc.)

  2. Then you add a method to the Element class.

    @Override public boolean equals (Object object) {if (object! = Null) {if (this.value! = Null && ((Element) object) .getValue ()! = Null) {return this.value.equals (( (Element) object) .getValue ()); }} return false; }

  3. Then you use the contains () method of the list to find out if the element of the first list in the list is number 2. If not, add to list 3; if it does, remove from list 2. Example:

    for (Element element: elements1) {if (! elements2.contains (element)) {elements3.add (element); } else {elements2.remove (element); }}

after that, the list 2 will remain those elements that are not contained in the list 1. just add them to the list 3:

elements3.addAll (elements2);

everything. Try, good luck :)

ps sorry, but the code is not formatted, excuse that in one line all

  • @piratewater, did you read the question? Code looked? @Ksenia itself tries to implement its own list in Java (this is the title of the question). You advise her to use standard Java lists. - avp
  • @avp, why reinvent the wheel ??? And the most interesting thing is that this answer was accepted, logically, this is exactly what @Ksenia wanted - pirateweter
  • I noticed. Maybe the girl took the answer from hopelessness? Judging by the code in question, she wanted to do it herself . About the "bike". If you do not try to invent them, you will not become a hacker (in the original meaning of this term). - avp
  • maybe not ... This is already some kind of philosophy. From despair could accept your answer :). I propose only effective code, one of the rules of writing of which, as I already said, is Do it simple. And about the "bicycle" - while you reinvent the next bike, trying to become a hacker - your "competitors" will step forward in development. A hacker does not become inventing bikes, but finding something new (this is my opinion). But again - this is a philosophy. And we are considering the issue of programming, which has long been resolved. So let's not go with the assumptions - pirateweter
  • one
    I agree! I was repelled by the fact that our source list still consists of Element objects (or any others). Which may include not only the int value. And it is necessary to compare precisely nonobtrusive OBJECTS (by a specific field, or several fields). That is, I tried to tell about the use of the contains () method and the implementation of the equals () method for complex objects. - pirateweter

To properly solve such problems, you must clearly represent the sequence of actions. For this it is better to sit down and write on a piece of paper all of the points:

  1. read data from 2 files
  2. cycle check items 1 of the list in the 2nd
  3. same items 2nd in 1st
  4. if the condition (contain ()) is false , write the element to the new array
  • I understand my algorithm, but I can’t understand at what point in time it starts to work incorrectly - Ksenia
  • one
    I mean that such a code is difficult to maintain and understand, you have a devil’s leg there, a bunch of variables, someone compares with someone, someone vanishes. Hardly anyone will be able to help you here =) - Gorets
  • I tried to write comments so that my code was a little clear - Ksenia

As far as I can see at the first cycle

 while(temp1.next!=null){ ... while(temp2.next!=null) 

temp2.next will be null, because temp2 as pointed to the tail of the second list, and remained.