顺序查找发:采用链表指针来查找
template<typename Key, typename Value>
class SequenceST {
private:
struct Node {
Key key;
Value value;
Node* next;
Node(Key key, Value value) {
this->key = key;
this->value = value;
this->next = NULL;
}
};
Node* head;
int count;
public:
SequenceST() {
head = NULL;
count = 0;
}
~SequenceST()
{
while (head != NULL) {
Node *node = head;
head = head->next;
delete node;
count--;
}
assert(head == NULL && count == 0);
}
int size() {
return count;
}
bool isEmpty() {
return count == 0;
}
void insert(Key key, Value value) {
Node *node = head;
while (node != NULL) {
if (key == node->key) {
node->value = value;
return;
}
node = node->next;
}
Node *newNode = new Node(key, value);
newNode->next = head;
head = newNode