找不到项目错误

时间:2014-07-20 21:51:12

标签: java algorithm dictionary

我的代码有两个我无法找到或理解的错误,希望有人可以就如何解决这个问题给我一些意见。它为我的删除功能的第118和125行说明符号'项目'无法找到。我想我在代码中明确说明了什么项目。我将在下面发布我的代码,希望有人可以提供帮助。

  8  public class Dictionary implements DictionaryInterface {
  9 
 10    // private inner Node class
 11    private class Node {
 12       String  key;
 13       String value;
 14       Node left;
 15       Node right;
 16       Node next;
 17 
 18       Node(String k, String v) {
 19          key = k;
 20          value = v;
 21          left = null;
 22          right = null;
 23          next = null;
 24       }
 25    }
 26 
 27    //Fields for the IntegerList class
 28    private Node root;    // reference to the head Node in list 
 29    private int numItems; // number of items in this IntegerList  
 30 
 31    //Dictionary()
 32    //constructor for the Dictionary class
 33    public Dictionary() {
 34       root = null;
 35       numItems = 0;
 36    }
 37 
38    // find key
 39    // returns a reference to the Node containing key k in the subtree rooted at 
 40    // R or Null if no such Node exists
 41    private Node findKey (String key){
 42       Node N = root;
 43         while (N != null){
 44            if(key.equals(N.key))
 45                break;
 46             N = N.next;
 47         }
 48    }
 49 
 50    // ADT operations ---------------------------------------------
 51 
 52    // isEmpty()
 53    // pre: none
 54    // post: returns true if this Dictionary is empty, false otherwise 
 55    public boolean isEmpty() {
 56       return (numItems == 0);
 57    }
 58 
 59    // size()
 60    // pre: none
 61    // post: returns the number of elements in this Dictionary 
 62    public int size() {
 63       return numItems;
 64    }
65 
 66    // lookup(String key)
 67    // get()
 68    // pre: 1<= key <= size()
 69    // post: returns item at position key in this Dictionary 
 70    public String lookup(String key){
 71         Node N = root;
 72            while( N != null){
 73               if(key.equals(N.key))
 74                 break;
 75                N = N.next;
 76            }
 77            if( N != null){
 78              return N.value;
 79            }else{
 80               return null;
 81                 }
 82    }
 83 
 84 
85    // insert(String key, String value)
 86    // inserts new (key, value) pair into this Dictionary
 87    // pre: key k does not exist in this Dictionary, i.e lookup(k) == UNDEFINED 
 88    // post: !isEmpty(), size() is increased by one 
 89    public void insert(String k, String v) throws KeyCollisionException{
 90        // Node N = root;
 91          if (lookup(k) != null){
 92            throw new KeyCollisionException("cannot insert duplicate keys");
 93           }
 94       if (numItems== 0) {
 95          root = new Node(k,v);
 96       }else{
 97          Node N = new Node(k,v);
 98           N=N.next;
 99       }
100          Node P = null;  //N
101          Node C = P.next;
102          P.next = new Node (k, v);
103          P = P.next;
104          P.next = C;
105 
106      numItems++;
107 
108    }
109 
110    // delete(String key)
111    // pre: key k currently exists in this Dictionary, i.e. lookup(k)!= UNDEFINED
112    // post: size() is decreased by one 
113    public void delete(String k) throws KeyNotFoundException{
114       Node N = root;
115       if (lookup (k) == null){
116           throw new KeyNotFoundException("cannot delete non-existent key");
117       }
118       if (k.compareTo(N.item)==0) {
119           Node P = root;
120           root = root.next;
121           P.next = root;
122           numItems--;
123        }else{
124           while(N !=null && N.next !=null){
125             if (k.compareTo(N.next.item)==0){
126                 Node P =  N;
127                 Node C = P.next;
128                 P.next = C.next;
129                 N=P;
130             }
131            N=N.next;
132        }
133       numItems--;
134      }
135    }
136 
137    // makeEmpty()
138    // pre: none
139    // post: isEmpty()
140    public void makeEmpty(){
141       root = null; // root = 0;
142       numItems = 0;
143    }

1 个答案:

答案 0 :(得分:0)

您好像希望将kN.key进行比较,但是您要将其与N.item进行比较,而item不存在。在两行上将key更改为{{1}}。