Current location: Hot Scripts Forums » Programming Languages » Everything Java » problem with linked list


problem with linked list

Reply
  #1 (permalink)  
Old 04-29-07, 07:43 PM
Reza_ Reza_ is offline
New Member
 
Join Date: Apr 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy problem with linked list

hi guys!

I have four different classes; one is main class which contain a array of names now i want to make a linked list ( reference-based implementation ) using node class, ListInterface and ListReferenceBased class. I don`t know how to pass my array to these classes.

can anyone help me with this plzzzzz ?

java Code:
  1. public class Node {
  2.   private Object item;
  3.   private Node next;
  4.  
  5.   public Node(Object newItem) {
  6.      item = newItem;
  7.     next = null;
  8.   } // end constructor
  9.  
  10.   public Node(Object newItem, Node nextNode) {
  11.     item = newItem;
  12.     next = nextNode;
  13.   } // end constructor
  14.  
  15.   public void setItem(Object newItem) {
  16.     item = newItem;
  17.   } // end setItem
  18.  
  19.   public Object getItem() {
  20.     return item;
  21.   } // end getItem
  22.  
  23.   public void setNext(Node nextNode) {
  24.     next = nextNode;
  25.   } // end setNext
  26.  
  27.   public Node getNext() {
  28.     return next;
  29.   } // end getNext
  30. } // end class Node
  31.  

java Code:
  1. // ****************************************************
  2. // Reference-based implementation of ADT list.
  3. // ****************************************************
  4. public class ListReferenceBased implements ListInterface {
  5.   // reference to linked list of items
  6.   private Node head;
  7.   private int numItems; // number of items in list
  8.   HandlePersonalData data;
  9.  
  10.   public ListReferenceBased() {
  11.     numItems = 0;
  12.     head = null;
  13.  
  14.   }  // end default constructor
  15.  
  16.   public boolean isEmpty() {
  17.     return numItems == 0;
  18.   }  // end isEmpty
  19.  
  20.   public int size() {
  21.     return numItems;
  22.   }  // end size
  23.  
  24.   private Node find(int index) {
  25.   // --------------------------------------------------
  26.   // Locates a specified node in a linked list.
  27.   // Precondition: index is the number of the desired
  28.   // node. Assumes that 1 <= index <= numItems+1
  29.   // Postcondition: Returns a reference to the desired
  30.   // node.
  31.   // --------------------------------------------------
  32.     Node curr = head;
  33.     for (int skip = 1; skip < index; skip++) {
  34.       curr = curr.getNext();
  35.     } // end for
  36.     return curr;
  37.   } // end find
  38.  
  39.   public Object get(int index)
  40.                 throws ListIndexOutOfBoundsException {
  41.     if (index >= 1 && index <= numItems) {
  42.       // get reference to node, then data in node
  43.       Node curr = find(index);
  44.       Object dataItem = curr.getItem();
  45.       return dataItem;
  46.     }
  47.     else {
  48.       throw new ListIndexOutOfBoundsException(
  49.                 "List index out of bounds on get");
  50.     } // end if
  51.   } // end get
  52.  
  53.   public void add(int index, Object item)
  54.                   throws ListIndexOutOfBoundsException {
  55.     if (index >= 1 && index <= numItems+1) {
  56.       if (index == 1) {
  57.         // insert the new node containing item at
  58.         // beginning of list
  59.         Node newNode = new Node(item, head);
  60.         head = newNode;
  61.       }
  62.       else {
  63.         Node prev = find(index-1);
  64.         // insert the new node containing item after
  65.         // the node that prev references
  66.         Node newNode = new Node(item, prev.getNext());
  67.         prev.setNext(newNode);
  68.       } // end if
  69.       numItems++;
  70.     }
  71.     else {
  72.       throw new ListIndexOutOfBoundsException(
  73.                 "List index out of bounds on add");
  74.     } // end if
  75.   }  // end add
  76.  
  77.   public void remove(int index)
  78.                      throws ListIndexOutOfBoundsException {
  79.     if (index >= 1 && index <= numItems) {
  80.       if (index == 1) {
  81.         // delete the first node from the list
  82.         head = head.getNext();
  83.       }
  84.       else {
  85.         Node prev = find(index-1);
  86.         // delete the node after the node that prev
  87.         // references, save reference to node
  88.         Node curr = prev.getNext();
  89.         prev.setNext(curr.getNext());
  90.       } // end if
  91.       numItems--;
  92.     } // end if
  93.     else {
  94.       throw new ListIndexOutOfBoundsException(
  95.                 "List index out of bounds on remove");
  96.     } // end if
  97.   }   // end remove
  98.  
  99.   public void removeAll() {
  100.     // setting head to null causes list to be
  101.     // unreachable and thus marked for garbage
  102.     // collection
  103.     head = null;
  104.     numItems = 0;
  105.   } // end removeAll
  106.  
  107. } // end ListReferenceBased
  108.  

java Code:
  1. / ********************************************************
  2. // Interface ListInterface for the ADT list.
  3. // *********************************************************
  4. public interface ListInterface {
  5.   public boolean isEmpty();
  6.   public int size();
  7.   public void add(int index, Object item)
  8.                   throws ListIndexOutOfBoundsException,
  9.                          ListException;
  10.   public Object get(int index)
  11.                   throws ListIndexOutOfBoundsException;
  12.   public void remove(int index)
  13.                   throws ListIndexOutOfBoundsException;
  14.   public void removeAll();
  15. }  // end ListInterface
  16.  

Last edited by Christian; 04-30-07 at 12:11 AM. Reason: Please use [highlight=java] tags when posting JAVA code.
Reply With Quote
  #2 (permalink)  
Old 04-30-07, 07:40 AM
King Coder King Coder is offline
Community VIP
 
Join Date: Jan 2006
Posts: 703
Thanks: 0
Thanked 0 Times in 0 Posts
But why would you pass arrays to these classes? Your constructors take Objects! Are you wanting a linked list where each element is an array
__________________
my site
Reply With Quote
  #3 (permalink)  
Old 04-30-07, 08:48 PM
jfulton's Avatar
jfulton jfulton is offline
Community VIP
 
Join Date: Apr 2006
Location: Los Angeles, CA
Posts: 660
Thanks: 0
Thanked 0 Times in 0 Posts
Do you want to convert an Array to a LinkedList?

(Sorry if my Java coding is a little rusty )
Java Code:
  1. //...myArray defined above...
  2. LinkedList myList = new ListReferenceBased();
  3. for (int i=0; i<myArray.length; i++) {
  4.      /**
  5.       * I think your implementation of LinkedList.add() is a little off
  6.       * Generally the first element in a collection has index=0
  7.       */
  8.      myList.add(i+1, myArray[i]);
  9. }
  10. //...continue code...
  11.  
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 to write linked list darksystem Everything Java 2 04-19-07 09:04 AM
problem in excution of HTML Selection list unsing ASP sujata_ghosh ASP 2 05-17-06 11:59 PM
Drop down list problem birchtree Script Requests 0 02-02-06 07:05 AM


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