001package hw4.problem1;
002
003import java.util.LinkedList;
004import java.util.List;
005
006/**
007 * IntQueue1 is our first implementation of a basic first-in, first-out queue
008 * for Integers.
009 * <p>
010 *
011 * An IntQueue can be described as [n1, n2, ..., n_k], where n1 is the
012 * least-recently-added item in the queue and is the next item to be
013 * removed.  n_k is the most-recently-added and will be the last of the
014 * current elements to be removed.
015 * <p>
016 *
017 * An IntQueue can also be described constructively, with the append operation,
018 * ':', such that [n1, n2, ..., n_k] : n_k+1 is the result of enqueing n_k+1
019 * at the end of the queue.
020 *
021 * @author Krysta Yousoufian
022 */
023public class IntQueue1 {
024    // This class represents a queue as a linked list where the front of
025    // the list corresponds to the front of the queue.
026
027    // Normally, your abstraction function and representation invarant would go
028    // here. For ease of grading, please place them in hw4/answers/problem1.txt
029    // instead with your answers to the other written exercises.
030
031    List<Integer> entries;
032
033    /**
034     * @effects constructs an empty queue
035     */
036    public IntQueue1() {
037        entries = new LinkedList<Integer>();
038        checkRep();
039    }
040
041    /**
042     * Enqueue an item
043     * @param entry item to be added to the queue
044     * @modifies this
045     * @effects places entry at the end of the queue
046                 * @throws IllegalArgumentException if entry is null
047     */
048    public void enqueue(Integer entry) {
049                                if (entry == null) {
050                                                throw new IllegalArgumentException("entry cannot be null");
051                                }
052        entries.add(entry);
053        checkRep();
054    }
055
056    /**
057     * Dequeue an item
058     * @requires size() > 0
059     * @modifies this
060     * @effects removes the item at the front of the queue
061     * @return the item that was first in the queue
062     */
063    public Integer dequeue() {
064        Integer front = entries.remove(0);
065        checkRep();
066        return front;
067    }
068
069    /**
070     * See the next item without removing it
071     * @requires size() > 0
072     * @return the item currently first in the queue
073     */
074    public Integer front() {
075        return entries.get(0);
076    }
077
078    /**
079     *
080     * @return number of elements in the queue
081     */
082    public int size() {
083        return entries.size();
084    }
085
086    /**
087     *
088     * @return size() == 0
089     */
090    public boolean isEmpty() {
091        return entries.isEmpty();
092    }
093
094    public void checkRep() {
095        // If I gave this to you, you wouldn't have the fun of figuring out the
096        // rep invariant for yourself :)
097    }
098}