0

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;
  }
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Zhoot
0

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 :)

Edited by Zhoot: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.