Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

Defining my own LinkedList Class

Thread view: 
Elizabeth - 27 Feb 2004 00:46 GMT
I'm a java student, and I've been given an assignment to implement a
singly linked list with references to the first and last node in the
list. I'm attempting to write the removeLast() method (part of the
assignment) and I can't figure out how to traverse the list. I think
I'm to use a list iterator, but I don't really understand how they
work. I'd love a quick instruction on how they work and whether it's
possible to use an iterator on a user defined class.

Thanks bunches!!

Elizabeth Bendler
Madison, NJ
Mark Haase - 27 Feb 2004 05:56 GMT
> I'm a java student, and I've been given an assignment to implement a
> singly linked list with references to the first and last node in the
[quoted text clipped - 3 lines]
> work. I'd love a quick instruction on how they work and whether it's
> possible to use an iterator on a user defined class.

I assume that you have two classes, one is called something like Node
(possilby an inner class of LinkedList) and looks like this:

class Node {
  Node next;
  int data;

  Node(data) {
     ...
  }

  Node getNext() {
     return this.next;
  }

  ...

}

and a class called something like LinkedList like this:

class LinkedList {
  Node startNode;

  findNode() {
     ...
  }

  // etc
}

To traverse the list, all you need to do is call
startNode.getNext().getNext().getNext()...etc until it returns null.
Then the last non-null object you pulled is the last object in the list.

I think by "iterator" you mean an object enumerator? You can implement
the Enumeration interface in your linked list if you want to do that.
Then you can say:

while (linkedListEnum.hasMoreElements())
  linkedListEnum.getElement();

to get through all the objects in the linked list.

|\/|  /|  |2  |<
mehaase(at)sas(dot)upenn(dot)edu
Adam - 27 Feb 2004 07:52 GMT
> In article <9c9c6ba9.0402261646.6210ccf7@posting.google.com>,

[cut useful examle]

> I think by "iterator" you mean an object enumerator? You can implement
> the Enumeration interface in your linked list if you want to do that.

Why do you want to make her use deprecated api?
Enumerations were replaced by Iterators in java2.
(Still the advice stays helpful - that the list should
have a method returning an object implementing Iterator:
Iterator iter = myList.iterator();
for traversing list's elements).

Adam
Elizabeth - 27 Feb 2004 13:22 GMT
Thanks for the help...turns out that because I was in a user Defined
listNode class, I couldn't use the iterator anyway. But thanks for
your help just the same. I'm sure it'll come in handy in the future.

Elizabeth

> > In article <9c9c6ba9.0402261646.6210ccf7@posting.google.com>,
>
[quoted text clipped - 13 lines]
>
> Adam
Chris Smith - 27 Feb 2004 14:41 GMT
> Thanks for the help...turns out that because I was in a user Defined
> listNode class, I couldn't use the iterator anyway.

Yes and no.

You can use Iterator... but you have to write your own implementation.  
It wouldn't be such a bad idea to do this... or even to have your class
implement the List interface (though reverse iteration would be a pain).

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Dale King - 27 Feb 2004 19:47 GMT
> > I'm a java student, and I've been given an assignment to implement a
> > singly linked list with references to the first and last node in the
[quoted text clipped - 7 lines]
> startNode.getNext().getNext().getNext()...etc until it returns null.
> Then the last non-null object you pulled is the last object in the list.

But since the problem statement said she had a reference to the last element
then you just call getNext until it returns the last element. At that point
you have a reference to the last element and the one before it and the rest
is easy.
--
 Dale King
tony - 02 Mar 2004 16:18 GMT
> I'm a java student, and I've been given an assignment to implement a
> singly linked list with references to the first and last node in the
[quoted text clipped - 8 lines]
> Elizabeth Bendler
> Madison, NJ
I recall spending hours in a computer lab agonizing over the mystery of
what a List Iterator might be.  The instructor was (by everyone's
observation) not explaining it and claiming that we already knew.

Turns out he was right, but wrong to say so.
A brilliant lab assistant from a previous class happened by and
lightened a number of sad faces by explaining, :

" A list iterator is just like a StringTokenizer.  There is a java
defined class with methods which are named different (from the
StringTokenizer) but serve the same purpose."

In less than two minutes everybody noticed the similarity and the
differences and had that portion of the assignment done.  He did not do
our homework, of course, by explaining the nature of one part of the
technology, but everyone knew where to go.   Thanks, Kevin!

So, while a StringTokenizer takes the next portion of the string until
it finds a parsing seperator and calls the part up to that the next token,

... a list iterator has a reference (in the object in the list - and you
have to add the variable in your object [ which I am assuming you know,
since you've moved on to the removeNode aspect ]to know what will be the
next object and make sure that your method in the list class adds them
properly) to the next object in your list.
So, traversing the list is much the same as tokenizing a string
conceptually.

Any help???
S


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.