How is it possible to fix this error in this circular linked list? (union method)

Viewed 37

I want to make a method in a circular linked list that gets the union of 2 lists given as an argument but it's not working, I am getting as result many more elements that I have combined in the 2 lists, and I think that my logic is correct, but I don't know what's wrong. If someone can help me out, I would appreciate it.

This is the method:

public CircularLinkedList union(CircularLinkedList a, CircularLinkedList b) {
        Element curA = a.head;
        Element curB = b.head;
        CircularLinkedList c = new CircularLinkedList();
        
        do {
            if(curA.data < curB.data) {
                c.insert(curA.data);
            curA = curA.next;
            }else {
                c.insert(curB.data);
                curB = curB.next;
            }
        }while(curA != a.rear && curB != b.rear);
        
        do {
            c.insert(curA.data);
            curA = curA.next;
        }while(curA != a.rear);
        
        do {
            c.insert(curB.data);
            curB = curB.next;
        }while(curB != b.rear);
        return c;
    }

This is the whole class:


public class CircularLinkedList {
    
class Element{
        
        int data;     // int type used as example
        Element next; // reference of the successor
        
        Element(int value) {
            this.data = value;
            this.next = this;
        }
        
    }

    private Element head = null;
    private Element rear = null;

    public CircularLinkedList() {
        this.head = this.rear = null;
    }
    
    
    public boolean isEmpty() {
        return head == null;
    }
    
    
    public boolean findValue(int value) {
        Element cur = this.head;
        
        while(cur != null) {
            if (cur.data == value)
                return true;
            cur = cur.next;
        }
        return false;
    }
    
    
    public int countValue(int value) {
        int c = 0; // counter
        Element cur = this.head;
        
        if(cur == null) 
            return 0;
        
        do {
            if(cur.data == value) 
                c++;
            cur = cur.next;
            
        }while (cur != this.head);
            
        return c;
    }
    
    
    public boolean isInList(int value) {
        if(this.head == null)
            return false;
        
        Element cur = this.head;
        while(cur != this.rear) {
            if(cur.data == value)
                return true;
            cur = cur.next;
        }
        return false;
    }
    
    
    @Override
    public String toString() {
        String str = "";
        Element cur = this.head;
        
            if(cur == null) 
                return "The list is empty";
        
            do {
                str += cur.data + " | ";
                cur = cur.next;
            
            }while (cur != this.head);
            
        return str;
        }
    
    
    
    public void insert(int value) {
        Element tmp = new Element (value);
//      Element cur = this.head;
//      Element prev = null;
        
        //special case: empty list
        if(this.head == null) {
            tmp.next = tmp;
            this.head = tmp;
            this.rear = tmp;
            
        }else { // general case
//          while (cur != rear && cur.data < value) { //ascending
//               prev = cur;
//               cur = cur.next;
//           }
//           
//           tmp.next = cur;
//           cur.next = tmp;
//      }  
//  }
            tmp.next = this.head;
            this.rear.next = tmp; 
            this.rear = tmp;
        }
            
    }
    
    
    public void deleteAtHead() {
        if(this.head == null) 
            return;
        
        Element cur = this.head;    
        while(cur.next != this.head) {            
             cur = cur.next;
            }
            cur.next = cur.next.next;       
            this.head = this.head.next;                
            return ;    
    }
    
    
    public void deleteAtRear() {
        if(this.head == null)
            return;
        
        Element cur = this.head;
//      Element prev = null;
        while(cur.next != this.rear) {
//          prev = cur;
            cur = cur.next;
        }
        cur.next = cur.next.next;
        this.rear = cur;
        
    }
    
        
    public boolean delete(int value) {
        Element cur = this.head;
        
        if(this.head.data == value) {                  //if the node to be deleted is head node
        
            while(cur.next != this.head) {         //iterate till the last node i.e. the node which is pointing to head         
                cur = cur.next;
            }
            cur.next = cur.next.next;       // update current node pointer to next node of head
            this.head = this.head.next;                //update head node
            return true;
        }
        else {                              // if node to be deleted is other than head node
        
            Element prev = cur;               // track previous node from current (node)
            while(cur.data != value) {       // find the node           
                prev = cur;
                cur = cur.next;
            }
            prev.next = cur.next; //updating next field of previous node to next of current node.current node deleted
            return true;
        }
    }
    
    
    
