Unable to insert into LinkedList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • huiling25
    New Member
    • Dec 2006
    • 35

    #1

    Unable to insert into LinkedList

    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...
    Code:
    //ListNode.java
    public class ListNode{
        private Object item;
        private ListNode next,head;
        public ListNode(Object newItem)
        {    item = newItem;    next = null;     }
        public ListNode(Object newItem, ListNode newNext)
        {    item = newItem;    next = newNext;    }
        public void setItem(Object newItem)
        {    item = newItem;    }
        public void setNext(ListNode newNext)
        {    next = newNext;    }
        public Object getItem()
        {     return item;    }
        public ListNode getNext()
        {    return next;    }}
     
    //LinkedList.java
    public class LinkedList{
        protected ListNode head; //pointer to the first object
        protected int numOfItems; //total number of objects
        public LinkedList()
        {    head = null;     numOfItems = 0; }
        public boolean isEmptyList(){
            if(numOfItems==0)  return true;
            else    return false;    }
        protected ListNode find(int index) {   
             ListNode   cur = head;
             if (index > numOfItems) return null;
             for (int count=1; count<index; count++)     cur = cur.getNext();
             return cur;}
        public boolean insert(int at, Object newItem)
        {    ListNode   pre, cur;
             if (at < 1 || at > numOfItems+1) return false;
                numOfItems++;
             if (at == 1){
                if (isEmptyList()) head = new ListNode(newItem);
                else head = new ListNode(newItem, head);
                return true;}
            pre = find(at-1);
        cur = pre.getNext();
        pre.setNext(new ListNode(newItem, cur));
        return true;}
        public int size()
        {    return numOfItems;    }
     
    //Customer.java
    public class Customer{
        protected int acc_id;
        protected String name,address,birthdate,phone_no;
        protected double balance, interest;
        protected String type;
        public Customer(){....} //default constructor
        public Customer(int id,int name....){....}
        //all the set and get methods 
     
    //Main Program
    Customer[] records = new Customer[30];
    LinkedList custRecords = new LinkedList();
    for(int i=0;i<records.length;i++){ records[i] = new Customer(); }
    //ask user for filename
    //call readfile(filename,records,custRecords)
    public static void readfile(String filename,Customer[] record,LinkedList cust_list){
    int i=0;
    try{
             File file = new File(filename);
                Scanner readfile = new Scanner(file);
                while(readfile.hasNext()){
                      //read all inputs from file
                      //e,g, record[i].setID(readfile.nextInt());
                        cust_list.insert(i+1,record[i]); //Something wrong here?
                      i++;
                 }
    }catch(FileNotFoundException){
        System.out.println("File not found");
    }}
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by huiling25
    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.prin tln() 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

    Comment

    • huiling25
      New Member
      • Dec 2006
      • 35

      #3
      Thanx for ur help~ I solved the problem already~

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by huiling25
        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

        Comment

        Working...