队列在Java中的实现

队列在Java中的实现,java,oop,linked-list,queue,Java,Oop,Linked List,Queue,我只是好奇,我有一个实现队列的类,它当前有一个项。。。我很好奇,在哪里可以最有效或最干净地初始化我希望添加到队列中的其他项目。。。这是作业的一部分,因此请提供解释,而不仅仅是回答!先谢谢你。。。这是我创建的类 import java.util.*; public class Queue<T> extends Node<T> { private LinkedList<T> list; // Queue constructor publ

我只是好奇,我有一个实现队列的类,它当前有一个项。。。我很好奇,在哪里可以最有效或最干净地初始化我希望添加到队列中的其他项目。。。这是作业的一部分,因此请提供解释,而不仅仅是回答!先谢谢你。。。这是我创建的类

import java.util.*;

public class Queue<T> extends Node<T> {
    private LinkedList<T> list;

    // Queue constructor
    public Queue()  {
        // Create a new LinkedList.
        list = new LinkedList<T>();
    }
    //check if empty
    public boolean isEmpty() {
        return (list.size() == 0);
    }
    //add items to queue
    public void enqueue(Object item) {
        // Append the item to the end of our linked list.
        list.add((T) item);
    }
    //remove items from queue
    public T dequeue() {

        T item = list.get(1);
        list.remove(1);     
        // Return the item
        return item;
    }
    //check top item
    public T peek() {
        return list.get(1);
    }
    //check size of queue
    public int size() {
        return list.size();
    }
}
import java.util.*;
公共类队列扩展节点{
私有链接列表;
//队列构造函数
公共队列(){
//创建一个新的LinkedList。
列表=新的LinkedList();
}
//检查是否为空
公共布尔值为空(){
返回值(list.size()=0);
}
//将项目添加到队列
公共无效排队(对象项){
//将项目附加到链接列表的末尾。
列表。添加((T)项);
}
//从队列中删除项目
公共T出列(){
T item=list.get(1);
列表。删除(1);
//退货
退货项目;
}
//勾选最上面的项目
公共T peek(){
返回列表。获取(1);
}
//检查队列的大小
公共整数大小(){
返回list.size();
}
}

以下是编写队列的方法:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Queue;

import java.util.*;

/**
 *
 * @author Madhurlucky
 */
public class arrayQueue {

    public static int[] Queue;
    public static int len, front, rear, size;

    public arrayQueue(int n) {
        len = 0;
        size = n;
        front = -1;
        rear = -1;
        Queue = new int[size];
    }

    public boolean isEmpty() {
        return front == -1;
    }

    public boolean isFull() {
        return (front == 0 && rear == size - 1);
    }

    public int getsize() {
        return len;
    }

    public void insert(int i) {
        if (rear == -1) {
            rear = front = 0;
            Queue[rear] = i;
        } else if (rear + 1 >= size) {
            System.out.println("OverFlow Queue");
        } else if (rear + 1 < size) {
            Queue[++rear] = i;
        }
        len++;
    }

    public int remove() {
        int x = -99;
        if (front != -1) {
            x = Queue[front];
            if (front == rear) {
                front = rear = -1;
            } else if (front < rear) {
                front += 1;
            }
            len--;
        }
        return x;
    }

    /*  Function to check the front element of the queue */
    public int peek() {
        if (isEmpty()) {
            throw new NoSuchElementException("Underflow Exception");
        }
        return Queue[front];
    }

    public void display() {
        System.out.print("\nQueue = ");
        if (len == 0) {
            System.out.print("Empty\n");
            return;
        }
        for (int i = front; i <= rear; i++) {
            System.out.print(Queue[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("Array Queue Test\n");
        System.out.println("Enter Size of Integer Queue ");
        int n = scan.nextInt();
        /* creating object of class arrayQueue */
        arrayQueue q = new arrayQueue(n);
        /* Perform Queue Operations */
        char ch;
        do {
            System.out.println("\nQueue Operations");
            System.out.println("1. insert");
            System.out.println("2. remove");
            System.out.println("3. peek");
            System.out.println("4. check empty");
            System.out.println("5. check full");
            System.out.println("6. size");
            int choice = scan.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("Enter integer element to insert");
                    try {
                        q.insert(scan.nextInt());
                    } catch (Exception e) {
                        System.out.println("Error : " + e.getMessage());
                    }
                    break;
                case 2:
                    try {
                        System.out.println("Removed Element = " + q.remove());
                    } catch (Exception e) {
                        System.out.println("Error : " + e.getMessage());
                    }
                    break;
                case 3:
                    try {
                        System.out.println("Peek Element = " + q.peek());
                    } catch (Exception e) {
                        System.out.println("Error : " + e.getMessage());
                    }
                    break;
                case 4:
                    System.out.println("Empty status = " + q.isEmpty());
                    break;
                case 5:
                    System.out.println("Full status = " + q.isFull());
                    break;
                case 6:
                    System.out.println("Size = " + q.getsize());
                    break;
                default:
                    System.out.println("Wrong Entry \n ");
                    break;
            }
            /* display Queue */
            q.display();
            System.out.println("\nDo you want to continue (Type y or n) \n");
            ch = scan.next().charAt(0);

        } while (ch == 'Y' || ch == 'y');
    }
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包队列;
导入java.util.*;
/**
*
*@author-madhullucky
*/
公共类数组队列{
公共静态int[]队列;
公共静电内胆,前,后,尺寸;
公共阵列队列(int n){
len=0;
尺寸=n;
正面=-1;
后部=-1;
队列=新整数[大小];
}
公共布尔值为空(){
返回前端==-1;
}
公共布尔值isFull(){
返回(前==0和后==大小-1);
}
公共int getsize(){
回程透镜;
}
公共空白插入(int i){
如果(后==-1){
后=前=0;
队列[后部]=i;
}否则如果(后部+1>=尺寸){
System.out.println(“溢出队列”);
}否则,如果(后部+1<尺寸){
队列[++后方]=i;
}
len++;
}
公共int-remove(){
int x=-99;
如果(前!=-1){
x=队列[前端];
如果(前==后){
前=后=-1;
}否则,如果(前<后){
正面+=1;
}
蓝--;
}
返回x;
}
/*函数检查队列的前端元素*/
公共int peek(){
if(isEmpty()){
抛出新的NoTouchElementException(“下溢异常”);
}
返回队列[前面];
}
公共空间显示(){
系统输出打印(“\nQueue=”);
如果(len==0){
System.out.print(“空\n”);
返回;
}

对于(inti=front;i你对这件事有什么想法?为什么
对象
而不是
T
无处不在?你确定你可以使用
链接列表
来操纵你的队列吗?它基本上是一个包装器,可能你的教授不会接受。IMO:为什么
队列扩展节点
?队列
应该基于一个li链接的
节点列表
@HotLicks是否在队列初始化下初始化它们?