Home > Back-end >  1. . Write a program to implement the order table of all kinds of basic operation, on the basis of t
1. . Write a program to implement the order table of all kinds of basic operation, on the basis of t

Time:11-01

#include
# define Maxsize 100
# define OK 1
# define the ERROR 0
# define OVERFLOW - 2
using namespace std;
Typedef char ElemType;
Typedef int the Status;
Typedef struct
{
ElemType * elem;//storage base address
Int length;//the current length
}SqList;
The Status InitList (SqList & amp; L)//initialization sequence table
{
L.e lem=new ElemType [Maxsize];
if(! L.e lem) exit (OVERFLOW);//storage allocation failure
L.l ength=0;
Return OK;
}
Void InsertList (SqList & amp; L)//to the order in the table insert element
{
Int I, num;
Cout<& lt;" (2), in turn, insert elements in the sequence table L a, b, c, e, "& lt; Cin> Num.
L.l ength=num;
For (I=1; iCin & gt;> L.e lem [I];
}
Void PrintList (SqList L)//output sequence table L
{
int i;
For (I=1; i <=L.l ength; I++)
Cout & lt; Cout}
Void LengthList SqList (L)//output the length of the sequence table
{
Cout & lt; <"(4) the length of the sequence table is:" & lt; }
Void SearchList SqList (L)//query sequence table L of the ith element
{
int i;
Cout & lt; <"(5) please input to query the location of the element:";
Cin & gt;> i;
Cout & lt; <"(5) your query element is:" & lt; }
Int LocateElem (SqList L, ElemType e)//the location of the output element a
{
for(int i=1; iIf (L.e lem [I]==e)
Cout<& lt;" (6) elements of a position is: "& lt; return 0;
}
The Status ListInsert (SqList & amp; L, int I ElemType e)//before the first element I insert element f
{
If ((i<1) | | (i> L.l ength + 1) | | (L.l ength==Maxsize)) return the ERROR;
For (int j=L.l ength + 1; J> i; J -)
L.e lem [j]=L.e lem [j - 1);
L.e lem [I]=e;
+ + L.l ength;
Return OK;
}
The Status ListDelete (SqList & amp; L, int I)//delete the ith element
{
If ((i<1) | | (i> L.l ength)) return the ERROR;
For (int j=I; j<=L.l ength; J++)
L.e lem [j]=L.e lem [j + 1);
- L.l ength;
return 0;
}
Int main ()
{
SqList L;
Cout<& lt;" (1) the initialization sequence table L: "& lt; InitList (L);
InsertList (L);
Cout<& lt;" (3) the output sequence table L: ";
PrintList (L);
LengthList (L);
SearchList (L);
LocateElem (L, 'a');
ListInsert (L, 4, 'f');
Cout<& lt;" After insert element (7) (8) f output sequence table L: ";
PrintList (L);
ListDelete (L, 3);
Cout<& lt;" (9) (10) to delete the third element output sequence table L: after ";
PrintList (L);
}