Home > Back-end >  [table] sequence basic operation - statically allocated
[table] sequence basic operation - statically allocated

Time:04-16

#include
# define MaxSize 10
Typedef struct {
The int data [MaxSize];
Int length;
} SqList;
Void InitList (SqList & amp; L) {
for(int i=0; iL.d ata [I]=0;
L.l ength=0;
}
Bool CreateList (SqList & amp; L, int n) {
If (n> 10)
return false;
Printf (" please enter the linear element in the table: ");
for(int i=0; iThe scanf (" % d ", & amp; L.d ata [I]);
L.l ength=n;
return true;
}
Void ListTravel SqList (L) {

for(int i=0; iPrintf (" % d ", L.d ata [I]);
}

printf("\n");
}
Bool ListInsert (SqList & amp; L, int, int) e {
If (i<1 | | i> L.l ength + 1)
return false;
If (I==L.l ength + 1) {
L.d ata [I - 1]=e;
L.l ength +=1;
return true;
}
For (int j=L.l ength; J>=i; J -)
L.d ata [j]=L.d ata [1];
L.d ata [I - 1]=e;
L.l ength +=1;
return true;
}
Bool ListDelete (SqList & amp; L, int I) {
If (i<1 | | i>=L.l ength + 1)
return false;
For (int j=I; j<=L.l ength; J++)
L.d ata [1]=L.d ata [j];
L.l ength -=1;
return true;
}
Bool ListDeletee (SqList & amp; Int L, e) {
Int n=0;
for(int i=0; iIf (L.d ata [I]==e) {
For (int j=I; jL.d ata [j]=L.d ata [j + 1);
L.l ength -=1;
i--;
n++;
}
}
If (n==0)
return false;
return true;
}
Bool for ChangeList (SqList & amp; Int L, a, int b) {
Int n=0;
for(int i=0; iIf (L.d ata [I]==a) {
L.d ata [I]=b;
n++;
}
}
If (n==0)
return false;
return true;
}
Bool ChangeListt (SqList & amp; {L, int, int b)
If (i<1 | | i> L.l ength - 1)
return false;
L.d ata [I - 1]=b;
return true;
}
Int GetElem (SqList L, int I) {
Return L.d ata [I - 1);
}
Int * LocateElem (SqList L, int e) {
Static int n [MaxSize];
Int j=0;
for(int i=0; iIf (L.d ata [I]==e) {
N [j + 1)=I + 1;
j++;
}
}
N [0]=j;
return n;
}
Void ClearList (SqList & amp; L) {
L.l ength=0;
}
Void DestroyList (SqList & amp; L) {
Printf (" L has had been destroyed. \ n ");
//static array at the end of the scope of the allocated memory is released (and therefore not as a function return value), don't need to destroy
}
{int Length (SqList L)
Return L.l ength;
}
Bool Empty (SqList L) {
If (L.l ength==0)
return true;
The else
return false;
}
Int main () {
SqList L;
int n;
Printf (" please enter the number of linear elements in the table (& lt;=10) : ");
The scanf (" % d ", & amp; n);
InitList(L);
If (CreateList (L, n))
ListTravel (L);
The else
Printf (" input element number is illegal!" );

//insert
Int I, e;
Printf (" please enter to insert a sequence and insert value: ");
The scanf (" % d % d ", & amp; I, & amp; E);
If (ListInsert (L, I, e))
ListTravel (L);
The else
Printf (" input order illegal!" );

//delete specified order
Printf (" please input to delete a sequence: ");
The scanf (" % d ", & amp; I);
If (ListDelete (L, I))
ListTravel (L);
The else
Printf (" input order illegal!" );

//delete specified values
Printf (" please enter a value: to delete ");
The scanf (" % d ", & amp; E);
If (ListDeletee (L, e))
ListTravel (L);
The else
Printf (" can't find want to delete the value of the!" );

//modify specified values
Int a, b;
Printf (" please input to modify values, and the modified value: ");
The scanf (" % d % d ", & amp; A, & amp; B);
If (for ChangeList (L, a, b))
ListTravel (L);
The else
Printf (" can not find to change the value of the!" );

//modify specifies a sequence of values
Printf (" please input to modify a sequence and the modified value: ");
The scanf (" % d % d ", & amp; I, & amp; B);
If (ChangeListt (L, I, b))
ListTravel (L);
The else
Printf (" input order illegal!" );

//find specifies a sequence of values
Printf (" please enter to find a sequence: ");
The scanf (" % d ", & amp; I);
E=GetElem (L, I);
Printf (" \ n value is % d ", e);

//to find the specified value of order
Printf (" please enter a value to find: ");
The scanf (" % d ", & amp; E);
Int * r=LocateElem (L, e);
for(i=0; iPrintf (" order for % d \ n ", r [I + 1));

//to empty
If (Empty (L))
Printf (" linear list is empty! \n");
The else
Printf (" linear table is not empty! \n");

//o long table
Int l=Length (l);
Printf (" long table for % d \ n "at this time, l);

//remove linear table
ClearList (L);
DestroyList(L);
If (Empty (L))
Printf (" linear list is empty! \n");
The else
Printf (" linear table is not empty! \n");

return 0;
}
  • Related