The next few blogs are all about search, mainly sequential search, half-fold search (applied to ordered tables), binary sort tree search, hash search;
The data structure of sorting is introduced directly.
Sequential lookup and half-fold lookup are sequential table storage; binary sorting tree is binary tree storage; hash lookup is class diagram (similar to adjacent table storage of graph);
Header file (Search.h);
# ifndef _SORT_ typedef int KeyType; typedef struct { KeyType key; }DataType; # endif
Header file (SeqList.h);
# ifndef _SEQLIST_ # define _SEQLIST_ # include <iostream> # include "Search.h" // Refer to the DataType here; using namespace std; # define MAXSIZE 64 typedef struct { DataType data[MAXSIZE]; int length; }SeqList, * PSeqList; typedef struct Node { DataType elem; struct Node * lchild; struct Node * rchild; }BSTree, * PBSTree; //Basic preparation for linear table lookup; PSeqList Init_SeqList(void); bool Full_SeqList(PSeqList p); int Push_SeqList(PSeqList p, KeyType keyValue); void Traversal_SeqList(PSeqList p); ostream & operator<<(ostream & os, DataType dataValue); //Search method based on linear table; //Sequential search; int SeqSearch(PSeqList p, KeyType key);ype * p, DataType * p1, int u, int w);
Implementation file (SeqList.cpp);
# include "SeqList.h" PSeqList Init_SeqList(void) { PSeqList p = (PSeqList)malloc(sizeof(SeqList)); if (NULL != p) { p->length = 0; return p; } else { cout << "Memory allocate is error! " << endl; system("pause"); exit(0); } } bool Full_SeqList(PSeqList p) { if (p->length >= MAXSIZE) { return true; } else { return false; } } int Push_SeqList(PSeqList p, KeyType keyValue) { if (Full_SeqList(p)) { cout << "SeqList is full! " << endl; return -1; } p->data[p->length].key = keyValue; p->length++; return 0; } void Traversal_SeqList(PSeqList p) { for (int i = 0; i < p->length; i++) { cout << p->data[i] << " "; } cout << endl; return; } ostream & operator<<(ostream & os, DataType dataValue) { cout << dataValue.key; return os; } //Search method based on linear table; //Sequential search; int SeqSearch(PSeqList p, KeyType key) { int i = 0; p->data[p->length].key = key; while (p->data[i].key != key)//The algorithm with outposts must be a dissatisfaction table, or it will make mistakes. { i++; } if (i == p->length) { return -1; } else { return i; } //for (int i = 0; i < p->length; i++) //{ // if (p->data[i].key == key) // { // return i; // } //} return -1; }
Main.cpp;
# include "SeqList.h" int main(int argc, char ** argv) { PSeqList p = Init_SeqList(); for (int i = 0; i < 10; i++) { Push_SeqList(p, i + 1); } cout << "------------------------SeqList Search------------------------" << endl; Traversal_SeqList(p); //This is the test area, which can test various storage structures based on linear tables. //The latter is not changed one by one; they are the same; just change the name of a function; cout << "Your search position is : " << SeqSearch(p, 0) << endl; cout << "Your search position is : " << SeqSearch(p, 1) << endl; cout << "Your search position is : " << SeqSearch(p, 5) << endl; cout << "Your search position is : " << SeqSearch(p, 10) << endl; cout << "Your search position is : " << SeqSearch(p, 11) << endl << endl; cout << "------------------------TreeList Search------------------------" << endl; //This is the test area, which can test various binary tree-based storage structures. PBSTree pt = NULL; PBSTree pp = NULL; BSTreeInsert(&pt, 34); BSTreeInsert(&pt, 18); BSTreeInsert(&pt, 76); BSTreeInsert(&pt, 13); BSTreeInsert(&pt, 25); BSTreeInsert(&pt, 52); BSTreeInsert(&pt, 82); BSTreeInsert(&pt, 20); BSTreeInsert(&pt, 67); BSTreeInsert(&pt, 91); BSTreeInsert(&pt, 58); BSTreeInsert(&pt, 73); InOrder(pt); cout << endl; if (NULL != (pp = BSTreeSearch(pt, 13))) { cout << pp->elem.key << endl; } BSTreeDelete(&pt, 34); InOrder(pt); cout << endl; cout << "------------------------Hash Search------------------------" << endl; //This is the test area; it can test the storage structure of hash tables; PHashTable pht = CreateHashTable(); HashTableInsert(pht, 26); HashTableInsert(pht, 38); HashTableInsert(pht, 73); HashTableInsert(pht, 21); HashTableInsert(pht, 54); HashTableInsert(pht, 35); HashTableInsert(pht, 167); HashTableInsert(pht, 32); HashTableInsert(pht, 7); HashTableInsert(pht, 223); HashTableInsert(pht, 62); HashTableTraversal(pht); system("pause"); return 0; }