//////////////////////////////////////////////////////////////////////////////// // Author: Julie Petrusa (991339) // // Program 2, Due date: February 13, 2001 // // The class CLList implements a circular linked list // // Fields: head, tail, current, and size // // Methods: - isEmpty, which checks if the list is empty // // - firstInsert, which inserts a new node as the first node into // // the sofar empty list // // - addAfter, which adds a new node after the current node // // - remove, which removes the node pointed to by current // // - lookup, which determines if a node is in the list or not // // - start, which sets the current pointer to the starting node and // // returns the node that current points to // // - node, which returns the node that current points to // // - advance, which advances the current pointer n steps // // - length, which returns the number of items in the list // // - printToFile, which cycles through the list and prints the data // // content of the list items // // Known bugs: None // //////////////////////////////////////////////////////////////////////////////// import java.io.*; // needed in order to do io // this allows you to use the classes for reading file input class CLList { //------------------------------------------------------------------------ // fields private Listnode head; // starting point of the list private Listnode tail; // ending point of the list private Listnode current; // current item in the list private int size; // number of items in the list //------------------------------------------------------------------------ // constructor // -initialize the list as null // -initialize current as null // -initialize the size as 0 public CLList() { head = null; // no items on list yet tail = null; current = null; size = 0; } //------------------------------------------------------------------------ // method isEmpty // -checks if the list is empty public boolean isEmpty() { return head == null; // true if no nodes } //------------------------------------------------------------------------ // method firstInsert // -creates a new node with ob as its data // -inserts that node as first node into the sofar empty list // -updates current to the new node and increments size public void firstInsert(Object ob) { Listnode newNode = new Listnode(ob); // make new node if(isEmpty()) // if empty list, tail = newNode; // newNode <-- tail else // if not empty list, head.previous = newNode; // newNode <-- old head newNode.next = head; // newNode --> old head head = newNode; // head --> newNode current = newNode; // current --> newNode size++; // increment size } //------------------------------------------------------------------------ // method addAfter // -creates a new node with stuff as its data // -adds the new node after the current node // -doesn't work if list is empty, print a message // -current afterwards points to the new node // -updates size public void addAfter(Object stuff) { Listnode newNode = new Listnode(stuff); // make new node if(isEmpty()) // if empty list, { // print message System.out.println(); System.out.println("The list is empty!"); } if(current == tail) // if last node, { newNode.next = null; // newNode --> null tail = newNode; // newNode <-- tail } else // not last node, { newNode.next = current.next; // newNode --> old next current.next.previous = newNode; // newNode <-- old next } newNode.previous = current; // old current <-- newNode current.next = newNode; // old current --> newNode current = newNode; // current --> newNode size++; // increment size } //------------------------------------------------------------------------ // method remove // -removes the node pointed to by current // -after that, current points to the node after the removed node // -if the only node in the list is removed, current will be null // and the list will be null as well // -if the list is empty, prints a message // -updates size public void remove() { if(isEmpty()) // if empty list, { // print message System.out.println(); System.out.println("The list is empty!"); } if(current == head) // if first node, head = current.next; // head --> old next else current.previous.next = current.next; // not first, // old previous --> old next if(current == tail) { // if last node, tail = current.previous; // old previous <-- tail current = current.previous; // current --> old previous } else // not last, { // old previous <-- old next current.next.previous = current.previous; current = current.next; // current --> old next } size--; // increment size } //------------------------------------------------------------------------ // method lookup // -cycles through the list and compares stuff with the data field of the // current Listnode // -if the stuff was found in the list, returns true, // otherwise returns false public boolean lookup(Object stuff) { boolean value = true; // initialize Listnode probe = head; // point to first node while( (!(probe.data.equals(stuff))) && (value) ) { // while stuff not found and not end of list, probe = probe.next; // point to next node if(probe == null) // if end of list, value = false; // stuff was not found } return value; // only true if found } //------------------------------------------------------------------------ // method start // -sets the current pointer to the starting node // -returns the node that current points to // -if list is empty, prints a message that list is empty public Listnode start() { current = head; // current points to first node if(isEmpty()) // if empty list, print message System.out.println("The list is empty!"); return current; // returns the new current } //------------------------------------------------------------------------ // method node // -returns the node that current points to // -if list is empty, prints a message public Listnode node() { if(isEmpty()) // if empty list, print message System.out.println("The list is empty!"); return current; // returns node pointed to by current } //------------------------------------------------------------------------ // method advance // -advances the current pointer n steps // -if list is empty, prints a message public void advance(int n) { if(isEmpty()) // if empty list, print message System.out.println("The list is empty!"); for(int i=0; i