Home > Back-end >  The basic operation of the linear table order and implementation
The basic operation of the linear table order and implementation

Time:03-29

Have bosses can look at my code? half a year, just learning C language is not a special foundation
Compile environment is visual Stdio found no syntax errors, but read access violation occurred operation process, what should I do!

//file: xc. H

# # ifndef x_h
# define x_h
#include
#include
# define MAXSIZE 20//storage for the initial allocation amount
# define true 1
# define FALSE 0
# define OK 1
# define the ERROR 0
# define INFEASIBLE - 1
# define OVERFLOW - 2
Typedef int ElemType;
Typedef int the Status;
Typedef struct
{
ElemType * elem;//array elements stored data, the maximum for MAXSIZE
Int listsize;
int len;//linear table
the current length} SqList;
Void CreateList (SqList * L, ElemType a [5], int n);//should be the first statement of the constructed linear table operation or linear correlation function table
The Status InitList (SqList * L);
The Status DestroyList (SqList * L);
The Status ClearList (SqList * L);
The Status ListEmpty SqList (L);
The Status ListLength SqList (L);
The Status GetELem SqList L, int, int * (e);
The Status LocateList (SqList L, int e);
The Status PriorElem (SqList L, int cur_e, int * pri_e);
The Status NextElem (SqList L, int cur_e, int * Nex_e);
The Status ListInsert (SqList * L, int, int) e;
Status ListDelete SqList * L, int, int * (e);
The Status TravelList SqList (L);
# endif

//file: c
2.
# include "xc. H"
C # include "xc."
Void MergeList (SqList La SqList Lb, SqList * Lc)
{
Int I, j, k, La_len, Lb_len, ai, bj.
I=j=0;
k=0;
La_len=La. Len;
Lb_len=Lb. Len;
While (I & lt; La_len & amp; & J & lt; Lb_len)
{
GetELem (La, I, & amp; Ai);
GetELem (Lb, j., & amp; Bj);
If (ai & lt;=bj)
{
ListInsert (& amp; Lc, k++, ai);
+ + I;
}
The else
{
ListInsert (& amp; Lc, k++, bj);
+ + j;
}
}
While (I & lt; La_len)
{
GetELem (La, i++, & amp; Ai);
ListInsert (& amp; Lc, k++, ai);
}
While (j & lt; Lb_len)
{
GetELem (Lb, j++, & amp; Bj);
ListInsert (& amp; Lc, k++, bj);
}
}
Int main ()
{
Int la [5]=,3,5,7,9 {1};
Int lb [5]=,3,6,8,10 {2};
Int I, m, n, k;
Int a=10;
I=3;
SqList La, Lb, Lc;
InitList (& amp; La);
InitList (& amp; Lb);
InitList (& amp; Lc);
CreateList (& amp; La, La, 5);
CreateList (& amp; Lb, Lb, 5);
Printf (" show has been founded by the linear table below La Lb \ n \ n ");
Linear table La: printf (" \ n ");
TravelList (La);
The linear table Lb: printf (" \ n \ n ");
TravelList (Lb);
Printf (" \ n combined linear table below La and Lb become Lc: \ n ");
MergeList (La and Lb, & amp; Lc);
TravelList (Lc);
Printf (" the destruction of the linear table below Lc \ n after the destruction of the linear list is empty \ n ");
DestroyList (& amp; Lc);
TravelList (Lc);
Printf (" below to determine whether a linear list is empty: % d (1, 0 for no) \ n ", ListEmpty (Lc));
Printf (" \ n below show the length of the existing linear table La is: % d \ n ", ListLength (La));
Printf (" \ n in the linear table La third element: % d \ n ", a);
Printf (" linear table Lb \ n \ n the following empty is empty after Lb: \ n ");
ClearList (& amp; Lb);
TravelList (Lb);
Printf (" below to determine whether a linear list is empty: % d (1, 0 for no) \ n ", ListEmpty (Lb));
Printf (" \ n looking for linear table below the third element in La, and the third element of precursor and the subsequent \ n ");
M=GetELem (La, I - 1, a);
N=PriorElem (La, I - 1, a);
K=NextElem (La, I - 1, a);
Printf (" the third element is: % d \ n the third element of the precursor is: % d \ n the third element of subsequent is: % d \ n \ n ", m, n, k);
Printf (" La, we insert an element in the table below \ n ");
Printf (" insert into the third position element a=10 \ n ");
A=10;
ListInsert (& amp; La, I, a);
Printf (" inserted into the linear table after La: \ n ");
TravelList (La);
Printf (" \ n we delete the resulting from the third element of the linear table La La: \ n ");
ListDelete (& amp; La, I, a);
TravelList (La);
Printf (" \ n the linear table lookup to see if there is value for 3 elements: \ n ");
LocateList (La, I);
}



//file: xc. C

# include "xc. H"
Void CreateList (SqList * L, ElemType a [100], int n)//should be the first statement of the constructed linear table operation or linear correlation function table
{
int i;
For (I=0; I & lt; n; I++)
{
* (L - & gt; Elem + I)=a, [I].
}
L - & gt; Len=n;
}
The Status InitList (SqList * L)
{
L - & gt; Elem=(ElemType *) malloc (MAXSIZE * sizeof (ElemType));
if (! L - & gt; Elem) exit (OVERFLOW);//error exit
L - & gt; Len=0;
L - & gt; Listsize=MAXSIZE;
return OK;
}
The Status DestroyList (SqList * L)
{
Free (L - & gt; Elem);
L - & gt; Elem=NULL;
L - & gt; Len=0;
L - & gt; Listsize=0;
return OK;
}
The Status ClearList (SqList * L)
{
L - & gt; Len=0;
return OK;
}
The Status ListEmpty SqList (L)
{
If (L.l en==0)
Return true;
The else
Return FALSE;
}
The Status ListLength SqList (L)
{
Return L.l en;
}

The Status GetELem SqList L, int, int * (e)
{
If (I & lt; L.l en)
{
E=L.e lem [I];
return e;
}
The else
{
Return FALSE;
}
}
The Status LocateList (SqList L, int e)
{
int i;
For (I=0; I & lt; L.l en; I++)
{
If (L.e lem [I]==e)
{
Printf (" % d value in the table element is the first % d element \ n ", e, I + 1);
return i;
}
}
}
The Status PriorElem (SqList L, int cur_e, int * pri_e)
{

If (cur_e + 1==0)
{
Return FALSE;
}
If (cur_e + 1!=0 & amp; & Cur_e & lt; L.l en)
{
Pri_e=L.e lem [cur_e - 1);
Return pri_e;
}
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related