[Data structure] Half-Lookup

Source: Internet
Author: User

Half-lookup of data structures

/* Name: Half lookup language: Data Structure C language compiling environment: VC ++ 6.0 Date: */# include <stdio. h> # include <malloc. h> # include <windows. h> # define N 11 // number of data elements typedef int KeyType; // set the keyword field to integer typedef struct // data element type {KeyType key; // The keyword field int others; // other part} ElemType; // Search_Seq.h the sequence storage structure of the static search table. The base address of the data element storage space is typedef struct {// allocated according to the actual length during table creation, element type * elem; int length; // table length} SSTable; ElemType r [N] = }, },{ }}; // Data elements (taking P219 data as an example), basic operations for global variables // static search tables (ordered tables and ordered tables) (7) // construct a static sequence query table ST (data from the Global Array r) int Creat_Seq (SSTable * ST, int n) {int I; (* ST ). elem = (ElemType *) calloc (n + 1, sizeof (ElemType); // dynamically generate n + 1 Data Element space (No unit 0) if (! (* ST ). elem) return 0; for (I = 1; I <= n; I ++) * (* ST ). elem + I) = r [I-1]; // assigns the value of the global array r to ST (* ST) in sequence ). length = n; return 1 ;}// rebuild the static search table to sort the void Ascend (SSTable * ST) by keyword in non-descending order. {int I, j, k; for (I = 1; I <(* ST ). length; I ++) {k = I; (* ST ). elem [0] = (* ST ). elem [I]; // the value to be compared is stored in the Unit [0] for (j = I + 1; j <= (* ST ). length; j ++) // find the smaller I value if (* ST ). elem [j]. key <(* ST ). elem [0]. key) {k = j; (* ST ). elem [0] = (* ST ). elem [j];} if (k! = I) // if there is a smaller value, the {(* ST) is exchanged ). elem [k] = (* ST ). elem [I]; (* ST ). elem [I] = (* ST ). elem [0] ;}}// construct a static search table ST with n data elements in non-descending order of keywords, // data comes from the Global Array r int Creat_Ord (SSTable * ST, int n) {int f; f = Creat_Seq (ST, n); // construct a static table if (f) // If a static table exists, perform Ascend (ST); return f;} // Destroy the table ST int Destroy (SSTable * ST) {free (* ST ). elem); (* ST ). elem = NULL; (* ST ). length = 0; return 1;} // algorithm 9.2 P220 // search for the data element whose keyword is key in the ST segment of the ordered table. If yes, the function // value is the position of the element in the table. Otherwise, the value is 0. Int Search_Bin (SSTable ST, KeyType key) {int low, high, mid; low = 1; // set the initial value of the interval to high = ST. length; while (low <= high) {mid = (low + high)/2; if (key = ST. elem [mid]. key) // find the element to be queried: return mid; else if (key <ST. elem [mid]. key) high = mid-1; // continue searching for elselow = mid + 1 in the First Half; // continue searching in the second half} return 0; // The sequence table does not contain the element to be queried} // the Visit () function is called once and only once for each element of ST in order. Int Traverse (SSTable ST, void (* Visit) (ElemType) {ElemType * p; int I; p = ++ ST. elem; // p points to the first element. The 0th elements are not used for (I = 1; I <= ST. length; I ++) Visit (* p ++); return 1;} void print (ElemType c) // Traverse () the called function {printf ("(% d)", c. key, c. others) ;}int main () {SSTable st; int I; KeyType s; Creat_Ord (& st, N ); // generate a non-descending static search table st Traverse (st, print) from the global array ); // stprintf ("\ n enter the keyword of the value to be searched:"); scanf ("% d", & s ); I = Search_Bin (st, s); // semi-query the ordered table if (I) print (st. elem [I]); elseprintf ("not found. \ n "); Destroy (& st); system (" pause "); return 0;}/* output result: (5 1) (13 2) (19 3) (21 4) (37 5) (56 6) (64 7) (75 8) (80 9) (88 10) (92 11) enter a keyword for the value to be searched: 75 (75 8) press any key to continue... */

The running result is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.