(10 points) Given an array of random integers, Push all the zero’s of a given array to…
- User
(10 points) Given an array of random integers, Push all the zero’s of a given array to the end of the array. For example, an array before and after reorganization is like: Before: arr[] = 1, 2, 0, 4, 3, 0, 5, 0; After: arr[] = 1, 2, 4, 3, 5, 0, 0, 0; Write in Java or pseudo-code. You are recommended to use the following pseudo-code style. pushZero(int arr[]){ ... // add your operations }
- Assistant
Here is the Java code to push all the zeros of a given array to the end of the array:
public void pushZero(int arr[]) { int n = arr.length; int count = 0; // count of non-zero elements // Traverse the array and move non-zero elements to the front for (int i = 0; i < n; i++) { if (arr[i] != 0) { arr[count] = arr[i]; count++; } } // Fill remaining elements with zeros while (count < n) { arr[count] = 0; count++; } }To test the code, you can call the
pushZeromethod and pass the input array as an argument:int[] arr = {1, 2, 0, 4, 3, 0, 5, 0}; pushZero(arr); System.out.println(Arrays.toString(arr));Output:
[1, 2, 4, 3, 5, 0, 0, 0] - User
用伪代码来解释
- Assistant
pushZero(arr): n = length(arr) count = 0 // Traverse the array and move non-zero elements to the front for i = 0 to n-1: if arr[i] != 0: arr[count] = arr[i] count++ // Fill remaining elements with zeros while count < n: arr[count] = 0 count++伪代码解释:
- 定义一个名为pushZero的函数,接受一个整数数组arr作为参数。
- 初始化一个变量n,用于存储数组的长度。初始化一个计数器count,用于统计非零元素的个数。
- 使用循环遍历数组arr,从索引0到n-1。
- 判断当前元素arr[i]是否不等于0,如果不等于0,则执行以下操作:
- 将当前元素arr[i]移到前面,即将其赋值给arr[count]。
- 计数器count自增1,表示已经找到一个非零元素。
- 使用循环填充剩余的元素为0,直到计数器count等于数组长度n。
- 函数执行完毕,数组arr中的所有零元素已被移动到数组的末尾。
- User
Given two queues Q1 and Q2 with their standard operations (enqueue, dequeue, isempty, size), implement a stack ADT S with its standard operations (pop, push, isempty, size). Suppose that once S is created, you are given two empty queues Q1 and Q2 by default. Write in Java or pseudo-code. You are recommended to use the following pseudo-code style. class S{ queue Q_1 //one queue instance queue Q_2 //another queue instance pop(){ ... // use Q_1, Q_2 here } push(x){ ... // use Q_1, Q_2 here } ... }
- Assistant
Here is the Java code to implement a stack ADT using two queues:
class S { Queue<Integer> Q_1; // one queue instance Queue<Integer> Q_2; // another queue instance // Constructor public S() { Q_1 = new LinkedList<>(); Q_2 = new LinkedList<>(); } // Function to remove the top element from the stack public int pop() { if (isEmpty()) { throw new NoSuchElementException("Stack is empty"); } // Move all elements from Q_1 to Q_2 except the last element while (Q_1.size() > 1) { Q_2.add(Q_1.remove()); } // Remove the last element from Q_1 (which is our top element) int topElement = Q_1.remove(); // Swap the names of Q_1 and Q_2 Queue<Integer> temp = Q_1; Q_1 = Q_2; Q_2 = temp; return topElement; } // Function to add an element to the top of the stack public void push(int x) { // Enqueue the new element to Q_1 Q_1.add(x); } // Function to check if the stack is empty public boolean isEmpty() { return Q_1.isEmpty(); } // Function to get the size of the stack public int size() { return Q_1.size(); } } - User
用伪代码来解释
- Assistant
class S: queue Q_1 // one queue instance queue Q_2 // another queue instance pop(): if isempty(): throw new Exception("Stack is empty") // Move all elements from Q_1 to Q_2 except the last element while size(Q_1) > 1: enqueue(Q_2, dequeue(Q_1)) // Remove the last element from Q_1 (which is our top element) topElement = dequeue(Q_1) // Swap the names of Q_1 and Q_2 temp = Q_1 Q_1 = Q_2 Q_2 = temp return topElement push(x): // Enqueue the new element to Q_1 enqueue(Q_1, x) isempty(): return size(Q_1) == 0 size(): return size(Q_1)伪代码解释:
S类定义了一个栈 ADT,使用两个队列Q_1和Q_2来实现栈的操作。pop方法用于从栈中移除栈顶元素。如果栈为空,则抛出异常。pop方法的实现:- 当
Q_1的大小大于1时,依次将Q_1的元素出队,并入队到Q_2中,除了最后一个元素。 - 从
Q_1中出队最后一个元素(即栈顶元素),将其保存到topElement变量中。 - 交换 和 的名称,使得下一次的操作仍然可以使用 作为队列。
- 当