Data structure algorithm C language implementation (32)---9.1 Static lookup table

Source: Internet
Author: User

First, brief

Static lookup tables are divided into sequential tables, ordered tables, static tree tables, and index tables. The following is a simple implementation of the algorithm and testing, not involving performance analysis.

Second, the head file

1 /**2 Author:zhaoyu3 date:2016-7-124 */5#include"6_3_part1_for_chapter9.h"6typedefstruct {7     intkey;8 }selemtype;9 //sequential storage structure for static lookup tablesTen #defineElemtype Selemtype One #defineKeyType int Atypedefstruct { -Elemtype *elem;//data element Storage base, number No. 0 left blank -     intLength//Table Length the }sstable; - //This simplifies the key values - //Implementing EQ - BOOLEQ (intAintb) + { -     returnA==b?true:false; + } A BOOLLT (intAintb) at { -     returnA<b?true:false; - } - //Implement Create Sstbale - voidCreatesstable (Sstable &T) - { in     //Enter length first -     //and enter the length of the element toscanf"%d", &t.length); +T.elem = (Elemtype *)malloc((T.length +Ten)*sizeof(int)); -      for(inti =1; I <= t.length; i++) the     { *scanf"%d", &t.elem[i].key); $     }Panax Notoginseng } -  the /** + Algorithm 9.1 A */ the intsearch_seq (sstable ST, KeyType key) + { -     //in order table ST in order to find the key values equal to the data element of key, $     //returns 0 if the return element is found in the table position $st.elem[0].key =key; -     inti; -      for(i = st.length;!) EQ (St.elem[i].key, key); --i); the     returni; - Wuyi } the  - /** Wu Algorithm 9.2 - */ About intSearch_bin (sstable ST, KeyType key) $ { -     //finding data elements whose keywords are equal to key in the ordered table ST binary -     //find return to its position, otherwise return 0 -     intLow =1; A     intHigh =st.length; +      while(Low <=High ) the     { -         intMid = (low + high)/2; $         if(EQ (Key, St.elem[mid].key)) the         { the             returnmid; the         } the         Else if(LT (Key, St.elem[mid].key)) -         { inHigh = mid-1; the         } the         Else About         { theLow = mid +1; the         } the     } +     return 0; -}
9_1_1.h

Iii. CPP File

1#include"9_1_1.h"2 intMainintargcChar Const*argv[])3 {4 sstable T;5 createsstable (T);6printf"Search 9:\t%d\n", Search_seq (T,9));7printf"Search 3:\t%d\n", Search_bin (T,3));8     return 0;9}
9_1_1.cpp

Iv. Testing

  

Data structure algorithm C language implementation (32)---9.1 Static lookup table

Related Article

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.