简体   繁体   English

使用类似于使用Java的Lisp的对象制作通用链接列表

[英]Making Generic Linked Lists Using Objects Similar to Lisp Using Java

How do you create a reference based linked list from an expression, and implements a number of methods to perform operations on the list. 如何从表达式创建基于引用的链接列表,并实现许多方法来对列表执行操作。 As provided by my professor, he use the tokenizer to break the expression. 正如我的教授所提供的那样,他使用标记符来破坏表达式。 Example: 例:

parse("(+ 3 5 ( 23.2 -1a 1/2 \"abc\" ) ) "); 

And my assignment is to add it to a simple linked list, but my problem is that for every open parentheses there's a new list and a parentheses inside this is a sublist. 我的任务是将其添加到一个简单的链接列表中,但是我的问题是,对于每个打开的括号,都有一个新列表,并且该括号内的是一个子列表。

For the case: (4 * (45 + 3) - (7/5)) 对于这种情况:(4 *(45 + 3)-(7/5))

(45 + 3) is a sublist or list. (45 + 3)是子列表或列表。 (7/5) is a sublist or list 4 is integer * symbol 45 integer + symbol 3 integer - symbol 7/5 ratio (7/5)是子列表或列表4是整数*符号45整数+符号3整数-符号7/5的比率

So for I have successfully created a tokenizer to grab values and place them in a linked list. 因此,对于我来说,我已经成功创建了一个分词器来获取值并将其放置在链接列表中。 The problem comes when I have to make sublists. 问题出在我必须创建子列表时。

I have tried to create an LispList object with its own "head" variable to hold the values in that sublist. 我试图用自己的“ head”变量创建一个LispList对象,以将值保存在该子列表中。 And temporarily moving the tokenizers main list pointer to the sublists "head" until the token reaches a ")" token but I still get an empty sublist list. 并暂时将令牌生成器的主列表指针移到子列表“ head”,直到令牌到达“)”令牌,但是我仍然得到一个空的子列表列表。

Any ideas on how to create the sublists Would recursion be a better option? 关于如何创建子列表的任何想法递归会是一个更好的选择吗? Thanks! 谢谢!

This is the LispConversion class that breaks down the list. 这是细分列表的LispConversion类。

public class LispConversion{

public LispObject head;
public LispObject temphead;

public void parse(String values){  
    StreamTokenizer t = new StreamTokenizer(new BufferedReader(new StringReader(values)));
    t.resetSyntax();   
    t.wordChars(0,255); 
    t.whitespaceChars(0,' '); 
    t.commentChar(';'); 
    t.quoteChar('"');
    t.ordinaryChar('('); 
    t.ordinaryChar(')');

    try {
        t.nextToken();
    }
    catch (IOException e){
        System.out.println(e);
    }

    int counter = 0;
    while (t.ttype != StreamTokenizer.TT_EOF){

        switch (t.ttype) {

        case StreamTokenizer.TT_WORD: 
            try{ 
                int value = Integer.parseInt(t.sval);
                head = new LispInteger(head, value);
                } 

            catch (NumberFormatException e1){
                try{
                    double value = Double.parseDouble(t.sval);
                    head = new LispFloat(head, value);
                    } 
                catch (NumberFormatException e2){
                    String[] array = t.sval.split("/");
                    if (array.length != 2){ 
                        String value = t.sval;
                        head = new LispSymbol(head, value);
                    } 
                    else {
                        int numerator = Integer.parseInt(array[0]);
                        int denominator = Integer.parseInt(array[1]);
                        head = new LispRatio(head, numerator, denominator);
                    }
                }
            }
            break;

        case '"': 
            String value = t.sval;
            head = new LispString(head, value);

        break;

        default: 
        counter++; //So the first '(' is not read I use a counter.
        if((char)t.ttype == '(' && counter > 1){

            LispList newList = new LispList(head);
            head = newList;

            temphead = head;
            head = newList.listHead;    
            }
        if((char)t.ttype == ')'){
            head = temphead;
            System.out.println();
        }


        }

        try {
            t.nextToken();
        }
        catch (IOException e){
            System.out.println(e);
        }
    }
}

This is my LispList Object. 这是我的LispList对象。

public class LispList extends LispObject {

public LispObject listHead;

public LispList(LispObject next){
    super(next);
}

public void printList(){
    System.out.print("List: ");
    if(listHead == null){
        System.out.println("The list is empty.");
        return;
    }
    LispObject temp = listHead;
    while(temp != null){
        System.out.print(temp + " ");
        temp = temp.next;
    }
    System.out.println();
}

Generics in Java exist. Java中存在泛型。 They are represented by <> symbols. 它们由<>符号表示。 To use a Linked List, import java.util.LinkedList class in your program and use it like: 要使用链接列表,请在程序中导入java.util.LinkedList类,并按如下方式使用它:

LinkedList<Integer> integerLinkedList = new LinkedList<Integer>();
integerLinedList.add(56);

LinkedList<Character> characterLinkedList = new LinkedList<Character>();
characterLinkedList.add('(');

Check out the LinkedList documentation. 查阅LinkedList文档。

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

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