    public void deleteEven() {
//      if(this.head == null)
//          return;
//      
//      //case of deleting the head
//      if(this.head.data % 2 == 0) {
//          this.head.next = this.head;
//          this.rear.next = this.head;
//      if(this.head == null) 
//          this.rear = null;
//      }
//      
//      Element cur = this.head;
//      Element prev = cur;
//      while(cur != this.head) {
//          prev = cur;
//          cur = cur.next;
//      }
//      prev.next = cur.next;
        
        if(this.head == null)
            return;
        Element cur = this.head;
        while(cur.next != this.head) {
            if(cur.data % 2 == 0)
                this.delete(cur.data);
            cur = cur.next;
        }
    }
    
    
    public void deleteLastOccurence(int value) {
        Element cur = this.head;
        Element prev = null;
        Element tmp = null;
        
        if(this.head == null)
            return;
        
        if(this.rear.data == value) {
            this.head = null;
            return;
        }
        
//      while(cur != this.rear) {
            while(cur.next != this.head && cur.next.data != value) {
                prev = cur;
                tmp = cur.next;
//              cur = cur.next;
            }
//          cur = cur.next;
//      }
        prev.next = tmp.next;
    }
    
    
    public void deleteAllOccurrences(int value) {
        Element cur = this.head;
        Element next = null;

        if (cur.data == value) {
            cur = cur.next;
            this.head = cur;
        }

        do {
            next = cur.next;
            if (next.data == value) {
                cur.next = next.next;
            }
            cur = next;

        } while (cur != this.head);
    }
    
    
    public static CircularLinkedList union(CircularLinkedList a, CircularLinkedList b) {
        Element curA = a.head;
        Element curB = b.head;
        CircularLinkedList c = new CircularLinkedList();
        
        do {
            if(curA.data < curB.data) {
                c.insert(curA.data);
            curA = curA.next;
            }else {
                c.insert(curB.data);
                curB = curB.next;
            }
        }while(curA != a.rear && curB != b.rear);
        
        do {
            c.insert(curA.data);
            curA = curA.next;
        }while(curA != a.rear);
        
        do {
            c.insert(curB.data);
            curB = curB.next;
        }while(curB != b.rear);
        return c;
    }
    
    
    public CircularLinkedList inter(CircularLinkedList a, CircularLinkedList b) {
        Element curA = a.head;
        CircularLinkedList c = new CircularLinkedList();
        
        if(a.head == null || b.head == null)
            return c;
        
        while(curA != a.rear) {
            if(b.isInList(curA.data))
                c.insert(curA.data);
            curA = curA.next;
        }
        return c;
    }
    
    
    public boolean isEqualTo(CircularLinkedList a) {  //same content in same order
        Element cur = this.head;
        Element curA = a.head;
        
        while(cur != this.rear && curA != a.rear) {
            if(cur.data != curA.data) {
                return false;
            }
            cur = cur.next;
            curA = curA.next;
        }
        return true;
    }
    
    
    public int countOddNbrs() {
        if(this.head == null)
            return 0;
        
        int c = 0;
        Element cur = this.head;
        do {
            if(cur.data % 2 != 0)
                c++;
            cur = cur.next;
        }while(cur != this.head);
        return c;
    }

    
    public int findLastOccurence(int value) {
        if(this.head == null)
            return -1;
        
        if(!isInList(value))
            return -1;
        
        if(this.head.data == this.rear.data)
            this.head =null;
        
        Element cur = this.head;
        Element prev = null;
        Element tmp = null;
        int c = 0;
        while(cur != this.rear) {
            if(cur.next != this.head && cur.next.data == value) {
                prev = cur;
                tmp = cur.next;
                c++;
            }
            cur = cur.next;
        }
        return c;
    }
    
    

    public static void main(String[] args) {
        CircularLinkedList list = new CircularLinkedList();
        CircularLinkedList list1 = new CircularLinkedList();
        CircularLinkedList list2 = new CircularLinkedList();
        
        list.insert(8);
        list.insert(2);
        list.insert(4);
        list.insert(3);
        list.insert(10);
        list.insert(5);
        list.insert(8);
        list.insert(4);     
        System.out.println(list);
        
//      list2.insert(8);
//      list2.insert(2);
//      list2.insert(4);
//      list2.insert(3);
//      list2.insert(10);
//      list2.insert(5);
//      list2.insert(8);
//      list2.insert(4);        
//      System.out.println(list);
        
        list1.insert(5);
        list1.insert(1);
        list1.insert(3);
        list1.insert(7);
        list1.insert(0);
        list1.insert(6);
        list1.insert(-4);
        list1.insert(1);        
        System.out.println(list1);
        
//      System.out.println(list.findValue(2)); // working
        
//      list.delete(8);  // working
//      System.out.println(list);
                
//      System.out.println(list.countOddNbrs());  //working
        
//      list.deleteEven();  //  working
//      System.out.println(list);
        
//      list.deleteAtHead();  //  working
//      System.out.println(list);
        
//      list.deleteAtRear();  //   working
//      System.out.println(list);
        
//      list.deleteLastOccurence(4);  //not working
//      System.out.println(list);
        
//      list.deleteAllOccurrences(4);  // working
//      System.out.println(list);
                 
//      System.out.println(union(list, list1));  //not working
                
//      System.out.println(list2.inter(list, list1));  //working
        
//      System.out.println(list.isEqualTo(list1));  // working

    }

}
1 Answers

Because you use a do while loop the seconden and thrid do while loop can cause the problem, when in the first all element a inserted the the new list. I would do it like this:

   public static CircularLinkedList union(CircularLinkedList a, CircularLinkedList b) {
    Element curA = a.head;
    Element curB = b.head;
    CircularLinkedList c = new CircularLinkedList();

    while (!(curA == a.rear && curB == b.rear)) {
        if (curA == a.rear) {
            c.insert(curB.data);
            curB = curB.next;
        } else if (curB == b.rear) {
            c.insert(curA.data);
            curA = curA.next;
        } else if (curA.data < curB.data) {
            c.insert(curA.data);
            curA = curA.next;
        } else {
            c.insert(curB.data);
            curB = curB.next;
        }
    }
    return c;
}

When you start to gernify you class merge sort could be very use full. Also I would check on duplicates.

Related