By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

Unable to insert into LinkedList

P: 35
I don't know why the customer records cannot be inserted into the linked list and the head of the linked list keep pointing to null...
Expand|Select|Wrap|Line Numbers
  1. //ListNode.java
  2. public class ListNode{
  3.     private Object item;
  4.     private ListNode next,head;
  5.     public ListNode(Object newItem)
  6.     {    item = newItem;    next = null;     }
  7.     public ListNode(Object newItem, ListNode newNext)
  8.     {    item = newItem;    next = newNext;    }
  9.     public void setItem(Object newItem)
  10.     {    item = newItem;    }
  11.     public void setNext(ListNode newNext)
  12.     {    next = newNext;    }
  13.     public Object getItem()
  14.     {     return item;    }
  15.     public ListNode getNext()
  16.     {    return next;    }}
  17.  
  18. //LinkedList.java
  19. public class LinkedList{
  20.     protected ListNode head; //pointer to the first object
  21.     protected int numOfItems; //total number of objects
  22.     public LinkedList()
  23.     {    head = null;     numOfItems = 0; }
  24.     public boolean isEmptyList(){
  25.         if(numOfItems==0)  return true;
  26.         else    return false;    }
  27.     protected ListNode find(int index) {   
  28.          ListNode   cur = head;
  29.          if (index > numOfItems) return null;
  30.          for (int count=1; count<index; count++)     cur = cur.getNext();
  31.          return cur;}
  32.     public boolean insert(int at, Object newItem)
  33.     {    ListNode   pre, cur;
  34.          if (at < 1 || at > numOfItems+1) return false;
  35.             numOfItems++;
  36.          if (at == 1){
  37.             if (isEmptyList()) head = new ListNode(newItem);
  38.             else head = new ListNode(newItem, head);
  39.             return true;}
  40.         pre = find(at-1);
  41.     cur = pre.getNext();
  42.     pre.setNext(new ListNode(newItem, cur));
  43.     return true;}
  44.     public int size()
  45.     {    return numOfItems;    }
  46.  
  47. //Customer.java
  48. public class Customer{
  49.     protected int acc_id;
  50.     protected String name,address,birthdate,phone_no;
  51.     protected double balance, interest;
  52.     protected String type;
  53.     public Customer(){....} //default constructor
  54.     public Customer(int id,int name....){....}
  55.     //all the set and get methods 
  56.  
  57. //Main Program
  58. Customer[] records = new Customer[30];
  59. LinkedList custRecords = new LinkedList();
  60. for(int i=0;i<records.length;i++){ records[i] = new Customer(); }
  61. //ask user for filename
  62. //call readfile(filename,records,custRecords)
  63. public static void readfile(String filename,Customer[] record,LinkedList cust_list){
  64. int i=0;
  65. try{
  66.          File file = new File(filename);
  67.             Scanner readfile = new Scanner(file);
  68.             while(readfile.hasNext()){
  69.                   //read all inputs from file
  70.                   //e,g, record[i].setID(readfile.nextInt());
  71.                     cust_list.insert(i+1,record[i]); //Something wrong here?
  72.                   i++;
  73.              }
  74. }catch(FileNotFoundException){
  75.     System.out.println("File not found");
  76. }}
Sep 23 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 10K+
P: 11,448
I don't know why the customer records cannot be inserted into the linked list and the head of the linked list keep pointing to null...
System.out.println() is a very good debugging tool. Sprinkle them all over your
code where you expect something fishy's going on and run your program again.

kind regards,

Jos
Sep 23 '07 #2

P: 35
Thanx for ur help~ I solved the problem already~
Sep 23 '07 #3

Expert 10K+
P: 11,448
Thanx for ur help~ I solved the problem already~
Great; care to elaborate on what the mistake was? (just for the posterity).

kind regards,

Jos
Sep 23 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.