-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListMultiset.java
149 lines (125 loc) · 2.87 KB
/
LinkedListMultiset.java
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.io.PrintStream;
import java.util.*;
public class LinkedListMultiset<T> extends Multiset<T> {
protected Node<T> mHead;
public LinkedListMultiset() {
mHead = null;
} // end of LinkedListMultiset()
public void add(T item) {
@SuppressWarnings({ "unchecked", "rawtypes" })
Node<T> newNode = new Node(item);
if (mHead == null){
mHead = newNode;
}
else{
Node<T> currNode = mHead;
Node<T> prevNode = null;
while (currNode != null) {
if (currNode.getValue().equals(item))
{
int temp = currNode.getCount();
temp++;
currNode.setCount(temp);
return;
}
prevNode = currNode;
currNode = currNode.getNext();
}
prevNode.setNext(newNode);
}
} // end of add()
public int search(T item) {
Node<T> currNode = mHead;
int temp = 0;
while (currNode != null) {
if (currNode.getValue().equals(item)) {
temp = currNode.getCount();
return temp;
}
currNode = currNode.getNext();
}
return temp;
} // end of add()
// removes one of the specified item
public void removeOne(T item) {
Node<T> currNode = mHead;
Node<T> prevNode = null;
while (currNode != null) {
if (currNode.getValue().equals(item)) {
int count = currNode.getCount();
if (count == 1) {
if (currNode == mHead) {
mHead = currNode.getNext();
} else {
prevNode.setNext(currNode.getNext());
}
}
count--;
currNode.setCount(count);
return;
}
prevNode = currNode;
currNode = currNode.getNext();
}
} // end of removeOne()
// removes all of the specified item
public void removeAll(T item) {
Node<T> currNode = mHead;
Node<T> prevNode = null;
while (currNode != null) {
if (currNode.getValue().equals(item)) {
if (currNode == mHead) {
mHead = currNode.getNext();
} else {
prevNode.setNext(currNode.getNext());
}
return;
}
prevNode = currNode;
currNode = currNode.getNext();
}
} // end of removeAll()
public void print(PrintStream out) {
Node<T> currNode = mHead;
while (currNode != null) {
out.println(currNode.getValue() + printDelim + currNode.getCount());
currNode = currNode.getNext();
}
}
// end of print()
/**
* Node type, inner private class.
*/
@SuppressWarnings("hiding")
private class Node<T> {
/** Stored value of node. */
protected T mValue;
/** Reference to next node. */
protected Node<T> mNext;
/** Counter for the item. */
int mCount;
public Node(T value) {
mValue = value;
mNext = null;
mCount = 1;
}
public T getValue() {
return mValue;
}
public int getCount() {
return mCount;
}
public Node<T> getNext() {
return mNext;
}
public void setValue(T value) {
mValue = value;
}
public void setNext(Node<T> next) {
mNext = next;
}
public void setCount(int count) {
mCount = count;
}
} // end of inner class Node
} // end of class LinkedListMultiset