2013-03-04 53 views
0

我只是好奇,我有一個類實現了一個隊列,它目前有一個項目...我很好奇,哪裏將是最有效或最乾淨的位置來初始化其他項目,我希望添加到隊列中......這是作業的一部分,所以請提供解釋,而不僅僅是回答!在此先感謝...這是我建立了類...在Java中執行隊列

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(); 
    } 
} 
+1

你對此事的看法? – 2013-03-04 17:27:23

+4

爲什麼'Object'而不是'T'到處都是? – 2013-03-04 17:27:28

+1

你確定你可以使用'LinkedList'來操縱你的隊列嗎?它基本上是一個包裝,可能你的教授不會接受它。 – 2013-03-04 17:28:09

回答

0

這是怎麼寫的隊列:

/* 
* 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'); 
    } 
}