• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

node class for linked list

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following code and want to check the methods. I know I have to create the nodes but I don't know what to put in the brackets to do this. can anyone help?
I'm really getting brain cramp.

I've tried putting Object and nextval and element, etc. but that evidently isn't correct becaus I keep getting errors with everything I've tried. How do I test Please!!!
Thanks in advance for any advice.
[This message has been edited by Ronald Pruitt (edited January 28, 2001).]
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is how i designed by own node class
[<code>]
private class Node {
private Node next;
private Node previouse;
private Object data;
private void setNext( Object o ) {
next = ( Node )o;
}
private void setPreviouse( Object o ) {
previouse = ( Node )o;
}
private void setData( Object o ) {
data = o;
}
private Node getNext() { return next;}
private Node getPreviouse() { return previouse;}
private Object getData() { return data;}
}
[</code>]
 
Ronald Pruitt
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thank you but that doesn't explain how to test the methods, how do I test the methods without creating a couple of nodes, and how do I do that?
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing is this is just a part of it. The Node class will just hold a one object . But what you now need is , is to design a list object which will bind nodes together.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ronald,
Val is right... you have to make a linked list out of your nodes to test them. Here's something you could do with the Link class you posted :

Hope this helps,
-Nate
 
Ronald Pruitt
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes thank you both, you see I have an instructor that is very vague as to what he expects. Others have called him sadistic but I'm inclined to believe that sometimes those that can do can't teach and visa versa. I don't think he was meant to teach.
Thanks again.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or he's probably getting the students to write these programs, so he can cut-n-paste them into his new JAVA book.
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i can post a code for that i did once ago , which i must say is not good but it worked , it was one of the first assignments i had done. Some teachers might know stuff but don't know how to present it to the students. In my school i have this great guy with only 3 years of field experience this guy is great , you won't even have to take notes he explains everything very well so you wouldn't second guess it. Too bad i didn't attend his class when he covered OOP stuff
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is my version of linked list don't laugh now
<code>
import java.util.NoSuchElementException;
public class ListTest {
private Node start,end,data,cursor;
private Object next,previouse;
private static int size;
protected final Object START = start;
protected final Object END = end;
private class Node {
private Node next;
private Node previouse;
private Object data;
private void setNext( Object o ) {
next = ( Node )o;
}
private void setPreviouse( Object o ) {
previouse = ( Node )o;
}
private void setData( Object o ) {
data = o;
}
private Node getNext() { return next;}
private Node getPreviouse() { return previouse;}
private Object getData() { return data;}
}
public void addElement( Object o ) {
if ( isEmpty() ) {
start = end = new Node();
start.setData( o );
}
else {
Node temp = end;
end = new Node();
setCursor( temp );
getCursor().setNext( end );
end.setPreviouse( temp );
}
end.setData( o );
setCursor( end );
size++;
}
private void setCursor( Node current ) { cursor = current; }
private Node getCursor() { return cursor; }
public boolean isEmpty() { return getSize() == 0; }
public Object getLast() { return end.getData(); }
public Object getFirst() { return start.getData(); }
public Object previouseElement() throws NoSuchElementException {
if ( getSize() == 0 ) throw new NoSuchElementException(
"No Previouse Record");
if ( getCursor().getPreviouse() == null )
setCursor( end );
else
setCursor( getCursor().getPreviouse() );
return getCursor().getData();
}
public Object nextElement() throws NoSuchElementException {
if ( getSize() == 0 ) throw new NoSuchElementException(
"No Next Record");
if ( getCursor().getNext() == null )
setCursor( start );
else
setCursor( getCursor().getNext() );
return getCursor().getData();
}
public void reset( Object pos ) throws NoSuchElementException {
if ( getCursor() == null ) throw new NoSuchElementException(
"No Record");
setCursor( ( Node )pos );
}
public boolean hasMoreElements() {
return getCursor().getNext() != null;
}
public void remove() throws NoSuchElementException {
if ( getCursor() == null ) throw new NoSuchElementException(
"No Records");
if ( ( getCursor().getPreviouse() != null ) && ( getCursor().getNext() != null ) ) {
Node prev = getCursor().getPreviouse();
Node next = getCursor().getNext();
next.setPreviouse( prev );
prev.setNext( next );
setCursor( next );
}
else {
if ( getCursor().getPreviouse() == null && getSize() > 1) {
start = getCursor().getNext();
start.setPreviouse( null );
setCursor( start );
}
else if ( getCursor().getNext() == null && getSize() > 1) {
end = getCursor().getPreviouse();
end.setNext( null );
setCursor( end );
}
else
start = end = cursor = null;
}
size--;
}
public Object currentElement() throws NoSuchElementException {
if ( getCursor() == null ) throw new NoSuchElementException(
"No Corrent Element");
return getCursor().getData();
}
public int getSize() {
return size;
}
}
</code>
 
reply
    Bookmark Topic Watch Topic
  • New Topic