简体   繁体   English

排序的基于链接的列表Java

[英]Sorted Linked Based List Java

I'm working on an assignment for my Data Structures class. 我正在为我的数据结构课程分配作业。 We have to create an address book using our own sorted linked based list adt. 我们必须使用我们自己的基于排序的链接列表adt创建通讯录。 Right now the add method works, but it seems to make all the nodes point to the first node. 现在add方法有效,但它似乎使所有节点都指向第一个节点。 Whenever I try to output the the list using getEntry() in a for loop, it gives me the last added entry each time. 每当我尝试在for循环中使用getEntry()输出列表时,每次都会给我最后添加的条目。 I've tried using toArray but it does the same thing. 我试过使用toArray,但它做同样的事情。 Can you see any problems? 你能看到任何问题吗?

public class GTSortedLinkedBasedList implements GTListADTInterface {
private Node firstNode;
private int  numberOfEntries;

public GTSortedLinkedBasedList(){
    //firstNode = new Node(null);
    numberOfEntries = 0;
}

public void setNumberOfEntries(int x){
    numberOfEntries = x;
}

public void add(ExtPersonType newEntry){
   //firstNode = null;
   Node newNode = new Node(newEntry);
   Node nodeBefore = getNodeBefore(newEntry);
   if (isEmpty() || (nodeBefore == null))
   {
      // Add at beginning
      newNode.setNextNode(firstNode);
      firstNode = newNode;
   }
   else
   {
      // Add after nodeBefore
      Node nodeAfter = nodeBefore.getNextNode();
      newNode.setNextNode(nodeAfter);
      nodeBefore.setNextNode(newNode);
   } // end if
   numberOfEntries++;
}

private Node getNodeBefore(ExtPersonType anEntry){
    Node currentNode = getFirstNode();
    Node nodeBefore = null;
    while ((currentNode != null) &&
    (anEntry.getFirstName().compareTo(currentNode.getData().getFirstName()) > 0))
    {
    nodeBefore = currentNode;
    currentNode = currentNode.getNextNode();
    } // end while
    return nodeBefore;
}

private class Node {

    private ExtPersonType data;
    private Node next;

    public Node(ExtPersonType dataValue) {
        next = null;
        data = dataValue;
    }

    public Node(ExtPersonType dataValue, Node nextValue) {
        next = nextValue;
        data = dataValue;
    }

    public ExtPersonType getData(){
        return data;
    }
    public void setData(ExtPersonType newData){
        data = newData;
    }
    public Node getNextNode(){
        return next;
    }
    public void setNextNode(Node newNode){
        next = newNode;
    }

}


public ExtPersonType getEntry(int givenPosition) {
    if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)){
      assert !isEmpty();
      return getNodeAt(givenPosition).getData();
   }
   else{
      throw new IndexOutOfBoundsException("Illegal position given to getEntry operation.");
   }
}

public void loadData(GTSortedLinkedBasedList contacts) throws FileNotFoundException{
    //int index = 0;
    ExtPersonType person = new ExtPersonType();
    DateType tempDate = new DateType();
    AddressType tempAddress = new AddressType();
    Scanner file = new Scanner(new FileInputStream("Programming Assignment 1 Data.txt"));
    while(file.hasNext()){
        person.setFirstName(file.next());
        person.setLastName(file.next());
        tempDate.setMonth(file.nextInt());
        tempDate.setDay(file.nextInt());
        tempDate.setYear(file.nextInt());
        person.setDOB(tempDate);
        tempAddress.setStreetAddress(file.nextLine());
        if(tempAddress.getStreetAddress().isEmpty()){
            tempAddress.setStreetAddress(file.nextLine());
        }
        tempAddress.setCity(file.nextLine());
        tempAddress.setState(file.nextLine());
        tempAddress.setZipCode(file.nextLine());
        person.setAddress(tempAddress);
        person.setPhoneNumber(file.nextLine());
        person.setPersonStatus(file.nextLine());
        if(person.getPersonStatus().isEmpty()){
            person.setPersonStatus(file.nextLine());
        }
        contacts.add(person);
        System.out.println(contacts.getEntry(contacts.getLength()).getFirstName());
        //index++;

    }
}

public static void main(String[] args) throws FileNotFoundException {
    AddressBook ab = new AddressBook();

    ab.loadData(ab);
   ExtPersonType people = new ExtPersonType();
    //people = ab.toArray(people);
    System.out.println(ab.getLength());
    for(int cnt = 1; cnt <= ab.getLength(); cnt++){
        people = ab.getEntry(cnt);
        System.out.println(people.getFirstName());
    }

}

EDIT: The add method is overwriting each previous object with the newly added one. 编辑:add方法用新添加的对象覆盖每个先前的对象。 It also doesn't seem to matter if I do a sorted list or just a basic list. 我是排序列表还是基本列表也似乎无关紧要。

I'm not going to lie here, I'm not totally sure I understand your code but I think I see what's wrong. 我不会躺在这里,我不完全确定我理解你的代码,但我想我看到了什么是错的。 In your getNodeBefore() method's code, you set currentNode() always to firstNode(). 在getNodeBefore()方法的代码中,将currentNode()始终设置为firstNode()。 I believe that is causing the problem. 我相信是造成问题的原因。 I see that you are trying to recursively move through the list to find the proper node but I don't think each recursive call is causing movement through the list. 我看到你试图递归遍历列表以找到正确的节点,但我不认为每个递归调用都会导致列表中的移动。 I suggest you add properties to the object that represent the forward and backward nodes. 我建议您向代表向前和向后节点的对象添加属性。

Something like this... 像这样......

private T data;
private Node nodeBefore;
private Node nodeAfter;

As you create objects, you assign the properties before and after and then all the information you need is contained in the object itself. 在创建对象时,您可以在之前和之后分配属性,然后您需要的所有信息都包含在对象本身中。

To move recursively through the list you would then just add a statement like currentNode = currentNode.nodeAfter. 要在列表中递归移动,您只需添加一条语句,例如currentNode = currentNode.nodeAfter。

Your getNodeBefore() method would simply return currentNode.nodeBefore and getNodeAfter() would return currentNode.nodeAfter. 你的getNodeBefore()方法只返回currentNode.nodeBefore,getNodeAfter()将返回currentNode.nodeAfter。

You don't have code that handles the situation where the node being added will be the first node in the list, but the list is also not empty. 您没有处理以下情况的代码:要添加的节点将成为列表中的第一个节点,但是列表也不为空。 In this case, getNodeBefore returns null, and your code overwrites the root node. 在这种情况下,getNodeBefore返回null,并且您的代码将覆盖根节点。

Try 尝试

   if (isEmpty() && (nodeBefore == null))
   {
     // Add at beginning
     newNode.setNextNode(firstNode);
     firstNode = newNode;
   }
   else if(nodeBefore == null)
   {
     Node temp = new Node();
     temp.setNextNode(first.next);
     temp.setData(first.data);
     newNode.setNextNode(temp);
     firstNode = newNode;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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