template <class K,class E> bool dataList<K,E>:Insert (Kk,E el) /在dataListr的尾部插入新元素,若插入失败,函数返 /回false,否则返回true. if (CurrentSize =ArraySize)return false; dataNode<K,E>node dataNode<K,E>(k,el); Element[CurrentSize]=node;/插入在尾端 CurrentSize++;return true; ; 11
template <class K, class E> bool dataList<K, E>::Insert (K k, E e1) { //在dataList的尾部插入新元素, 若插入失败,函数返 //回false, 否则返回true. if (CurrentSize == ArraySize) return false; dataNode<K, E> node = dataNode<K, E> (k,e1); Element[CurrentSize] = node; //插入在尾端 CurrentSize++; return true; }; 11
template <class K,class E> bool dataList<K,E>::Remove (K x,E&el) /在dataList中删除关键码为X的元素,通过el返回。 /川用尾元素填补被删除元素。 if (CurrentSize ==0)return false; for (int i==0;i<CurrentSize & Element[].key!=x;it+)∥在表中顺序寻找 if (i=CurrentSize)return false; /∥未找到 e1 Element[i].other;/ /找到,保存被删元素的值 Element[j=Element[CurrentSize-l];/填补 CurrentSize--;return true; }; 12
template <class K, class E> bool dataList<K, E>::Remove (K x, E& e1) { //在dataList中删除关键码为x的元素, 通过e1返回。 //用尾元素填补被删除元素。 if (CurrentSize == 0) return false; for (int i == 0; i < CurrentSize && Element[i].key != x; i++) //在表中顺序寻找 if (i == CurrentSize) return false; //未找到 e1 = Element[i].other; //找到,保存被删元素的值 Element[i] = Element[CurrentSize-1]; //填补 CurrentSize--; return true; }; 12
顺序搜索(Sequential Search) 顺序搜索主要用于在线性表中搜索。 设若表中有CurrentSize个元素,则顺序搜索从 表的最前端开始,顺序用各元素的关键码与给定 值x进行比较 若找到与其值相等的元素,则搜索成功,给出该 元素在表中的位置。 若整个表都已检测完仍未找到关键码与x相等的 元素,则搜索失败。给出失败信息。 13
顺序搜索(Sequential Search) • 顺序搜索主要用于在线性表中搜索。 • 设若表中有 CurrentSize 个元素,则顺序搜索从 表的最前端开始,顺序用各元素的关键码与给定 值 x 进行比较 • 若找到与其值相等的元素,则搜索成功,给出该 元素在表中的位置。 • 若整个表都已检测完仍未找到关键码与 x 相等的 元素,则搜索失败。给出失败信息。 13
般的顺序搜索算法在第二章已经讨论过 本章介绍一种使用“监视哨”的顺序搜索方 法。 设在数据表dataList中顺序搜索关键码与给 定值x相等的数据元素,要求数据元素在表 中从下标0开始存放,下标为CurrentSize的 元素作为控制搜索过程自动结束的“监视哨' 使用。 若搜索成功,则函数返回该元素在表中序号 Location(比下标大1),若搜索失败,则函 数返回CurrentSize+l。 14
• 一般的顺序搜索算法在第二章已经讨论过, 本章介绍一种使用“监视哨”的顺序搜索方 法。 • 设在数据表 dataList 中顺序搜索关键码与 给 定值 x 相等的数据元素,要求数据元素在表 中从下标 0 开始存放, 下标为 CurrentSize 的 元素作为控制搜索过程自动结束的“监视哨” 使用。 • 若搜索成功,则函数返回该元素在表中序号 Location(比下标大 1), 若搜索失败,则函 数返回 CurrentSize+1。 14
使用监视哨的顺序搜索算法 template <class K,class E> int dataList<K,E>::SeqSearch (const K x)const{ Element[CurrentSize].key =x; int i=0; /将X设置为监视哨 while (Element[i].key !=x)++; 川从前向后顺序搜索 return i+1; }; 15
使用监视哨的顺序搜索算法 template <class K, class E> int dataList<K, E>::SeqSearch (const K x) const { Element[CurrentSize].key = x; int i = 0; //将x设置为监视哨 while (Element[i].key != x) i++; //从前向后顺序搜索 return i+1; }; 15