合并队列LinkedList

时间:2015-05-18 07:28:27

标签: java

我已经使用LinkedList创建了一个Queue,我想制作一个合并此方法的方法,该方法调用此方法的参数,我为一个Queue创建一个深层副本但是当我尝试然后合并这个副本Queue i不能插入元素,但是当这个方法从另一个方法调用时它不是一个副本就可以正常工作。

QueueLinkedList
{
    public static void main(String[] args){
        Queue myQueue = new Queue();
        myQueue.insert(new Person("Alice",0));
        myQueue.remove();
        myQueue.insert(new Person("Alice",0));
        myQueue.insert(new Person("Bob", 2));
        myQueue.insert(new Person("Charlie", 2));

        Queue yourQueue = new Queue(myQueue);

        Queue herQueue = new Queue();
        herQueue.insert(new Person("Alice",1));
        yourQueue.merge(herQueue);
        System.out.println(yourQueue) ;
        myQueue.insert(new Person("Thodoris", 11));
        myQueue.insert(new Person("Dimitris", 24));
        yourQueue.remove() ;
        System.out.println(myQueue) ;
        System.out.println(yourQueue) ;

        if (yourQueue.equals(myQueue)){
            System.out.println("The two queues are the same");
        }else{
            System.out.println("The two queues are different");
        }
        System.out.println("My queue has " + myQueue.size() + " elements");
        while (!myQueue.isEmpty()){
            myQueue.remove();
        }
        Person p = myQueue.remove();
    }
}
class Queue
{
    private PersonQueueElement head ;
    private PersonQueueElement tail ;
    private int size = 0;

    public Queue()
    {
        head = null ;
        tail = null ;
        size = 0;
    }

    public Queue(Queue other)
    {
        if(other.head != null)
        {
            PersonQueueElement head = new PersonQueueElement(other.head);
            this.head = head ;
        }
        if(other.tail != null)
        {
            PersonQueueElement tail = new PersonQueueElement(other.tail);
            this.tail = tail ;
        }

        this.size = other.size ;

    }

    public void insert(Person value)
    {
        PersonQueueElement element = new PersonQueueElement(value);
        if(head == null)
        {
            tail = element ;
            head = tail ;
            size ++ ;
        }
        else
        {
            tail.setNext(element)  ;
            tail = tail.getNext();
            size ++ ;
        }
    }

    public Person remove()
    {
        if (head == null)
        {
            System.out.println("The Queue is empty.") ;
            return null ;
        }
        else 
        {
            Person temp = head.getValue() ;
            head = head.getNext() ;
            size -- ;
            return temp;

        }
    }

    public String toString() 
    {
        String result = "" ;
        PersonQueueElement current = head ;
        while(current != null)
        {
            result += current.getValue().toString() + " " ;
            current = current.getNext() ;
        }

        return "Queue: " + result ;
    }

    public boolean equals(Queue other)
    {
        Queue otherQueue = other ;
        if(size() != otherQueue.size())
        {
            return false ;
        }
        PersonQueueElement position = head ;
        PersonQueueElement otherPosition = otherQueue.head ;
        while(position != null)
        {
            if(!(position.getValue().equals(otherPosition.getValue())))
            {
                return false ;
            }

            position = position.getNext() ;
            otherPosition = otherPosition.getNext() ;
        }
        return true ;
    }

    public Queue merge(Queue other)
    {
        Queue mergeQueue = this ;
        PersonQueueElement position = other.head ;
        while(position != null)
        {
            mergeQueue.insert(new Person(position.getValue())) ;
            position = position.getNext() ;
        }
        return mergeQueue;
    }
    public boolean isEmpty()
    {
        return head == null ;
    }

    public int size()
    {
        return size ;
    }
}
class Person
{
    private String name;
    private int AM;

    public Person(String name, int AM)
    {
        this.name = name;
        this.AM = AM;
    }

    public Person(Person other)
    {
        this.name = other.name ;
        this.AM = other.AM ;
    }

    public String toString()
    {
        return "(" + name + "," + AM + ")" ;
    }

    public String getName()
    {
        return name ;
    }

    public int getAM()
    {
        return AM ;
    }
}
class PersonQueueElement
{
    private Person value ;
    private PersonQueueElement next ;

    public PersonQueueElement(Person value)
    {
        this.value = value ;
    }

    public PersonQueueElement(PersonQueueElement other)
    {
        if(other.value != null)
        {
            Person value = new Person(other.value) ;
            this.value = value ;
        }

        if(other.next != null)
        {
            PersonQueueElement next = new PersonQueueElement(other.next) ;
            this.next = next ;
        }

    }

    public void setNext(PersonQueueElement element)
    {
        next = element ;
    }

    public PersonQueueElement getNext()
    {
        return next ;
    }

    public Person getValue()
    {
        return value ;
    }

}

1 个答案:

答案 0 :(得分:1)

你在Queue(Queue other)

中犯了一个错误

特别是这一行

PersonQueueElement tail = new PersonQueueElement(other.tail);

目前实施的方式是,您无法创建必须使用现有尾部的新节点。

PersonQueueElement tail = other.tail;

顺便说一句,此构造函数不会执行deep副本。 我怀疑你真正想要做的是插入原始队列中的所有元素。

类似的东西:

public Queue(Queue other){

    if(other != null){
        PersonQueueElement node = other.head;
        while(node != null){
            this.insert(node.getValue());
            node =node.getNext();
        }
    }

}

这段代码产生了

Queue: (Alice,0) (Bob,2) (Charlie,2) (Alice,1) Queue: (Alice,0) (Bob,2) (Charlie,2) (Thodoris,11) (Dimitris,24) Queue: (Bob,2) (Charlie,2) (Alice,1) The two queues are different My queue has 5 elements The Queue is empty.