Member Avatar for Gsterminator

Hey I need help making a isEqual method in my singly linked class. The goal is to prove that both list have the same elements without necessarily having the same order. Now my method does call a different method in the node class, and it's job is to to search the for the element int the list. Here are two different outputs:
Output1:
Size of list1:
5
[1][2][4][5][7]
[1][7][2][4][5]
First list contains 4:
true
list1 is equal tolist2:
true

Output2:
Size of list1:
5
[1][2][4][5][7]
[1][9][2][4][5]
First list contains 4:
true
list1 is equal tolist2:
true
Please help!!!

public boolean isEqual(SLinkedList obj){
      int count = 0;
      Node cursor;
      Node head2 = obj.head;
      Node tmp = obj.head.getNext();
      cursor = Node.listSearch(head.getNext(), tmp.getElement());
      while(cursor!= null){
          count++;
          while(head2.getNext()!= null){
              head2 = head2.getNext();
          }
          cursor = Node.listSearch(head.getNext(), head2.getElement());
          if(count==size) cursor = null;
      }
      if(count == size){
          return true;
      }
      return false;
  }

I'd solved this using priority queues.
Simply input the values of both your lists into 2 seperate queues and compare the queues. As the queues are self-sorted you should end up with two equal queues, if the lists match ofc.
I'm not saying this is the best way, just saying its A way :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.