001 package aima.util; 002 003 import java.util.LinkedList; 004 import java.util.List; 005 006 /** 007 * @author Ravi Mohan 008 * 009 */ 010 011 /** 012 * Artificial Intelligence A Modern Approach (2nd Edition): page 71. 013 * 014 * The operations on a queue are as follows: MAKE-QUEUE(element,...) creates a 015 * queue with the given element(s). EMPTY?(queue) returns true only if there are 016 * no more elements in the queue. FIRST(queue) returns the first element of the 017 * queue. REMOVE-FIRST(queue) returns FIRST(queue) and removes it from the 018 * queue. INSERT(element, queue) inserts an element into the queue and returns 019 * the resulting queue. (We will see that different types of queues insert 020 * elements in different orders.) INSERT-ALL(elements, queue) inserts a set of 021 * elements into the queue and returns the resulting queue. 022 */ 023 024 public class AbstractQueue implements Queue { 025 protected LinkedList<Object> linkedList; 026 027 public AbstractQueue() { 028 linkedList = new LinkedList<Object>(); 029 } 030 031 public void addToFront(Object n) { 032 linkedList.addFirst(n); 033 } 034 035 public void addToBack(Object n) { 036 linkedList.addLast(n); 037 } 038 039 public void addToFront(List list) { 040 for (int i = 0; i < list.size(); i++) { 041 addToFront(list.get(list.size() - 1 - i)); 042 } 043 } 044 045 public void addToBack(List list) { 046 for (int i = 0; i < list.size(); i++) { 047 addToBack(list.get(i)); 048 } 049 } 050 051 public Object removeFirst() { 052 return (linkedList.removeFirst()); 053 } 054 055 public Object removeLast() { 056 return (linkedList.removeLast()); 057 } 058 059 public Object getFirst() { 060 return (linkedList.getFirst()); 061 } 062 063 public Object getLast() { 064 return (linkedList.getLast()); 065 } 066 067 public boolean isEmpty() { 068 return linkedList.isEmpty(); 069 } 070 071 public int size() { 072 return linkedList.size(); 073 } 074 075 public List asList() { 076 return linkedList; 077 } 078 079 public void add(Object anItem) { 080 throw new RuntimeException("must be implemented by subclasses"); 081 } 082 083 public void add(List items) { 084 throw new RuntimeException("must be implemented by subclasses"); 085 } 086 087 public Object remove() { 088 throw new RuntimeException("must be implemented by subclasses"); 089 } 090 091 public Object get() { 092 throw new RuntimeException("must be implemented by subclasses"); 093 } 094 095 }