Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mia023
    New Member
    • May 2007
    • 89

    #1

    Help

    Hello Everyone I have a couple of questions concerning the following chunk of code below:

    [CODE=java]package sortedlist;

    public interface Keyable {
    public int getKey();
    public boolean lessThan(Keyabl e 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(Keyabl e 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(Keyabl e 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(posi tion - 1);
    }
    }
    }


    /* SortedList.java */

    package sortedlist;
    import java.util.Enume ration;

    /**
    * 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.les sThan(x)) {
    newnode.next = head;
    head = newnode;
    } else {
    ListNode temp = head;
    while (temp.next != null) {
    if (!temp.next.ite m.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.getK ey() == 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;
    }
    }[/CODE]


    1.What are the errors above?
    2. Can we use the interface Keyable as used above in the class ListNode?
    Last edited by r035198x; Mar 26 '08, 03:37 PM. Reason: added code tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    What did Kathy Yelick and Bob Zasio say about it?

    kind regards,

    Jos

    Comment

    • mia023
      New Member
      • May 2007
      • 89

      #3
      Originally posted by JosAH
      What did Kathy Yelick and Bob Zasio say about it?

      kind regards,

      Jos
      Don't know I don't really know them but can you be of assistance please because I have an exam for next week and need to know the answer.
      Thank you

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by mia023
        Don't know I don't really know them but can you be of assistance please because I have an exam for next week and need to know the answer.
        Thank you
        Read the comments in your .. I mean in the code.

        Comment

        • mia023
          New Member
          • May 2007
          • 89

          #5
          Originally posted by r035198x
          Read the comments in your .. I mean in the code.
          How did they use the interface keyable in the ListNode I didn't understand that.

          Comment

          • chaarmann
            Recognized Expert Contributor
            • Nov 2007
            • 785

            #6
            it's not a good idea to post code here that you have not written, but just copied from somewhere. If you don't understand foreign code, how can you then understand our response? We are here to help you and not to debug foreign code. Try writing your own code, or you will never learn. It's a very, very bad idea to use foreign code without understanding it.

            If you have problems with your own code, post it here. And then when we tell you to change here and there, you will be able to understand our answer.

            Also, don't post millions of lines of code and ask us to help you for hundreds of errors inside.
            Just post a few line only which have a single error. Don't list all other lines that have nothing to do with the error. I mean, try to delete code lines by lines from the problem until only the single problem stands out.

            Comment

            Working...