Order table some basic operations

#include <stdio.h>
#include <stdlib.h>
#define elemType int
#define the MaxSize maximum length sequence table 10 //
#define InitSize 5 // initial length sequence table
typedef struct {
elemType Data *;
int length;
} SqList;
int InitList (SqList * L) {
L-> Data = (elemType *) the malloc (* the MaxSize the sizeof (elemType));
IF (L-> Data!) {the printf ( "memory allocation failure \ n"); return 0;}
elemType * L-P => Data;
for (int I = 0; I <InitSize; I ++) {
* P = I +. 1;
P ++;
} // initial order of the table element is 2. 3. 4. 5. 1
L- > length = InitSize;
return. 1;
} // initialization sequence table
int LocateElem (SqList L *, elemType E) {
elemType * L-P => Data;
int In Flag = 0;
// the printf ( "% D", P * );
for (int I = 0; I <L-> length; I ++) {
IF (E == * P) {
In Flag =. 1;
return I +. 1;
}
P ++;
}
IF (In Flag == 0) {the printf ( "no element in the sequence table% d \ n", e) ; return 0; }
} // return the element to find a position e in order in a table
int ListInsert (SqList L *, I int, elemType e) {
elemType * = L-P> L-Data +>-length. 1;
IF (I < 1 || i> MaxSize) {return 0;} // Analyzing element is inserted is valid
if (L-> length> = MaxSize ) {return 0;} // for out of bounds Analyzing insertion occurs
if (i> L- > length) {* (. 1 + P) = E; L-> = L-length> length +. 1; return. 1;}
for (int J = 0; J <L-> length-I +. 1; J ++) {
* (. 1 + P) = P *;
the P--;
}
* (. 1 + P) = e;
L-> = L-length> length +. 1;
return. 1;
} // e is inserted into the element position i
ListDelete int (SqList L *, int I) {
elemType * L-P => I-Data +. 1;
IF (I <. 1 || I> the MaxSize) {return 0;} // determine the elements that were removed position is valid
if (L-> length <1) {return 0;} // for out of bounds Analyzing insertion occurs
IF (L-I ==> length) {L-> length = L->-length. 1; return. 1;}
for (J int = 0; J <L-> length-I; J ++) {
* = P * (P +. 1);
P ++;
}
L-> length = L->-length. 1;
return. 1;
} // remove located at position i the elements of the sequence table
void printlist (SqList * L) {
elemType * L-P => Data;
the printf ( "output sequence table:");
for (int i = 0; i <L-> length; i ++) {
the printf ( "% D", P *);
P ++;
}
the printf ( "\ n-");
the printf ( "length order table:% D \ n-", L-> length);
} // print out the sequence table
void main () {
SqList L;
int E;
InitList (& L);
ListInsert (& L, 3,6);
ListDelete (L &,. 1);
the printf ( "to find the location of the elements:% d \ n",LocateElem(&L,3));
PrintList(&L);
}

Guess you like

Origin www.cnblogs.com/Yshun/p/11123357.html