移除队列的中间数字。

huangapple go评论64阅读模式
英文:

Removing middle number of a queue

问题

以下是你要翻译的内容:

我需要创建一个方法,在其中移除链式队列的中间数字。[1,2,3,4,5] 变成 [1,2,4,5],如果队列的长度是偶数,则向下取整并从那个位置移除。所以 [1,2,3,4] 变成 [1,3,4]。

我不确定如何去除中间的数字,然后再将其他我移除的数字添加回去。

这是我目前的代码。

...

public class LinkedQueue {
    Node head;
    Node front;
    Node rear;
    int count;
    int elements;

    public void enqueue(int numEnqueue) {
        Node node = rear;
        rear = new Node();
        rear.numEnqueue = numEnqueue;
        rear.next = null;
        if (front == null)
            front = rear;
        else
            node.next = rear;
        count++;
        elements = count;
    }

    public void dequeue() {
        for (int i = 0; i < 6; i++) {
            int numEnqueue = front.numEnqueue;
            front = front.next;
            count--;
            System.out.print("\nDequeueing: " + numEnqueue);
        }
    }

    public void show() {
        Node node = front;
        System.out.print("\n\nQueued linked elements: ");
        while (node != null) {
            System.out.print(node.numEnqueue + " ");
            node = node.next;
        }
    }

    public void removeMiddle() {
        int half = 0;
        half = elements / 2;
        for (int i = 0; i < half; i++) {
            int numEnqueue = front.numEnqueue;
            front = front.next;
        }
    }
}

...

驱动代码

...

public class Driver {
    public static void main(String[] args) {
        LinkedQueue link = new LinkedQueue();
        link.enqueue(1);
        link.enqueue(7);
        link.enqueue(3);
        link.enqueue(4);
        link.enqueue(9);
        link.enqueue(2);
        link.show();
        link.dequeue();

        link.enqueue(1);
        link.enqueue(7);
        link.enqueue(3);
        link.enqueue(4);
        link.enqueue(9);
        link.enqueue(2);
        link.removeMiddle();
        link.show();
    }
}

...

节点代码

public class Node {
    int numEnqueue;
    Node next;
}
英文:

I need to create a method in which I remove the middle number of a Linked queue. [1,2,3,4,5] turns into [1,2,4,5] and if the queue is even-numbered you round down and remove from there. So [1,2,3,4] turns into [1,3,4].

I am unsure how to go by removing the middle number and then adding back in the other numbers I removed.

This is what I currently have.

...

public class LinkedQueue {
Node head;
Node front;
Node rear;
int count;
int elements;




public void enqueue(int numEnqueue) {
	Node node = rear;
	rear=new Node();
	rear.numEnqueue=numEnqueue;
	rear.next=null;
	if (front==null) 
		front = rear;
		else 	
			node.next = rear;
	count++;
	elements=count;
	}

//
public void dequeue() {
	for(int i=0;i&lt;6;i++) {
	int numEnqueue = front.numEnqueue;
    front = front.next;
    count--;
    System.out.print(&quot;\nDequeueing: &quot;+numEnqueue);
	}

}

public void show() {
	Node node = front;
	System.out.print(&quot;\n\nQueued linked elements: &quot;);
	while(node!=null) {
		System.out.print(node.numEnqueue+&quot; &quot;);
		node=node.next;
		
	}
	
	
}


public void removeMiddle() {	
	int half = 0;
    half=elements/2;
    for (int i=0;i&lt;half;i++) {
    	int numEnqueue = front.numEnqueue;
        front = front.next;
    }

}
  
}

...
Driver code

...

public class Driver {

public static void main(String[] args) {

	LinkedQueue link=new LinkedQueue();
	link.enqueue(1);
	link.enqueue(7);
	link.enqueue(3);
	link.enqueue(4);
	link.enqueue(9);
	link.enqueue(2);
	link.show();
	link.dequeue();
	
	
	link.enqueue(1);
	link.enqueue(7);
	link.enqueue(3);
	link.enqueue(4);
	link.enqueue(9);
	link.enqueue(2);
	link.removeMiddle();
	link.show();

}



}

...
Node code

public class Node {
    int numEnqueue;
    Node next;
}

答案1

得分: 1

因为这是作业,我会提供伪代码/建议:

  • 给定长度为 n 的队列,你需要移除元素 (n - 1) / 2int 算术会为你截断除法的小数部分)。
  • 在一个循环中,循环次数为 (n - 1) / 2 次,从根节点开始,每次迭代前进到下一个节点。
  • 移除循环结束时所在的节点。

实际的解决方案比这略微复杂,你可能会发现由于每个节点没有对其前任的引用,所以更容易移除下一个节点。在这种情况下,迭代到根节点将算作循环的一次迭代。

英文:

Since this is homework, I'll offer pseudo code/advice:

  • given a queue of length n, you need to remove element (n - 1) / 2 (int arithmetic truncates the fractional part of the division for you).
  • in a loop that iterates (n - 1) / 2 times, starting with the root node, advance to the next node each iteratation
  • remove the node the loop ends on

The actual solution is slightly more complicated than this, and you may find that it easier to remove the next node due to each node not having a reference to its predecessor. In which case, iterating to the root would count as one iteration of the loop.

huangapple
  • 本文由 发表于 2020年8月26日 07:13:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63588351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定