//Garth Bruen //10.09.02 public class LL23{ /* Basics for setting up the lists */ private Node frontOfList = null; public void addFirst(int data){ Node newNode = new Node(); newNode.data = data; newNode.next = frontOfList; frontOfList = newNode; } class Node{ int data; Node next = null; } /* Search a list for a specific item */ public boolean findItem(int myItem){ Node current = frontOfList; while(current != null){ if(current.data == myItem){ return true; } current = current.next; } return false; } /* Accepts one list as a parameter and compares the contents of that list to the contents of the other list by passing each item to the another method called findItem() which searches the list for the parameter */ public boolean compareLists(LL23 list){ boolean same = true; //Defaulted to true Node current = frontOfList; while((current != null)&&(same != false)){ if(list.findItem(current.data)==true){ same = true; } else{ same = false; } current = current.next; } return same; } /* Method accepts the two lists from the main program */ public static boolean compareTwo(LL23 list1, LL23 list2){ /* Pass both lists to the same function and report true if they both pass the test */ if((list2.compareLists(list1))==(list1.compareLists(list2))){ return true; } return false; } public static void main(String args[]) throws Exception { /* This section returns true */ LL23 list1 = new LL23(); LL23 list2 = new LL23(); list1.addFirst(1); list1.addFirst(3); list1.addFirst(4); list1.addFirst(2); list2.addFirst(4); list2.addFirst(2); list2.addFirst(1); list2.addFirst(3); System.out.println(compareTwo(list1, list2)); /* This section returns false */ LL23 list3 = new LL23(); LL23 list4 = new LL23(); list3.addFirst(1); list3.addFirst(3); list3.addFirst(4); list3.addFirst(2); list4.addFirst(4); list4.addFirst(2); list4.addFirst(2); list4.addFirst(3); System.out.println(compareTwo(list3, list4)); } }