/* * 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. */ //Copyright Dr. Marek A. Suchenek, 2015 //All rights reserved package split; /** * * @author suchenek */ public class QUEUE1 { List front; // points to "dummy" header // it never changes List rear; // points to the last element // Both variables (above) point one position ahead // in order to facilitate enqueue and dequeue public QUEUE1() //constructs the empty QUEUE consisting of "dummy" header { cnt.incr(); List temp = new List(); temp.head = null; //never used temp.tail = null; front = temp; rear = temp; // rear = front; } public void enqueue(E x) { cnt.incr(); List temp = new List(); temp.head = x; temp.tail = null; rear.tail = temp; rear = temp; // rear = rear.tail; } public E dequeue() { cnt.incr(); if (front == rear) return null; E toReturn = front.tail.head; if(front.tail==rear) rear = front; // dequeue the only element front.tail = front.tail.tail; return toReturn; } public Boolean isEmpty() { return front == rear; } public void dump() { List i = front; System.out.print("Dump:"); while (i!=rear) { System.out.print(i.tail.head + " "); i = i.tail; } System.out.println(" End Dump"); } public QUEUE1 cloneAbstract() // Deep clone { QUEUE1 toReturn = new QUEUE1(); for (List p = front; p !=rear; p = p.tail) { toReturn.enqueue(p.tail.head); } return toReturn; } public QUEUE1 clone() // Deep clone { QUEUE1 toReturn = new QUEUE1(); for (List p = front; p != rear; p = p.tail) { List temp = new List (); temp.head = p.tail.head; temp.tail = null; toReturn.rear.tail = temp; toReturn.rear = temp; } return toReturn; } }