catalogue
1, Definition of two classes of linear table structure
2, Initialization of linear table and establishment of linear table according to input elements
1. Initialization of linear table. Initialize an empty linear table
2. Add elements to the linear table according to user requirements
5, Half search (non recursive) Search3 function
6, Half search (recursive) Search4 function
7, Show output function show function
Function 2: sequential search without sentry
Function 3: find sentinels in sequence
Function 4: half search non recursive
Function 4: half search non recursive
1, Definition of two classes of linear table structure
typedef struct//Define linear table data item structure { int key;//Keyword domain int otherinfo; }ElemType; typedef struct//Define a linear table structure { int length;//Length of sequence table ElemType* R;//For the dynamic establishment of sequence table, R is used to dynamically establish a continuous storage space, which is the base address of the established storage space }SeqList;
2, Initialization of linear table and establishment of linear table according to input elements
1. Initialization of linear table. Initialize an empty linear table
In order to use the bool function, you need to add #include < stdpool. Exe to the header file h>
The code is as follows (example):
//Initialize linear table bool InitList(SeqList& T)//&Symbol, two-way transfer, reference { T.R = (ElemType*)malloc(sizeof(ElemType) * MaxSize);//Allocate a space of this type with a size of MaxSize and return the base address R if (!T.R)//Check whether space is successfully allocated to linear table T { printf("Failed to allocate space"); return false; } T.length = 0;//Let L.length=0 return true; }
2. Add elements to the linear table according to user requirements
We just started to input data and assign values to the space in the linear table, starting from position 1. Position 0 is used to store sentinels
//Establish a linear table based on the input elements bool CreateList(SeqList& T) { int n; printf("Please enter the number of elements in the linear table you want to create:\n"); scanf_s("%d", &n); printf("Please enter the elements in turn:\n"); for (int i = 1; i <= n; i++) { scanf_s("%d", &T.R[i].key);//assignment L.length++//!!!!!!!! This must be remembered. The first time you forget it, the later functions are implemented incorrectly } return true; }
3, Search the search 1 function in sequence (there is no sentry set, so it needs to be compared twice)
//Sequential search (no sentry) int Search1(SeqList T, int key) { for (int i=1; i<=n; i++) { if (T.R[i].key == key) return i; } return 0; }
4, Sequential search (set sentry, do not compare whether it will cross the boundary, only compare once) Search2 function
int Search2(SeqList T, int key) //Sequential search (set sentry) { //In the sequence table ST, find the data elements whose keyword is equal to key in sequence. If found, the function value is //The position of the element in the table, otherwise 0 int i; T.R[0].key = key; //"Sentry" for (i =T.length; T.R[i].key != key; i--); //Look from the back, because the sentry we set is in the first position //So look from the back return i; }
5, Half search (non recursive) Search3 function
//Half search (non recursive) int Search3(SeqList T, int key) { int low = 1; int high = T.length; while (low <= high) { int mid = (low + high) / 2; if (T.R[mid].key == key) return mid; else if (T.R[mid].key < key) low = mid + 1; else high = mid - 1; } }
6, Half search (recursive) Search4 function
//Half search (recursive) int Search4(SeqList T, int key, int low, int high) { //low = 1; //high = T.length; int mid = (low + high) / 2; if (T.R[mid].key == key) return mid; else if (T.R[mid].key < key) Search4(T, key, mid + 1, high); else Search4(T, key, low, mid-1); }
7, Show output function show function
void Show(int result, int key) { if (result == 0) printf("The element was not found"); else printf("Find element%d,In the first%d position", key, result); } /*This result is the return value i in the four lookup functions we implemented before, because we just started to input data and assign values to the space in the linear table, starting from position 1, and position 0 is used to store sentinels. So the last i returned is the location of the element If i=0, it means that the element is not found, and result=0*/
8, In order to realize the above functions in the program at one time, we have established a Menu function to perform different operations according to the user's choice
It's really like my big homework (stupid)
void Menu() { printf("\n************menu***********\n"); printf("\n1.Create linear table;\n"); printf("\n2.Sequential search((no sentry);\n"); printf("\n3.Sequential search((sentry);\n"); printf("\n4.Half search(non-recursive );\n"); printf("\n5.Half search(recursion);\n"); printf("\n0.sign out;\n"); printf("\n***************************\n"); printf("\n[Please enter your choice]\n>>>"); printf("\n"); printf("\n"); printf("\n"); }
9, Complete code
Because I am running in visual, the input is scanf_s. If UUS run in their own compiled software and there is an error, scanf_s ------ > scanf
#include<stdio.h> #include<stdlib.h> #include<stdbool. h> / / if you want to use bool type, call the header file #define MaxSize 10 typedef struct//Define linear table data item structure { int key;//Keyword domain int otherinfo; }ElemType; typedef struct//Define a linear table structure { int length;//Length of sequence table ElemType* R;//For the dynamic establishment of sequence table, R is used to dynamically establish a continuous storage space, which is the base address of the established storage space }SeqList; //Initialize linear table bool InitList(SeqList& T)//&Symbol, two-way transfer, reference { T.R = (ElemType*)malloc(sizeof(ElemType) * MaxSize);//Allocate a space of this type with a size of MaxSize and return the base address R if (!T.R)//Check whether space is successfully allocated to linear table T { printf("Failed to allocate space"); return false; } T.length = 0;//Let L.length=0 return true; } //Establish a linear table based on the input elements bool CreateList(SeqList& T) { int n; printf("Please enter the number of elements in the linear table you want to create:\n"); scanf_s("%d", &n); printf("Please enter the elements in turn:\n"); for (int i = 1; i <= n; i++) { scanf_s("%d", &T.R[i].key);//assignment L.length++//!!!!!!!! This must be remembered. The first time you forget it, the later functions are implemented incorrectly } return true; } //Sequential search (no sentry) int Search1(SeqList T, int key) { for (int i=1; i<=n; i++) { if (T.R[i].key == key) return i; } return 0; } /*//Sequential search (set up sentry) int Search2(SeqList T, int key) { T.R[0].key = key; int i; for (i = T.length; T.R[i].key != key;i--); return i; }*/ int Search2(SeqList T, int key) //Sequential search (set up sentry) { //In the sequence table ST, find the data elements whose keyword is equal to key in sequence. If found, the function value is //The position of the element in the table, otherwise 0 int i; T.R[0].key = key; //"Sentry" for (i =T.length; T.R[i].key != key; i--); //Look from the back return i; }// Search_Seq //Half search (non recursive) int Search3(SeqList T, int key) { int low = 1; int high = T.length; while (low <= high) { int mid = (low + high) / 2; if (T.R[mid].key == key) return mid; else if (T.R[mid].key < key) low = mid + 1; else high = mid - 1; } } //Half search (recursive) int Search4(SeqList T, int key, int low, int high) { //low = 1; //high = T.length; int mid = (low + high) / 2; if (T.R[mid].key == key) return mid; else if (T.R[mid].key < key) Search4(T, key, mid + 1, high); else Search4(T, key, low, mid-1); } void Show(int result, int key) { if (result == 0) printf("The element was not found"); else printf("Find element%d,In the first%d position", key, result); } void Menu() { printf("\n************menu***********\n"); printf("\n1.Create linear table;\n"); printf("\n2.Sequential search((no sentry);\n"); printf("\n3.Sequential search((sentry);\n"); printf("\n4.Half search(non-recursive );\n"); printf("\n5.Half search(recursion);\n"); printf("\n0.sign out;\n"); printf("\n***************************\n"); printf("\n[Please enter your choice]\n>>>"); printf("\n"); printf("\n"); printf("\n"); } int main() { SeqList T; int key, result, user; while (true) { Menu(); scanf_s("%d", &user); switch (user) { case 1: { if (InitList(T) && CreateList(T)) printf("\n[Successfully created...]\n"); break; } case 2: { printf("\n-----------Sequential search without sentry-----------\n"); printf("\n[Please enter the keyword to find]\n>>>"); //getchar(); scanf_s("%d", &key); result = Search1(T, key); Show(result, key); printf("\n-------------------------------\n"); break; } case 3: { printf("\n-----------Sequential search Sentinel-----------\n"); printf("\n[Please enter the keyword to find]\n>>>"); //getchar(); scanf_s("%d", &key); result = Search2(T, key); Show(result, key); printf("\n-------------------------------\n"); break; } case 4: { printf("\n-----------Half search(non-recursive )-----------\n"); printf("\n[Please enter the keyword to find]\n>>>"); //getchar(); scanf_s("%d", &key); result = Search3(T, key); Show(result, key); printf("\n---------------------------------------\n"); break; } case 5: { printf("\n-----------Half search(recursion)-----------\n"); printf("\n[Please enter the keyword to find]\n>>>"); //getchar(); scanf_s("%d", &key); result = Search4(T, key, 1, T.length); Show(result, key); printf("\n-------------------------------------\n"); break; } case 0:default; } } return 0; }