Linking 2 Linked Lists together

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

    Linking 2 Linked Lists together

    I have a Customer.java and a Transaction.jav a Transaction is when the user wants to deposit money. Customer.java includes the particulars of the user such as Name, AccountID, Balance etc. I have a LinkedList for Customer and a LinkedList for Transaction. How can i link the 2 together?

    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 
    
    //Transaction
    public class Transaction{
        protected int acc_id;
        protected String transDate,transTime;
        protected double amount;
        protected String type;
        protected int place;
    public Transaction(){
        acc_id=0;
        transDate=null;
        transTime=null;
        type=null;
        place=0;}
    public Transaction(int id,String d.....){
        acc_id=id;
        transDate = d;
        amount = amt; 
        type=typ;
        place=p;}
    //all the set and get methods
    
    //Main Program
     Customer[] records = new Customer[30];
    LinkedList custRecords = new LinkedList();
    ListNode numRecord = new ListNode(records);
    //read from file and store into Customer array
    //call insertTramsaction()
    public static void insertTransaction(String filename, LinkedList list_record,Customer[] record){
            int i=0,j=0;
        Transaction[] trans = new Transaction[30];
     LinkedList transRecords = new LinkedList();
    //store into trans array...
     for(i=0;i<trans.length;i++)
         transRecords.insert(i+1,trans[i]);
    for(i=0;i<numRecords;i++){    
         for(j=0;j<numTrans;j++){
             if(trans[j].getID()==record[i].getID()){
                           //how to link to Customer LinkedList?
            }
        }
    }
    }//end program
    I need to link Transaction LinkedList to Customer LinkedList, because each user have their own transaction records. So, i have to linked Transaction linked list to Customer linked list according to the user's account ID.
Working...