Current location: Hot Scripts Forums » Programming Languages » Everything Java » how to write linked list


how to write linked list

Reply
  #1 (permalink)  
Old 04-17-07, 12:04 PM
darksystem darksystem is offline
Newbie Coder
 
Join Date: Mar 2005
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
how to write linked list

Hello,

Is there someone who can help me to build linked list in java
without using the Collections API? but to implement the following
two operations.

void add(Object ob);
boolean find(Object ob);

I am a Programmer but my expertise only applies to web development
using LAMP. My brother gives me a pain in the *** to solve this and
I'm also interested to learn this. I'm willing to give any donations
that can help my brother and me. Any help will be much appreciated.

Best regards,
Mark
__________________
Ebay API, OSC/CRE/OscMax/ZenCart/SEO Services
5MServices - Ebay Import Tool - CollectaMania - Email
Reply With Quote
  #2 (permalink)  
Old 04-18-07, 08:09 AM
King Coder King Coder is offline
Community VIP
 
Join Date: Jan 2006
Posts: 703
Thanks: 0
Thanked 0 Times in 0 Posts
In Java there are no pointers (at least explicitly) so you have to use classes. Basically, you will have your own LinkedList class which acts as the outlying data structure and a Node class (obviously to model each node). That should help you get started.
__________________
my site
Reply With Quote
  #3 (permalink)  
Old 04-19-07, 09:04 AM
jdavidw13 jdavidw13 is offline
New Member
 
Join Date: Apr 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
You'll probably need to write two classes to create a linked list, one to act a a node (the class that has all elements you want in the node), and one to act as a manager of the list. The node class should, at least, have a member that holds a reference to the next node in the list. The manager class should, at least, have a member that holds a reference to the first node in the list.

When creating the list, you might do something like this.
java Code:
  1. Node firstNode = new Node();
  2. Node currentNode = firstNode;
  3. //loop to create the desired number of nodes
  4. {
  5.    Node n = new Node();
  6.    currentNode.setNextNode(n);
  7.    currentNode = n;
  8. }
  9. NodeManager nm= new NodeManager();
  10. nm.setFirstNode(firstNode);

To find a node in the list, let the node manager have a method as such...
java Code:
  1. public boolean find(Object toFind) {
  2.    boolean ret = false;
  3.    Node currentNode = firstNode;
  4.    while(true) {
  5.    if (currentNode != null) {
  6.       if (currentNode == toFind) {
  7.          ret = true;
  8.          break;
  9.       }
  10.       else {
  11.          currentNode = currentNode.getNextNode();
  12.    }
  13.    else {
  14.       break;
  15.    }
  16. }
  17. return ret;
  18. }
When creating new nodes, make sure to set the next node to null.

Last edited by Nico; 04-19-07 at 09:16 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how is the program of the INFIX to POSTFIX in stack using linked list? venice C/C++ 1 08-09-04 11:59 PM


All times are GMT -5. The time now is 05:42 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.