第十章第十题(Queue类)(Queue class)

第十章第十题(Queue类)(Queue class)

  • *10.10(Queue类)10.6节给出了一个Stack类。设计一个名为Queue的类用于存储整数。像栈一样,队列保存元素。在栈中,元素以“后进先出”的方式获取。在队列中,元素以“先进先出”的方式 获取。该类包含:

    • 一个名为element的int[]类型的数据域,保存队列中的int值。
    • 一个名为size的数据域,保存队列中的元素个数。
    • 一个构造方法,以默认容量为8来创建一个Queue对象。
    • 方法enqueue(int v),用于将v加入队列中。
    • 方法dequeue(),用于从队列中移除元素并返回该元素。
    • 方法empty(),如果队列为空,该方法返回true。
    • 方法getSize(),返回队列的大小。

    画出该类的UML图并实现这个类,初始数组的大小为8.一旦元素个数超过了数组大小,数组大小将会翻倍。如果一个元素从数组的开始部分移除,你需要将数组中的所有元素往左边移动一个位置。编写一个测试程序,添加1到20的20个数字到队列中,然后将这些数字移除并显示它们。
    *10.10(Queue class)Section 10.6 gives a stack class. Design a class called queue to store integers. Like stacks, queues hold elements. In the stack, elements are obtained in a “last in, first out” manner. In a queue, elements are obtained in a first in, first out manner. This class contains:

    • A data field of type int [] named element, which holds the int value in the queue.
    • A data field named size holds the number of elements in the queue.
    • A constructor that creates a queue object with a default capacity of 8.
    • The enqueue (int V) method is used to add V to the queue.
    • The dequeue () method is used to remove the element from the queue and return it.
    • Method empty(), which returns true if the queue is empty.
    • Method getsize() returns the size of the queue.

    Draw the UML diagram of the class and implement the class. The initial size of the array is 8. Once the number of elements exceeds the size of the array, the size of the array will double. If an element is removed from the beginning of the array, you need to move all the elements in the array one position to the left. Write a test program, add 20 numbers from 1 to 20 to the queue, then remove the numbers and display them.

  • 参考代码:

package chapter10;

public class Code_10 {
    
    
    public static void main(String[]  args){
    
    
        Queue qu = new Queue();
        for (int i = 0;i < 20;i++)
            qu.enqueue(i);
        while (!qu.empty()){
    
    
            System.out.print(qu.dequeue() + " ");
        }
    }
}
class Queue {
    
    
    private  int[] elements;
    private int size;
    public static final int DEFAULT_CAPACITY = 8;

    public Queue(){
    
    
        this(DEFAULT_CAPACITY);
    }
    public Queue(int capacity){
    
    
        elements = new int[capacity];
    }
    public void enqueue(int value){
    
    
        if (size >= elements.length){
    
    
            int[] temp = new int[elements.length * 2];
            System.arraycopy(elements,0,temp,0,elements.length);
            elements = temp;
        }
        elements[size] = value;
        size++;
    }
    public int dequeue(){
    
    
        int key = elements[0];
        for (int i = 0;i < size;i++){
    
    
            elements[i] = elements[i+1];
        }
        size--;
        return key;
    }
    public boolean empty(){
    
    
        return size == 0;
    }
    public int getSize(){
    
    
        return size;
    }
}


  • 结果显示:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 
Process finished with exit code 0

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/jxh1025_/article/details/109295574