Expand|Select|Wrap|Line Numbers
- package sortedlist;
- public interface Keyable {
- public int getKey();
- public boolean lessThan(Keyable x);
- }
- /* ListNode.java */
- package sortedlist;
- /**
- * ListNode is a class used internally by the List class. Each node in a
- * List is represented as a ListNode, with an item and a reference to the
- * next node in the list.
- **/
- class ListNode {
- Keyable item;
- ListNode next;
- /**
- * Constructs a ListNode with item obj and next null.
- * @param obj will be the item in the node.
- **/
- ListNode(Keyable obj) {
- item = obj;
- next = null;
- }
- /**
- * Constructs a ListNode with item obj and next n.
- * @param obj will be the item in the node.
- * @param n will be the next ListNode in the list.
- **/
- ListNode(Keyable obj, ListNode n) {
- item = obj;
- next = n;
- }
- /**
- * ptrTo() returns a reference to the node at the given position. If
- * position < 1 or position > the number of nodes in the list, returns
- * null. Assumes the list is acyclic.
- * @return a reference to the node at position "position".
- */
- public ListNode ptrTo(int position) {
- if (position < 1) {
- return null;
- } else if (position == 1) {
- return this;
- } else if (next == null) {
- return null;
- } else {
- return next.ptrTo(position - 1);
- }
- }
- }
- /* SortedList.java */
- package sortedlist;
- import java.util.Enumeration;
- /**
- * The SortedList class is a singly-linked implementation of a linked list in
- * sorted order. SortedLists are mutable data structures that can grow at
- * either end.
- * @author Kathy Yelick, Bob Zasio
- **/
- public class SortedList {
- private int size;
- ListNode head;
- /**
- * Construct an empty list
- **/
- public SortedList() {
- size = 0;
- head = null;
- }
- /**
- * isEmpty() returns true if this list is empty, false otherwise.
- * @return true if the list is empty, false otherwise.
- **/
- public boolean isEmpty() {
- return (size == 0);
- }
- /**
- * length() returns the length of this list.
- * @return the length of the list.
- **/
- public int length() {
- return size;
- }
- /**
- * insert() inserts the element x into the proper sorted location.
- **/
- public void insert(Keyable x) {
- ListNode newnode = new ListNode(x, null);
- if (head == null) {
- head = newnode;
- } else if (!head.item.lessThan(x)) {
- newnode.next = head;
- head = newnode;
- } else {
- ListNode temp = head;
- while (temp.next != null) {
- if (!temp.next.item.lessThan(x)) {
- newnode.next = temp.next;
- temp.next = newnode;
- temp = temp.next;
- break;
- }
- temp = temp.next;
- }
- if (temp.next == null) {
- temp.next = newnode;
- }
- }
- size++;
- }
- /**
- * Keyable() returns the element with the given key, or null if none of the
- * elements have that key.
- **/
- public Keyable find(int key) {
- ListNode temp = head;
- while (temp != null) {
- if (temp.item.getKey() == key) {
- return temp.item;
- }
- temp = temp.next;
- }
- return null;
- }
- /**
- * elements() returns an Enumeration of the components of this list.
- * @return an Enumeration of the components of this list.
- **/
- public Enumeration elements() {
- return new ListEnum(head);
- }
- /**
- * toString() returns a String representation of this list.
- * @return a String representation of this list.
- **/
- public String toString() {
- int i;
- Object obj;
- String result = "[ ";
- ListNode cur = head;
- while (cur != null) {
- obj = cur.item;
- result = result + obj.toString() + " ";
- cur = cur.next;
- }
- result = result + "]";
- return result;
- }
- }
1.What are the errors above?
2. Can we use the interface Keyable as used above in the class ListNode?