简体   繁体   中英

Not all data being read into one double ended doubly linked list, but is all being read into two others

I have three double ended doubly linked lists. The keep, in order, first names, last names, and ID numbers for navigating a data structure. The first name and ID number lists are filling up fine with all needed data, but not everything is going into the last name list. I've tried a couple things already. I moved around some increments of variables thinking that was the problem, and also thought it was my checkFor method at one point, but I don't believe it was the case. Below is the data and the code I believe is relevant to the problem. My current results have the first name list and Id list containing all the needed data(duplicate ID numbers are deleted) while the last name list is about one half to a third of the size.

//The data, this in a file read Lname , Fname , ID
Dunn Sean 31111
Duong Geoffrey 29922
Fazekas Nicholas 31100
Prezioso Stefano 22223
Puvvada Mohana 11224
Ravikumar Rakhi 11226
Salyers Matthew 11227   
Gillespie William 49587
Hess Caleb 29282  
Armatis Jared 34512     
Beckman Allan 35176
Wang Zhen 22113
Wingett Jordan 12345 
Belt Keith 34987
Bixler Tyler 22234
Chambers Quentin 22567
Chinni Adithya 28456
Donheiser Michael 28456
Kondrashov Mikhail 33331
Kraus Laura 33332
Krupp Phillip 49888
Maass John 44112
McCarty Amanda 44223
Moldovan Gregory 44335
Oshiyoye Adekunle 44556 
Pagalos Frank 33112
Perski Zackery 33221 
Saunders Jordan 77556   
Simpson Ashlynne 77665  
Szalai Kyle 33112 
Witting Robert 21354 

 Database d=new Database();  
    //Data Reading:
    Scanner inputStream;
    try {
        inputStream = new Scanner(new FileInputStream("Data"));
        while(inputStream.hasNextLine())
        {
            String line = inputStream.nextLine();
            String[] temp = line.split(" ");
            //Constructor for DataBaseRec is Fname , Lname , ID
            DataBaseRec begin = new DataBaseRec(temp[1] , temp[0] , temp[2]);
            d.intialAdd(begin);
        }
    } catch (FileNotFoundException e) {
        e.getMessage()
    }



// The initial add method used with data reading to insert where needed
public void intialAdd(DataBaseRec student)
{
    if(nextDBRec == 0)
    {
        data[nextDBRec] = student;
        Node Ftemp = new Node(student.getFName() , nextDBRec);
        Node Ltemp = new Node(student.getLName() , nextDBRec);
        Node Itemp = new Node(student.getID() , nextDBRec);
        FnameRecs.insert(Ftemp);
        LnameRecs.insert(Ltemp);
        IDRecs.insert(Itemp);
    }
    else 
    {
        if(IDRecs.checkFor(student.getID()) == 1)
            return;
        else
        {
            data[nextDBRec] = student;
            Node Ftemp = new Node(student.getFName() , nextDBRec);
            Node Ltemp = new Node(student.getLName() , nextDBRec);
            Node Itemp = new Node(student.getID() , nextDBRec);
            LnameRecs.insert(Ltemp);
            FnameRecs.insert(Ftemp);
            IDRecs.insert(Itemp);

        }
    }
    nextDBRec++;
}


// The check for method
public int checkFor(String key)
{
    Node rover;
    rover = front;
    int retVal = 0;

    while(rover != null)
    {
        if(rover.getKey().equals(key))
        {
            retVal = 1;
            break;
        }
        rover = rover.getNext();
    }
    return retVal;
}

// My insert method this inserts the nodes into the lists
public void insert(Node newNode)
{
    Node rover;
    rover = front;
    if(front == null)
    {
        front = newNode;
        if(back == null)
            back = newNode;
    }
    else if(front == back)
    {
        if(rover.getKey().compareTo(newNode.getKey()) >= 0)
        {
            newNode.setNext(rover);
            rover.setPrev(newNode);
            front = newNode;
        }
        return;
    }
    else
    {
        while(rover != null)
        {
            if(rover.getKey().compareTo(newNode.getKey()) >= 0) break;
                rover = rover.getNext();
        }
        if(rover == null)
        {
            newNode.setPrev(back);
            back.setNext(newNode);
            back = newNode;
        }
        else if(rover.getPrev() != null)
        {
            newNode.setPrev(rover.getPrev());
            newNode.getPrev().setNext(newNode);
            newNode.setNext(rover);
            rover.setPrev(newNode);
        }
        else
        {
            newNode.setNext(rover);
            rover.setPrev(newNode);
            front = newNode;
        }
    }
}

I believe that's all the relevant code, if anyone has idea what I'm doing wrong I would appreciate the help. If I need to provide more code I can do so. Also all names being compared are done so in lowercase form. The node strings are lower case.

I've solved it! In the insert method my code was wrong if front == back returned true. If the new data needed to go before and become the new front it was just returning. Where the return statement was I needed the code that was inside the the if statement using the compare to. And inside that if statement I needed code for inserting into the back of the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM