Home > Back-end >  C static table definition and basic operation sequence
C static table definition and basic operation sequence

Time:09-22

/* the realization of the static sequence table */
# include & lt; Iostream>
# define Maxsize 10

using namespace std;
Typedef struct {
The int data [Maxsize];
int length;
} SqlList;
//table long, returns to the length of the linear table L, or the number of data elements in L
{int Length (SqlList L)
Return L.l ength;
}
//in order to find the first element in table L value is equal to the e element, and returns its order
Int LocateElem (SqlList L, int e) {
for (int i=0; i If (L.d ata [I]==e)
Return the I + 1;
}
return -1;
}
//by a find operation, get the position of the element I in table L the value of the
Int GetElem (SqlList L, int I) {
If (I & gt; Length (L) | | I & lt; 1)
Throw "this index is not true".
Return L.d ata [I - 1);
}
///initialization list, construct a linear table of empty
Void InitList (SqlList & amp; L) {
L.length=0;
}
//insert operation, on the position of the ith a in table L insert specified element e
Bool InsertList (SqlList & amp; L, int index, int e) {
If (index & gt; L.l ength | | index & lt;=0)
return false;
For (int I=L.l ength; I & gt;=index; I -) {
L.d ata [I]=L.d ata [I - 1);
}
L.d ata [index - 1]=e;
L.l ength++;
return true;
}

//delete, delete the position of the element in the ith in table L
Bool DeleteList (SqlList & amp; L, int index, int & amp; E) {
If (index & gt; L.l ength | | index & lt;=0)
return false;
E=L.d ata [index - 1);
For (int I=index; i L.d ata [I - 1]=L.d ata [I];
}
L.l ength -;
return true;
}
///create the starting list data
Bool CreateList (SqlList & amp; L, int n) {
If (n & gt; Maxsize | | n & lt;=0)
return false;
cout <"Please enter" & lt; for (int i=0; i Cin & gt;> L.d ata [I];
L.l ength++;
}
}
//print
Void PrintList (SqlList & amp; L) {
for (int i=0; i cout }
cout }
//to empty, and if L is empty table, return true, otherwise it returns false
Bool Empty (SqlList L) {
If (Length (L))
return true;
The else
return false;
}
Int main () {
SqlList L;
InitList (L);
Printf (" please enter to create a table length: ");
int n;
Cin & gt;> n;
CreateList (L, n);
PrintList (L);
cout Int I=LocateElem (L, 4);
cout <"Element is a place in the table 4:" & lt; Int data=https://bbs.csdn.net/topics/GetElem (L, 3);
cout <"A sequence of three element values as" & lt; InsertList (L, 3, 10);
cout <"After the insert list:" & lt; PrintList (L);
Int e=1;
DeleteList (L, 3, e);
cout <"Remove elements as:" & lt; cout <"After delete list:" & lt; PrintList (L);
system("pause");
return 0;
}

CodePudding user response:

What's the problem?
  • Related