package lists; /* * A List is a sequential structure that is either empty or it has a * sequence of nodes such that one node is the first, one node is the * last, and each node except the last has a unique successor. Finally, * one node in a non-empty list is designated as the current node. * * The list supports the following operations: * * List - makes an empty list * * boolean isEmpty() - checks if list is empty * boolean isFull() - checks if List if full * boolean isFirst() - checks if current is the first * boolean isLast() - checks if current is the last * int getSize() - returns number of elements in the list * * void first() - makes current the first * void next() - makes current the successor of current * * void add(Object obj) - adds obj to the list * void delete() - removes current object from list * * void set(Object obj - changes current object to obj * Object get() - returns object at current position */ /* * This implementation of a List uses the Node class, which is defined * as follows: * * Node(Object obj) - makes a node containing obj * Object getData() - returns the node's data object * void setData(Object obj) - sets the node's data to obj * Node getNext() - returns the node's successor * void setNext(Node next) - sets the node's successor */ public class List implements ADTList { private Node start; private Node current; private int size; /* Constructor creates an empty list. * * PRE: none * POST: the list is empty */ public List() { start = null; current = null; size = 0; } /* Checks if list is empty. * * PRE: * POST: */ public boolean isEmpty() { return (start == null); } /* Checks if List if full. * * PRE: * POST: */ public boolean isFull() { return false; } /* Checks if current is the first. * * PRE: list is not empty * POST: */ public boolean isFirst() { return (current == start); } /* Checks if current is the last. * * PRE: * POST: */ public boolean isLast() { return (current.getNext() == null); } /* Returns number of elements in the list. * * PRE: * POST: */ public int getSize() { /* int counter = 0; Node p = start; while (p != null) { counter++; p = p.getNext(); } return counter; */ return size; } /* Makes current the first. * * PRE: * POST: */ public void first() { current = start; } /* Makes current the successor of current. * * PRE: * POST: changes current to its successor, if possible, * else current remains unchanged */ public void next() { if (!isLast()) current = current.getNext(); } /* Adds object to the list. * * PRE: the list is not full * POST: the new object is added after the current one and * becomes the new current object. */ public void add(Object o) { Node p = new Node(o); if (isEmpty()) { start = p; } else { p.setNext(current.getNext()); current.setNext(p); } current = p; size++; } /* Removes current object from list. * * PRE: List must not be empty * POST: current node is removed and the old successor of * current becomes new current node. */ public void delete() { if (start == current) { start = current.getNext(); current = current.getNext(); } else { Node p = start; while (p.getNext() != current) { p = p.getNext(); } p.setNext(current.getNext()); current = current.getNext(); if (current == null) current = p; } size--; } /* Returns object at current position. * * PRE: * POST: */ public Object get() { return current.getData(); } /* Changes current object to obj. * * PRE: * POST: */ public void set(Object obj) { current.setData(obj); } }