5-5 indexsearch.c
1#include <stdio.h>2 #defineIndextable_len 33 #defineTable_len 304typedefstructItem5 {6 intIndex//Index Value7 intStart//Start Position8 intLength//child table Length9 }indexitem;Ten //defining the primary table data One Longstu[table_len]={ A 1080101,1080102,1080103,1080104,1080105,1080106,0,0,0,0, - 1080201,1080202,1080203,1080204,0,0,0,0,0,0, - 1080301,1080302,1080303,1080304,0,0,0,0,0,0}; the //Defining Index Tables -Indexitem indextable[indextable_len]={ -{10801,0,6}, -{10802,Ten,4}, +{10803, -,4}}; - intIndexsearch (intKey//Find by index + { A inti,index1,start,length; atindex1=key/ -;//Calculate index value - for(i=0; i<indextable_len;i++)//finding index values in the index table - { - if(INDEXTABLE[I].INDEX==INDEX1)//index value found - { -Start=indextable[i].start;//get array start sequence number inLength=indextable[i].length;//get element length - Break;//Jump out of the loop to } + } - if(i>=Indextable_len) the return-1;//failed to find in Index table * for(i=start;i<start+length;i++) $ {Panax Notoginseng if(Stu[i]==key)//Find Keywords - returnI//return ordinal the } + return-1;//lookup failed, return-1 A } the intInsertnode (Key) + { - inti,index1,start,length; $index1=key/ -;//Calculate index value $ for(i=0; i<indextable_len;i++)//finding index values in the index table - { - if(INDEXTABLE[I].INDEX==INDEX1)//index value found the { -Start=indextable[i].start;//get array start sequence numberWuyiLength=indextable[i].length;//get element length the Break;//Jump out of the loop - } Wu } - for(i=0; i<indextable_len;i++)//finding index values in the index table About { $ if(INDEXTABLE[I].INDEX==INDEX1)//index value found - { -Start=indextable[i].start;//get array start sequence number -Length=indextable[i].length;//get element length A Break;//Jump out of the loop + } the } - if(i>=Indextable_len) $ return-1;//failed to find in Index table theStu[start+length]=key;//Save keyword to main table theindextable[i].length++;//to modify the length of a child table in an index table the return 0; the } - in intMain () the { the Longkey; About intI,pos; theprintf"Original data:"); the for(i=0; i<table_len;i++) theprintf"%ld", Stu[i]); +printf"\ n"); -printf"Enter search keywords:"); thescanf"%ld",&key);Bayipos=Indexsearch (key); the if(pos>0) theprintf"the lookup succeeds, which is located in the first%d position of the array. \ n", POS); - Else -printf"Find failed!\n"); theprintf"Enter insert keyword:"); thescanf"%ld",&key); the if(Insertnode (Key) ==-1) theprintf"failed to insert data!\n"); - Else the { the for(i=0; i<table_len;i++) theprintf"%ld", Stu[i]);94printf"\ n"); the } the getch (); the return 0;98}
5.4 Index Lookup