Domanda

I'm trying to store a mathematical expression by tokenizing it and putting it into a doubly linked list I created in a lab assignment. The insert method I implemented in the list worked completely fine when I tested before handing in the lab, but when I print the contents of the list after inserting the expression's tokens, some tokens are missing. I know the String Tokenizer is working, as I had it print all the tokens and they all showed up just fine.

Is there something about how String Tokenizer tokenizes strings that prevents me from inserting them as generic Objects in my linkes list? I feel like there's some behavior I'm not aware of here.

For reference, the class containing the method which tokenizes the string and inserts it into a list:

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.*;
import java.util.*;

public class Calculator {
DoubleLinkedListTest infixstorage;

public Calculator(){
    infixstorage = new DoubleLinkedListTest();
}

public static DoubleLinkedListTest ReadInFile(String path){
    File file = new File(path);
    DoubleLinkedListTest list = new DoubleLinkedListTest();

    try {
        Scanner scanner = new Scanner(file);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            StringTokenizer st = new StringTokenizer(line);
            while (st.hasMoreTokens()){
                list.insert(st.nextToken());
            }
        }
        scanner.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return list;
}

}

Here is the code for my DoubleLinkedList:

public class DoubleLinkedListTest implements DoubleLinkedList {
private DoubleNode head;
private DoubleNode tail;

public DoubleLinkedListTest(){
    head = new DoubleNode();
    tail = new DoubleNode();
    head.next = tail;
    tail.prev = head;
}

public void insert(Object x){
    if (lookup(x) == false){
        if (head.data == null){
            head.data = x;
        }
        else if (head.data != null && tail.data == null){
            tail.data = x;
        }
        else{
            DoubleNode NewNode = new DoubleNode();
            NewNode.data = x;
            NewNode.next = null;
            NewNode.prev = tail;
            tail.next = NewNode;
            tail = NewNode;
        }
    }
}
    //Runtime of insert method will be n, where n is the number of nodes

public void delete(Object x){
    if (this.lookup(x).equals(true)){
        if(x.equals(head.data)){
            head.next.prev = null;
            head = head.next;
        }

        else{
            DoubleNode temp = head;
            while(temp.next != null){
                if (temp.next.next == null && (temp.next).data.equals(x)){
                    temp.next.prev = null;
                    temp.next = null;
                    break;
                }
                if ((temp.next).data.equals(x)){
                    temp.next.next.prev = temp;
                    temp.next = (temp.next).next;
                    break;
                }
                else{
                    temp = temp.next;
                }
            }
        }
    }


}

public Object lookup(Object x){
    Boolean search = false;

    if (x.equals(head.data)){
        search = true;
    }

    else{
        DoubleNode temp = head;
        while (temp.next != null){
            if (x.equals(temp.data)){
                search = true;
                break;
            }
            if (temp.next.next == null && x.equals(temp.next.data)){
                search = true;
                break;
            }

            else{
                temp = temp.next;
            }
        }
    }
    return search;  
}

public boolean isEmpty(){
    if(head.next == tail && tail.prev == head)
        return true;
    else
        return false;

}

public void printList(){
    DoubleNode temp = head;
    System.out.println(temp.data + " ");

    while (temp.next != null){
        temp = temp.next;
        System.out.println(temp.data + " ");
    }

}

public void printListRev(){
    System.out.print(tail.data + " ");

    while (tail.prev != head){
        tail = tail.prev;
        printList();
    }


}
}

Nessuna soluzione corretta

Altri suggerimenti

I figured it out. The scanner is reading multiple lines of the file at once. I thought that there was one line, but it just appears that way in the txt file. It's actually three lines sitting next to each other.

EDIT: False alarm, after deleting the other lines, it is still incorrectly reading the remaining line

EDIT 2: Or at least the print of the list is not correct

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top