publicclassArrayQueue{ private String[] items; privateint n = 0; privateint head = 0; privateint tail = 0;
publicArrayQueue(int n){ items = new String[n]; this.n = n; }
publicvoidenqueue(String item)throws Exception { if (tail == n) { thrownew Exception("queue is full , cant enter any more !"); } items[tail] = item; tail++; }
public String dequeue()throws Exception { if (head == tail) { thrownew Exception("queue is empty !"); } String ret = items[head]; head++; return ret; }
publicvoidprintQueue(){ for (int i = head; i < tail; i++) { System.out.print(items[i] + " "); } }
publicvoidenqueue2(String item)throws Exception { if (tail == n) { if (head == 0) { thrownew Exception("queue is full , cant enter any more !"); } for (int i = head; i < tail; i++) { items[i - head] = items[i]; } tail -= head; head = 0; } items[tail] = item; tail++; }
publicvoidenqueue(T item){ Node<T> node = new Node<T>(item, null); if (head == null) { head = node; tail = node; }
tail.next = node; tail = node; size++; }
public T dequeue()throws Exception { if (head == null) { thrownew Exception("Queue is empty, no element to dequeue !"); } T ret = head.data; head = head.next; size--; return ret; }
publicvoidprintQueue(){ Node<T> tem = head; while (tem!= null){ System.out.print(tem.data + " "); tem = tem.next; } }