/* Research Project: Graphical Database for Category Theory J. Bradbury, Dr. R. Rosebrugh, I. Rutherford Mount Allison University 2001 File: CategoryList.java Description: This class implements a list of ListNode classes which contain categories. */ //import statements import java.awt.*; import java.applet.Applet; class CategoryList //class DefList definition { CategoryNode head; //Points to front of LinkedList CategoryNode tail; //Points to back of LinkedList public void CategoryList() //List Constructor { head = tail = null; } public void insertNode(Category insertItem) //Inserts node at end of linked list { if (isEmpty()) head = tail = new CategoryNode(insertItem); else tail = tail.next = new CategoryNode(insertItem); } public void getList(TextArea view) //Retrieves list of categorys in linked list { int count = 0; CategoryNode current = head; view.append("\nOpen Categories\n"); view.append("------------------\n"); while(current != null) { count++; view.append(" - " +current.cat.name + "\n"); //view.appendText(count + ". " + current.cat.name + "\n"); current = current.next; } view.append("------------------\n\n"); } public void replaceNode(Category c) //Replaces a node with an updated version of the same node { CategoryNode current = head; while(current !=null) { if (c.name.equals(current.cat.name)) { current.cat = c; } current = current.next; } } public Category getNode(String name) //returns a Category when given Category's name { Category getItem = null; CategoryNode current = head; while(current != null) { if (name.equals(current.cat.name)) { getItem = current.cat; return getItem; } current = current.next; } getItem = new Category(); getItem.name = ""; return getItem; //return null if category not in list } public boolean removeNode(String name) throws EmptyListException //Removes node from end of linked list { Category removeItem = null; CategoryNode current = head; CategoryNode previous = null; if (isEmpty()) throw new EmptyListException(); if (name.equals(head.cat.name)) { if (head.equals(tail)) head = tail = null; else head = head.next; return true; } else { //current.next = head; while(current != null) { if (name.equals(current.next.cat.name)) { current.next = current.next.next; //previous = current; //previous is node before one being removed //current = current.next; //current is node being removed //previous.next = current.next; //previous.next points to current.next return true; } current = current.next; } } return false; //if (head.equals(tail)) //head = tail = null; } public boolean isEmpty() //Returns if the linked list is empty //That is, if the head of the list points to null { return head == null; } /* public void displayAll() { //if (isEmpty()) //{ //Message saying is empty displayed on the screen //} ListNode current = head; while (current != null) { //display category current = current.next; } } */ public Category removeHead() //Removes the category at the front of the list and //returns it { Category removeItem = head.cat; if (head.equals(tail)) head = tail = null; else head = head.next; return removeItem; } public boolean doesExist(String name) // Returns true if a category of the given name is in // the list. Otherwise returns false. { if (head == null) return false; CategoryNode temp = head; while (temp.next != null) { if (temp.cat.name.equals(name)) return true; temp = temp.next; } return false; } }