2014-07-20 5 views
-3

내 코드에서 찾을 수 없거나 이해할 수없는 두 가지 오류가 있습니다.이 오류를 해결하는 방법에 대한 의견을 누군가에게 줄 수 있기를 바랍니다. 내 '삭제'기능을 위해 118 번과 125 번 줄에 'item'기호를 찾을 수 없다고 표시되어 있습니다. 나는 항목이 무엇인지 내 코드에서 분명히 알았습니다. 아래에 코드를 게시하고 누군가가 도와 줄 수 있기를 바랍니다.항목 오류를 찾을 수 없습니다.

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 } 
+2

'노드'에는 '항목'속성이 없습니다. –

+0

Node 클래스의 item 속성은 어디에 있습니까? –

+0

클래스'Node'에는 'item'이라는 멤버가 포함되어 있지 않습니다. – DavidPostill

답변

0

N.keyk을 비교해야 할 것처럼 보이지만 존재하지 않는 N.item,과 비교된다. 두 줄 모두에서 itemkey으로 변경하십시오.

관련 문제