Chapter 10 Problem 10 (Queue class)

Chapter 10 Problem 10 (Queue class)

  • * 10.10 (Queue class) Section 10.6 gives a Stack class. Design a class called Queue to store integers. Like a stack, a queue holds elements. In the stack, elements are acquired in a "last in, first out" manner. In the queue, elements are acquired in a "first in, first out" manner. This class contains:

    • A data field of type int[] named element that holds the int value in the queue.
    • A data field named size holds the number of elements in the queue.
    • A construction method to create a Queue object with a default capacity of 8.
    • The method enqueue(int v) is used to add v to the queue.
    • The method dequeue() is used to remove an element from the queue and return it.
    • The method empty(), if the queue is empty, the method returns true.
    • The method getSize() returns the size of the queue.

    Draw the UML diagram of this class and implement this class. The initial array size is 8. Once the number of elements exceeds the array size, the array size 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 to add 20 numbers from 1 to 20 to the queue, then remove these numbers and display them.
    *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.

  • Reference Code:

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


  • The results show that:
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

Insert picture description here

Guess you like

Origin blog.csdn.net/jxh1025_/article/details/109295574