Help on Linked Lists

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

    #1

    Help on Linked Lists

    I have a file containing all the customer records. I had a program to read from the file and store using LinkedLists. Each Node is each customer record. Each customer record have AccID, Add, PhoneNum, etc.. All the accID, add are stored in an array. My question is how can I get all the names, ids all printed out?

    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[] record = new Customer[30];
    LinkedList custRecords = new LinkedList();
    ListNode numRecord = new ListNode(record);
    while(readfile.hasNext()){ //readfile is using the Scanner class
    //read all the data from file
    //e.g. record[i].setID(readfile.nextInt());
     custRecords.insert(i+1,record[i]);}
    //put the printing here, but how?
    I use NetBeans debugger mode and found that it is in this structure:
    numRecord
    item
    [0]
    acc_id //the correct id e.g. 123
    name //Matt Damon
    item
    [1]
    acc_id //e.g. 456
    name // Ben Affleck
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Override the toString method of your Customer class then just loop through all the customers and printing their toString values.

    Comment

    • huiling25
      New Member
      • Dec 2006
      • 35

      #3
      Hmm, so, i don't need to use the LinkedList() or ListNode() to help me print out the data?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by huiling25
        Hmm, so, i don't need to use the LinkedList() or ListNode() to help me print out the data?
        No, LinkedLists are not used for printing, they are just a collection for storing many objects.

        Comment

        • huiling25
          New Member
          • Dec 2006
          • 35

          #5
          Originally posted by r035198x
          No, LinkedLists are not used for printing, they are just a collection for storing many objects.
          I see, thanx for all your help~

          Comment

          Working...