-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLink.java
More file actions
104 lines (87 loc) · 1.93 KB
/
Link.java
File metadata and controls
104 lines (87 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package Search;
import java.util.Scanner;
import edu.princeton.cs.algs4.Queue;
public class Link<Key,Value> {
private Node first;
private int N;
private class Node{
Key key;
Value value;
Node next;
public Node(Key key,Value value,Node next){
this.key=key;
this.value=value;
this.next=next;
}
}
//由键查值
public Value get(Key key){
for(Node x=first;x!=null;x=x.next){
if(key.equals(x.key)){
return x.value;
}
}
return null;
}
//增加节点
public void put(Key key,Value value){
for(Node x=first;x!=null;x=x.next){
if(key.equals(x.key)){
x.value=value;
return;
}
}
first=new Node(key,value,first);
N++;
}
//返回链表大小
public int size(){
return N;
}
//链表是是否为空
public boolean isEmpty(){
return N==0;
}
//是否包含某键
public boolean contains(Key key){
if(get(key)==null){
return false;
}
return true;
}
public void delete(Key key){
if(key==null){
throw new IllegalArgumentException("键值不存在!");
}
first=delete(first,key);
}
private Node delete(Node x,Key key){
if(x==null){
return null;
}
if(key.equals(x.key)){
N--;
return x=x.next;
}
x.next=delete(x.next,key);
return x;
}
public Iterable<Key> keys() {
Queue<Key> queue = new Queue<Key>();
for (Node x = first; x != null; x = x.next)
queue.enqueue(x.key);
return queue;
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Link<String, Integer> st = new Link<String, Integer>();
String line=in.nextLine();
String[] value=line.split(" ");
for(int n=0;n<value.length;n++)
{
st.put(value[n], n);
}
for (String s : st.keys())
System.out.println(s + " " + st.get(s));
}
}