Home > Back-end >  The sequential table according to the value of search data structure
The sequential table according to the value of search data structure

Time:10-25

Order table lookup by value, regardless of input right or wrong, the output is not found, the result of the
 # define _CRT_SECURE_NO_WARNINGS 
#include
#include
# include & lt; The malloc. H>
# include & lt; Stdlib. H>
using namespace std;
# define MAXSIZE 10
# define OK 1
# define the ERROR 0
# define OVERFLOW - 2


Typedef struct {
Const char * name;
Const char * number;
Int grade;
} ElemType;
Typedef struct {
ElemType * elem;
int length;
Int listsize;
}SqList;

//constructs an empty sequence table
Int InitList_Sq (SqList& L)
{
L.e lem=new ElemType [MAXSIZE];
if (! L.e lem) exit (OVERFLOW);
L.l ength=0;
L.l istsize=MAXSIZE;
return OK;

}

Int append_Sq SqList * L, ElemType (e)
{
Int I=L - & gt; Length;
L - & gt; Elem [I]=e;
L - & gt; Length++;
return OK;
}

//get the ith a data element in linear table L the content of the
Int GetElem (SqList L, int I ElemType& E)
{
If (i<1 | | i> L.l ength) return the ERROR;
E=L.e lem [I - 1);
return OK;
}
//find the linear table with equal e data elements, returns its position in the linear table,
Int LocateElem (SqList L, ElemType e)
{
int i;
For (I=0; I & lt; L.l ength; I++)
{
If ((L.e lem [I] name==e.n ame) & amp; & (L.e lem [I] number==e.n umber) & amp; & (L.e lem [I] grade==um participant rade))
Return the I + 1;
}
return 0;
}

//determine whether linear table is the "full"
Int isFull_Sq (SqList * L)
{
The return of L - & gt; Length==MAXSIZE? OK: the ERROR;
}

//determine whether linear table is the "empty"
Int isEmpty_Sq (SqList * L)
{
The return of L - & gt; Length==0? OK: the ERROR;
}

//in the linear table data elements of the ith line insert e
Int ListInsert_Sq (SqList& L, int I ElemType e)
{
If (i<1 | | i> L.l ength + 1)
Return the ERROR;
If (L.l ength==MAXSIZE)
Return the ERROR;
For (int j=L.l ength - 1; J & gt;=I - 1; J -)
{
L.e lem [j + 1)=L.e lem [j];
L.e lem] [I - 1=e;
+ + L.l ength;
}
return OK;
}

//remove linear table
Int ListDelete_Sq (SqList& L, int I)
{
If ((I & lt; 1) | | (I & gt; L.l ength))
Return the ERROR;
For (int j=I; J & lt;=L.l ength - 1; J++)
L.e lem [1]=L.e lem [j];
- L.l ength;
return OK;
}

//this program is used to output to determine the linear table content in a certain area
Void ListTraverse (SqList L)
{
int i;
Printf (" linear table current capacity is: % d \ n ", L.l istsize);
If (L.l ength & gt; 0)
{
Printf (" % d element, the current linear table are \ n ", L.l ength);
For (I=0; I & lt;=L.l ength; I++)
{
Printf (" % d % s % s \ n ", L.e lem [I]. Name, L.e lem [I] number, L.e lem [I] grade).
}
}
The else
Printf (" linear table is empty. \ n ");
}

Int main ()
{
//the first step to initialize the linear table
SqList L;
int i;
Printf (" the first step to create a structure variable, initialize \ n ");
InitList_Sq (L);
Printf (" the second step is to order table assignment \ n ");

//the second step is to order table assignment
ElemType s [4]={
{" yangyang ", "060214201", 79},
{" xuelin ", "060214202", 83},
{" wangshimeng ", "060214203", 92},
{" fengzihan ", "060214204", 88}
};
For (I=1; I & lt;=4; I++)
{
If (append_Sq (& amp; L, s [I - 1)))
Printf (" add data success \ n ");
The else
Printf (" add data failure \ n ");
}

//the third step to determine whether a linear table for full
Printf (" the third step to determine whether a linear table for full \ n ");
Int isFull_Sq SqList (L);
If (OK)
Printf (" linear table is for full \ n ");
The else
Printf (" linear table for full \ n ");

//step 4 to determine whether a linear list is empty
Printf (" the fourth step to determine whether a linear list is empty \ n ");
Int isEmpty_Sq SqList (L);
If (OK)
Printf (" linear table is for empty \ n ");
The else
Printf (" linear list is empty \ n ");

//step 5 according to the position of value k
Printf (" step 5 according to the position of k value ");
Int k, getElem_flag;
ElemType value;
Printf (" please enter the location k value: ");
The scanf (" % d ", & amp; K);
GetElem_flag=GetElem (L, k, value);
If (getElem_flag==1)
{
Printf (" access to the data for: \ n ");
Printf (" % d % s % s \ n ", the value. The name, value, number, value, grade).
}
The else
Printf (" input the position of the cross! \n");

//step 6 by value lookup, e return position
Printf (" step 6 by value e lookup, return position \ n ");
Int local_flag;
ElemType e;
Printf (" please enter the query value: \ n ");
The scanf (" % d % s % s ", & amp; E.n ame, & amp; E.n umber, & amp; Um participant rade);
Local_flag=LocateElem (L, e);
If (local_flag!=0)
Printf (" \ n the element's position is: % d ", local_flag);
The else
Printf (" could not find the element! \n");

//step 7 insert operation
Printf (" step 7 in position I insert element e \ n ");
Int Insert_i Insert_flag;
Int h=0;
ElemType Insert_value;
Printf (" please enter the location I: ");
The scanf (" % d ", & amp; Insert_i);
Printf (" please enter the inserting value: ");
The scanf (" % d % s % s ", & amp; Insert_value. Name, & amp; Insert_value. Number, & amp; Insert_value. Grade);
Insert_flag=ListInsert_Sq (L, Insert_i Insert_value);
If (Insert_flag)
{
Printf (" insert success! \n");
Printf (" the current is the length of the sequence table L % d \ n ", L.l ength);
}
The else
Printf (" insert failed! \n");

//step 8 delete operation, the table of the ith element delete
Printf (" step 8 delete operation, the table of the ith element deletion \ n ");
Int delete_i delete_flag;
Printf (" please input element I to delete ");
The scanf (" % d ", & amp; Delete_i);
Delete_flag=ListDelete_Sq (L, delete_i);
If (delete_flag)
{
Printf (" deleted successful!" );
Printf (" the current is the length of the sequence table L % d \ n ", L.l ength);
}
return OK;
}
  • Related