繁体   English   中英

java中2个循环单链表并集的方法

[英]A method to get the union of 2 circular singly linked lists in java

我在考试前练习了一些链表方法,我试图得到 2 个循环链表的并集,但是在 output 中,元素应该被排序。 我写了代码,但我得到了错误的答案。

我在一个双向链表中尝试了这种方法,它工作得非常好,但当然我必须更改一些符号。

输入:列表:8 | 2 | 4 | 3 | 10 | 5 | 8 | 4 |
列表 1:5 | 1 | 3 | 7 | 0 | 6 | -4 | 1 |
output:5 | 1 | 3 | 7 | 0 | 6 | -4 | 8 | 2 | 4 | 3 | 10 | 5 | 8 | 1 | 5 | 1 | 3 | 7 | 0 | 6 | -4 |

正如你所看到的,我得到的数字比我已经拥有的多。
所以任何帮助将不胜感激。

这是方法:

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;
    }

这是整个 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);
        
        //special case: empty list
        if(this.head == null) {
            tmp.next = tmp;
            this.head = tmp;
            this.rear = tmp;
            
        }else { // general case
            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 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) {
//      
//  }
    
    

    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(list2.union(list, list1));  //not working
                
//      System.out.println(list2.inter(list, list1));  //working
        
//      System.out.println(list.isEqualTo(list1));  // working

    }

}

这应该这样做。 虽然这只会在两个输入都排序的情况下产生一个排序列表。

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

    return c;
}

要保持列表排序,请调整您的插入方法:

public void insert(int value) {
     Element tmp = new Element(value);
     Element cur = head;
        
     // special case: empty list
     if (this.head == null) {
         tmp.next = tmp;
         this.head = tmp;
         this.rear = tmp;     
     } else { // general case
         // Change this condition if you want the list to be sorted decending
         while (cur != rear && cur.data < value) {
             prev = cur;
             cur = cur.next;
         }
         
         tmp.next = cur;
         cur.next = tmp;
    }     
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM