473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help

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

Expand|Select|Wrap|Line Numbers
  1. package sortedlist;
  2.  
  3. public interface Keyable {
  4.     public int getKey();
  5.     public boolean lessThan(Keyable x);
  6. }
  7.  
  8.  
  9. /* ListNode.java */
  10.  
  11. package sortedlist;
  12.  
  13. /**
  14.  *  ListNode is a class used internally by the List class.  Each node in a
  15.  *  List is represented as a ListNode, with an item and a reference to the
  16.  *  next node in the list.
  17.  **/
  18. class ListNode {
  19.   Keyable item;
  20.   ListNode next;
  21.  
  22.   /**
  23.    *  Constructs a ListNode with item obj and next null.
  24.    *  @param obj will be the item in the node.
  25.    **/
  26.   ListNode(Keyable obj) {
  27.     item = obj;
  28.     next = null;
  29.   }
  30.  
  31.   /**
  32.    *  Constructs a ListNode with item obj and next n.
  33.    *  @param obj will be the item in the node.
  34.    *  @param n will be the next ListNode in the list.
  35.    **/
  36.   ListNode(Keyable obj, ListNode n) {
  37.     item = obj;
  38.     next = n;
  39.   }
  40.  
  41.   /**
  42.    *  ptrTo() returns a reference to the node at the given position.  If
  43.    *  position < 1 or position > the number of nodes in the list, returns
  44.    *  null.  Assumes the list is acyclic.
  45.    *  @return a reference to the node at position "position".
  46.    */
  47.   public ListNode ptrTo(int position) {
  48.     if (position < 1) {
  49.       return null;
  50.     } else if (position == 1) {
  51.       return this;
  52.     } else if (next == null) {
  53.       return null;
  54.     } else {
  55.       return next.ptrTo(position - 1);
  56.     }
  57.   }
  58. }
  59.  
  60.  
  61. /* SortedList.java */
  62.  
  63. package sortedlist;
  64. import java.util.Enumeration;
  65.  
  66. /**
  67.  *  The SortedList class is a singly-linked implementation of a linked list in
  68.  *  sorted order.  SortedLists are mutable data structures that can grow at
  69.  *  either end.
  70.  *  @author Kathy Yelick, Bob Zasio
  71.  **/
  72. public class SortedList {
  73.   private int size;
  74.   ListNode head;
  75.  
  76.   /**
  77.    *  Construct an empty list
  78.    **/
  79.   public SortedList() {
  80.     size = 0;
  81.     head = null;
  82.   }
  83.  
  84.   /**
  85.    *  isEmpty() returns true if this list is empty, false otherwise.
  86.    *   @return true if the list is empty, false otherwise.
  87.    **/
  88.   public boolean isEmpty() {
  89.     return (size == 0);
  90.   }
  91.  
  92.   /**
  93.    *  length() returns the length of this list.
  94.    *  @return the length of the list.
  95.    **/
  96.   public int length() {
  97.     return size;
  98.   }
  99.  
  100.   /**
  101.    *  insert() inserts the element x into the proper sorted location.
  102.    **/
  103.   public void insert(Keyable x) {
  104.     ListNode newnode = new ListNode(x, null);
  105.  
  106.     if (head == null) {
  107.       head = newnode;
  108.     } else if (!head.item.lessThan(x)) {
  109.       newnode.next = head;
  110.       head = newnode;
  111.     } else {
  112.       ListNode temp = head;      
  113.       while (temp.next != null) {
  114.     if (!temp.next.item.lessThan(x)) {
  115.       newnode.next = temp.next;
  116.       temp.next = newnode;
  117.       temp = temp.next;
  118.       break;
  119.     }
  120.     temp = temp.next;
  121.       }
  122.       if (temp.next == null) {
  123.     temp.next = newnode;
  124.       }
  125.     }
  126.     size++;
  127.   }
  128.  
  129.   /**
  130.    *  Keyable() returns the element with the given key, or null if none of the
  131.    *  elements have that key.
  132.    **/
  133.   public Keyable find(int key) {
  134.     ListNode temp = head;
  135.     while (temp != null) {
  136.       if (temp.item.getKey() == key) {
  137.     return temp.item;
  138.       }
  139.       temp = temp.next;
  140.     }
  141.     return null;
  142.   }
  143.  
  144.   /**
  145.    *  elements() returns an Enumeration of the components of this list.
  146.    *  @return an Enumeration of the components of this list.
  147.    **/
  148.   public Enumeration elements() {
  149.     return new ListEnum(head);
  150.   }
  151.  
  152.   /**
  153.    *  toString() returns a String representation of this list.
  154.    *  @return a String representation of this list.
  155.    **/
  156.   public String toString() {
  157.     int i;
  158.     Object obj;
  159.     String result = "[  ";
  160.  
  161.     ListNode cur = head;
  162.  
  163.     while (cur != null) {
  164.       obj = cur.item;
  165.       result = result + obj.toString() + "  ";
  166.       cur = cur.next;
  167.     }
  168.     result = result + "]";
  169.     return result;
  170.   }
  171. }

1.What are the errors above?
2. Can we use the interface Keyable as used above in the class ListNode?
Mar 26 '08 #1
5 1122
JosAH
11,448 Expert 8TB
What did Kathy Yelick and Bob Zasio say about it?

kind regards,

Jos
Mar 26 '08 #2
mia023
89
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
Mar 26 '08 #3
r035198x
13,262 8TB
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.
Mar 26 '08 #4
mia023
89
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.
Mar 26 '08 #5
chaarmann
785 Expert 512MB
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.
Mar 26 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.