1) define the sequence table storage structure
(2) the design sequence table initialization, insert, delete, search algorithms, such as
3. Write the main function test,
# include & lt; stdio.h>
# include & lt; stdlib.h>
# include & lt; The malloc. H>
100//# define LIST_INIT_SIZE order table storage space of the initial allocation amount
10//# define LISTINCREMENT linear table storage space allocation of incremental
Typedef struct {
Int * elem;//storage base address
int length;//the current length
Int listsize;//allocate storage capacity
}SqList;
Int InitList_Sq (SqList& L) {
Linear table L//constructs an empty
L.e lem=(int *) malloc (LIST_INIT_SIZE * sizeof (int));
if (! L.e lem) exit (1);
L.l ength=0;
L.l istsize=LIST_INIT_SIZE;
return 1;
}
Int ListInsert_Sq (SqList& L, int, int) e {
Int * newbase;
//in order linear table L the position I insert a new element e
If (i<1 | | i> L.l ength + 1) return 0;//I value is not a legal
If (L.l ength>=L.l istsize) {//the current storage space is full, the increased allocation
Newbase=(int *) realloc (L.e lem, (L.l istsize + LISTINCREMENT) * sizeof (int));
if(! Newbase) exit (1);
L.e lem=newbase;
L.l istsize +=LISTINCREMENT;
}
Int * p;
Int * q=& amp; (L.e lem] [I - 1);
For (p=& amp; (L.e lem [L.l] ength - 1); p>=q; - * (p + 1)=p) * p;
* q=e;
+ + L.l ength;
return 1;
}
Int ListDelete_Sq (SqList & amp; L, int, int & amp; E)//remove the position of the element, I and e return
{
If ((i<1) | | (i> L.l ength)) return 0;
Int * p=& amp; (L.e lem] [I - 1);
E=* p;
Int * q=L.e lem + L.l ength - 1;
For (+ + p; p<=q; * (p + + p) - 1)=* p;
- L.l ength;
return 1;
}
Int LocateElem_Sq (SqList L, int e, int (* compare) (int, int))//positioning to meet the index to compare function element
{
Int I=1;
Int * p=L.e lem.
While (i<=L.l ength & amp; & ! Compare (*) (* p++, e)) + + I;
If (i<=L.l ength) return the I;
The else return 0;
}
Int print (SqList L)//traverse the print order table element
{the for (int I=0; i
printf("\n");
return 0;
}
Int main () {
SqList L;//declare a sequence table
InitList_Sq (L);
Int [10] e;
Printf (" please enter 10 digit student number: ");
For (int I=0; i<10; I++)
{
The scanf (" % d ", & amp; E [I]);
L.e lem [I]=e [I];
/* L.e lem [I]=I; */
L.length++;
}
Print (L);
ListInsert_Sq (L, 2, 8);//in the second position into 8
Print (L);
Int g;
ListDelete_Sq (L, 2, g);//delete the second position of the element, e returns its value
Printf (" % d \ n ", g);
Print (L);
}
CodePudding user response:
# include & lt; stdio.h>
# include & lt; stdlib.h>
# include & lt; The malloc. H>
100//# define LIST_INIT_SIZE order table storage space of the initial allocation amount
10//# define LISTINCREMENT linear table storage space allocation of incremental
Typedef struct {
Int * elem;//storage base address
int length;//the current length
Int listsize;//allocate storage capacity
}SqList;
Int InitList_Sq (SqList& L)
{
Linear table L//constructs an empty
L.e lem=(int *) malloc (LIST_INIT_SIZE * sizeof (int));
if (! L.e lem)
The exit (1);
L.l ength=0;
L.l istsize=LIST_INIT_SIZE;
return 1;
}
Int ListInsert_Sq (SqList& L, int, int) e
{
Int * newbase;
//in order linear table L the position I insert a new element e
If (i<1 | | i> L.l ength + 1)
return 0;//I value is not a legal
If (L.l ength>=L.l istsize) {//the current storage space is full, the increased allocation
Newbase=(int *) realloc (L.e lem, (L.l istsize + LISTINCREMENT) * sizeof (int));
if(! Newbase) exit (1);
L.e lem=newbase;
L.l istsize +=LISTINCREMENT;
}
Int * p;
Int * q=& amp; (L.e lem] [I - 1);
For (p=& amp; (L.e lem [L.l] ength - 1); P & gt;=q; - p)
* (p + 1)=* p;
* q=e;
+ + L.l ength;
return 1;
}
Int ListDelete_Sq (SqList & amp; L, int, int & amp; E)//remove the position of the element, I and e return
{
If ((i<1) | | (i> L.l ength))
return 0;
Int * p=& amp; (L.e lem] [I - 1);
E=* p;
Int * q=L.e lem + L.l ength - 1;
For (+ + p; p<=q; + + p)
* (p=* p - 1);
- L.l ength;
return 1;
}
Int LocateElem_Sq (SqList L, int e, int (* compare) (int, int))//positioning to meet the index to compare function element
{
Int I=1;
Int * p=L.e lem.
While (i<=L.l ength & amp; & ! Compare (*) (* p++, e)) + + I;
If (i<=L.l ength) return the I;
The else return 0;
}
Int print (SqList L)//traverse the print order table element
{
for(int i=0; iPrintf (" % d ", L.e lem [I]);
printf("\n");
return 0;
}
Int main () {
SqList L;//declare a sequence table
InitList_Sq (L);
//int [10] e;
int e;
Printf (" please enter 10 digit student number: ");
For (int I=0; i<10; I++)
{
//the scanf (" % d ", & amp; E [I]);
The scanf (" % d ", & amp; E);
# if 0
L.e lem [I]=e;
/* L.e lem [I]=I; */
L.length++;
# the else
ListInsert_Sq (L, I + 1, e);
# endif
}
Print (L);
ListInsert_Sq (L, L.l ength + 1, 8).//in the second position into 8
//ListInsert_Sq (L, 2, 8);//in the second position into 8
Print (L);
Int g;
//ListDelete_Sq (L, 2, g);//delete the second position of the element, e returns its value
If (ListDelete_Sq (L, L.l ength, g))//delete the second position of the element, e returns its value
Printf (" % d \ n ", g);
Print (L);
}
Did some modification, for your reference