-
Homework help: should be simple for you guys
I go to SDSU and am stuck on my homework for intro to Java (due tonight) and I missed a couple classes so problem solving and the book will make it take past my deadline to solve ..... what am I doing wrong? here is the code I am playing with and I am not sure if I am even on the right track ...... the homework part is noted with HW
<a href="http://www-rohan.sdsu.edu/~masc0500/LECS108/s06hw5.txt"> here is the description of what I am trying to do</a>
class Node
{
public Object data;
public Node next;
public Node()
{
this.data = null;
this.next = null;
}
public Node(Object a)
{
this.data = a;
this.next = null;
}
public Node(Object a, Node b)
{
this.data = a;
this.next = b;
}
}
public class List
{
private Node head;
private Node tail;
public List()
{
this.head = null;
this.tail = null;
}
//-------------------------
public void insertAtFront(Object obj)
{
if (this.size()==0)
{
Node n = new Node(obj);
this.head = n;
this.tail = n;
}
else
{
Node n = new Node(obj);
n.next = this.head;
this.head = n;
}
}
//----------------------------
public void insertAtBack(Object obj)
{
if (this.size()==0)
{
Node n = new Node(obj);
this.head = n;
this.tail = n;
}
else
{
Node n = new Node(obj);
this.tail.next = n;
this.tail = n;
}
}
//------------------------------
public Object removeFromFront()
{
if (this.size()==0)
{
System.out.println("No data to remove.");
return null;
}
else if (this.size()==1)
{
Object a = this.head.data;
this.head = null;
this.tail = null;
return a;
}
else
{
Object a = this.head.data;
this.head = this.head.next;
return a;
}
}
//-----------------------------start HW ----------
public void insertAtSorted(String s)
{
if (this.size()==0)
{
insertAtFront(s);
return;
}
else if (s<=this.head)
{
insertAtFront(s);
return;
}
else if (s>=this.tail) // how to make so this works?
{
insertAtBack(s);
return;
}
else
{
Node ntlnode = this.head;
while (ntlnode.next <= s)
{
ntlnode = ntlnode.next;
Node blah = new Node(s);
blah.next = ntlnode;
this.next = blah;
}
}
}
//----------------------------- end HW -----------
public Object removeFromBack()
{
if (this.size()==0)
{
System.out.println("No data to remove.");
return null;
}
else if (this.size()==1)
{
Object a = this.tail.data;
this.head = null;
this.tail = null;
return a;
}
else
{
Object a = this.tail.data;
Node ntlnode = this.head;
while (ntlnode.next != this.tail)
{
ntlnode = ntlnode.next;
}
this.tail = ntlnode;
this.tail.next = null;
return a;
}
}
//-------------------------
public boolean isEmpty()
{
return this.head == null;
}
//-------------------------
public void print()
{
if (this.size()==0)
{
System.out.println("Empty list!");
return;
}
Node curr = this.head;
while (curr != null)
{
System.out.println(curr.data.toString());
curr = curr.next; //move forward to the next data item.
}
System.out.println();
}
//-------------------------
public int size()
{
int count = 0;
Node curr = this.head;
while (curr != null)
{
count++;
curr = curr.next;
}
return count;
}
//-------------------------
} //end List class
-
this is what my ultimate goal is in the HW section
public void insertAtSorted(String s)
{
if list is currently empty, then issue an insertAtFront(s),
and then exit this routine immediately.
(CS107 reminder: Java's "return;" command will end a subroutine.)
if s's value <= the head node's value,
then just do an insertAtFront(s),
and then exit this routine immediately.
(IMPORTANT HINT: use the data-type conversion functions
we discussed in detail, earlier in the semester!
Also, you may need to "cast" to a String, when you retrieve
the head node's data value, because the Node class's .data property
is declared as data type Object.)
if s's value is >= the tail node's value,
then just do an insertAtBack(s),
and then exit this routine immediately.
Observation: code execution can only get to this location, here,
if s needs to be inserted somewhere in the middle of the list.
So, therefore, you should now "roller coaster" through the list
(using the roller coaster techniques used in the List class's "print" routine
or "size" routine or "removeFromBack" routine). However, you should halt
the roller-coastering as soon as (or right before) you hit a node whose value
is >= s's value. Then, using Java references techniques (discussed earlier
in the semester), you should create a new node having s as its "data",
and should manually insert this new node right before the node that halted you.
}
-
to do your comparison, you must use the String class's compareTo method. So if
Code:
{
if( s.compareTo(head.data) <= 0 ) // returns neg val if s is "less than", 0 if equal
{
this.insertFront( s );
}
else if ( s.compareTo(tail.data) >= 0 ) // returns pos val if s is "greater than"
{
this.insertTail( s );
}
else
{
this.insertSearch( s );
}
}
Similar Threads
-
By Ant_Magma in forum Java
Replies: 5
Last Post: 09-26-2005, 03:41 AM
-
By BAW264 in forum VB Classic
Replies: 3
Last Post: 05-09-2005, 09:20 PM
-
By Ranchoz71 in forum ASP.NET
Replies: 3
Last Post: 04-06-2005, 12:40 PM
-
By slimasian in forum VB Classic
Replies: 1
Last Post: 03-02-2005, 01:38 PM
-
Replies: 0
Last Post: 03-21-2001, 04:01 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks