Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

adding doubly linklists to an array

$
0
0
I have an array, and it's of type DataRecord. I have 3 double ended doubly linked lists (fNamel, lNamel, Idl) that each contain nodes that are of type indexrecord. These 3 DLL's make up the array. I need to be able to read in a file as well as take in user input after the file read in has partially populated the array. I have an insertFirst method(as well as a few other insert methods ive tried) that is implmented in a method called add(). add() takes in the three DLL's and fills the fName, lName, and Id parts of the DataRecord. I then need to be able to sort the DLL's individually for later calls. I put a print statement inside my add method to check it's behavior and it increments and adds the DLL's and nElems count properly. However, when i try and call data[] elsewhere it always prints the last element of the array no matter what index element of data i call? The following are partial class fragments to give the idea:
IndexRecord (nodes are made of these):
public class IndexRecord
{
    public String key;
    public int index;
    public IndexRecord(String s, int i)
    {
        key = s;
        index = i;
    }


Node class(constructor):
private class Node
    {
      public IndexRecord dData;               
      public Node next;           
      public Node previous;
      public Node(String key, int index)             
      { 
         dData = new IndexRecord(key ,index); 
         key = null;
         index = 0;
         next = null;
         previous = null;
      }


LinkedList insertFirst:
public void insertFirst(String key, int index)
   {
      Node newLink = new Node(key, index); 

      if( isEmpty() )             
         tail = newLink;           
      else
         head.previous = newLink;  
      newLink.next = head;         
      head = newLink;               
   }


DataRecord constructor:
public  class DataRecord
{
    private static String fNameS;
    private static String lNameS;
    private static String IdS;
    
   public DataRecord(String f,String l,String i)
   {
        this.IdS = i;
        this.fNameS = f;
        this.lNameS = l;
   }


DataRecord initialization:
    private static DataRecord[]data = new DataRecord[100]; 
    private static int nElems = 0;
    public static DLL fNamel = new DLL();
    public static DLL lNamel = new DLL();
    public static DLL Idl    = new DLL();


add() calls to insertFirst:
public static void add(String f, String l, String id)
{
    	   
    	DataRecord s = new DataRecord(f, l, id);
        fNamel.insertFirst(f,nElems);
        lNamel.insertFirst(l,nElems);
        Idl.insertFirst(id,nElems);
       
    	//System.out.println(nElems);//This is purely just to check behavior of method.
    	
    	data[nElems++] = s;
    	//System.out.println(f+" "+l+" "+id);//This is purely just to check behavior of method.
}


places where add() is called:
 while(inputStream.hasNextLine())
 {
                
            	line = inputStream.nextLine();
                StringTokenizer st1 = new StringTokenizer(line);
                	
                        fNameA = st1.nextToken();
                    	lNameA = st1.nextToken();
                        IdA = st1.nextToken();
                    	myStructure.add(fNameA, lNameA, IdA);
 }
            


and here:
System.out.println("Enter first name");
       name1=keyboard.nextLine();
       System.out.println("Enter last name");
       name2=keyboard.nextLine();
       myStructure.add(name1,name2,tempID);


I can tell that something is wrong with my incrementing of the data array, i've also just tried nElems++ after instead of data[nElems++] = s but that doesn't change the output. Here is an example of the problem i get:

System.out.println(data[23].getfName());


Output: Bjorn
System.out.println(data[12].getfName()+" "+data[46].getId());


Output: Bjorn 12349
System.out.println(data[nElems-30].getfName()+" "+data[nELems-30].getId());//Just an idea.


Output: Bjorn 12349
Now this data Bjorn Riegger 12349 is the last (data[67]) element in the array and it prints no matter what element i call? Any assistance would be a big help.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>