算法之顺序查找法

本文介绍了如何使用链表数据结构进行顺序查找操作,详细阐述了通过链表指针进行查找的过程。
摘要由CSDN通过智能技术生成

顺序查找发:采用链表指针来查找


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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值