反转符号表

问题描述 投票:0回答:1

所以,我一直在使用符号表。我已经成功实现了get,put,delete和其他几种与ST一起使用的方法。我当前的问题是尝试创建填充符号表的逆。我正在对符号表本身使用链接列表实现。更多信息:我已经使用以下命令成功地将键设置为Iterable:

   public Iterable<Key>  keys() {
        Queue<Key> createdKeys = new Queue<Key>();
        for ( Node temp = first; temp != null; temp=temp.next) {
            createdKeys.enqueue(temp.key);
        }
        return createdKeys;
    }

我已经从这种格式开始使用逆方法。

  public LinkedListST<Value, Key> inverse () {
        LinkedListST<Value, Key> inversed=new LinkedListST<>();

        for(LinkedListST<> : inversed){ //this code was a shot in the dark.

}
            return inversed; 
    }

从这里,我有些迷茫。我应该使用for循环进行迭代吗?我不确定如何填充列表然后将其反转。

LinkedListST代码:

public class LinkedListST<Key extends Comparable<Key>, Value extends Comparable<Value>> {
    private Node first;      // the linked list of key-value pairs

    // a helper linked list data type
    private class Node {
        private Key key;
        private Value val;
        private Node next;

        public Node(Key key, Value val, Node next)  {
            this.key  = key;
            this.val  = val;
            this.next = next;
        }
    }    

所以,我一直在使用符号表。我已经成功实现了get,put,delete和其他几种与ST一起使用的方法。我当前的问题是尝试创建... ...>

java dictionary symbols symbol-table
1个回答
0
投票
public LinkedListST<Value, Key> inverse() {
    LinkedListST<Value, Key> inversed = new LinkedListST<>();

    for (Key key : this) {
        inversed.put(this.get(key), key);
    }

    return inversed;
}

如果不起作用,请分享您的LinkedListST代码。

© www.soinside.com 2019 - 2024. All rights reserved.