#include <stdio.h>
#include <stdlib.h>
/*
typedef struct Student {
char* name;
int nID;
}ElemType;
*/
typedef int ElemType;
struct List {
ElemType *pElem;
int nLength;
int nSize;
};
void InitList(struct List *L,int nSize)
{
L->pElem = malloc(sizeof(ElemType)*nSize);
if ( !L->pElem )
{
printf("Error!");
exit(1);
}
L->nLength = 0;
L->nSize = nSize;
}
void DestroyList(struct List *L)
{
if ( L->pElem )
{
free(L->pElem);
L->pElem = NULL;
L->nLength = L->nSize = 0;
}
}
int ListLength(struct List *L)
{
return L->nLength;
}
int EmptyList(struct List *L)
{
if ( L->nLength == 0 )
return 1;
else
return 0;
}
void GetElemByPos(struct List *L,int nPos,ElemType *pData)
{
if ( nPos < 1 || nPos > L->nLength )
return ;
else
*pData = L->pElem[nPos-1];
}
/* the difference between exit and return. */
/*
ElemType GetElemByPos(struct List *L,int nPos)
{
if ( nPos < 1 || nPos > L->nLength )
exit(1);
else
return L->pElem[nPos-1];
}
*/
void TraverseList(struct List *L)
{
int i;
for ( i= 0; i<L->nLength; i++ )
{
printf("%d/n",L->pElem[i]);
}
printf("/n");
}
int FindElemPos(struct List *L,ElemType data)
{
int i;
for ( i=0; i<L->nLength; i++ )
{
if ( L->pElem[i] == data )
return i+1;
}
return -1;
}
int UpdateList(struct List *L,int nPos,ElemType data)
{
if ( nPos<1 || nPos >L->nLength )
return 0;
L->pElem[nPos-1] = data;
return 1;
}
void AgainMalloc(struct List *L)
{
int i;
/* struct List *p = L; */
ElemType *pElem = L->pElem;
L->pElem = malloc(sizeof(ElemType)*L->nSize*2);
L->nSize = 2*L->nSize;
for ( i=0; i<L->nLength; i++ )
{
L->pElem[i] = pElem[i];
}
free(pElem);
pElem = NULL;
}
void ContinueMalloc(struct List *L)
{
ElemType *p = realloc(L->pElem, 2 * L->nSize * sizeof(ElemType));
if(!p){
exit(1);
}
L->pElem = p;
L->nSize = 2 * L->nSize;
}
void InsertListFirst(struct List *L,ElemType data)
{
int i;
if ( L->nLength == L->nSize )
AgainMalloc(L);
for ( i=L->nLength;i>0;i-- )
{
L->pElem[i] =L->pElem[i-1];
}
L->pElem[0] = data;
L->nLength++;
}
void InsertListLast(struct List *L,ElemType data)
{
if ( L->nLength == L->nSize )
AgainMalloc(L);
L->pElem[L->nLength] = data;
L->nLength++;
}
int DelFirstElemByValue(struct List* L,ElemType data)
{
int i;
for (i=0; i<L->nLength; i++)
{
if ( L->pElem[i] == data )
{
int j;
for ( j=i; j<L->nLength-1; j++)
{
L->pElem[j] = L->pElem[j+1];
}
L->nLength--;
break;
}
}
if ( i == L->nLength )
{
return 0;
}
else
{
return 1;
}
}
ElemType DelElemByPos(struct List* L,int nPos)
{
int i;
ElemType j;
if ( nPos<1 || nPos>L->nLength )
{
exit(1);
}
j = L->pElem[nPos-1];
for (i=nPos; i<L->nLength; i++)
{
L->pElem[i-1] = L->pElem[i];
}
L->nLength--;
return j;
}
ElemType DelLastElem(struct List* L)
{
ElemType temp;
if ( L->nLength <= 0 )
exit(1);
temp = L->pElem[L->nLength-1];
L->nLength--;
return temp;
}
ElemType DelFirstElem(struct List* L)
{
int i;
ElemType temp;
if ( L->nLength <= 0 )
exit(1);
temp = L->pElem[0];
for ( i=0; i<L->nLength-1; i++ )
{
L->pElem[i] = L->pElem[i+1];
}
L->nLength--;
return temp;
}
void InsertElemByOrder(struct List* L,ElemType data)
{
int i;
if ( L->nLength == L->nSize )
AgainMalloc(L);
for ( i=0; i<L->nLength; i++ )
{
if ( L->pElem[i]<data && L->pElem[i+1]>data )
{
int j;
for ( j=L->nLength; j>i; j-- )
{
L->pElem[j] = L->pElem[j-1];
}
L->pElem[i+1] = data;
L->nLength++;
break;
}
}
}
int InsertElemByPos(struct List* L,int nPos,ElemType data)
{
int i;
if ( nPos<1 || nPos>L->nLength+1 )
{
return 0;
}
if ( L->nLength == L->nSize )
AgainMalloc(L);
for (i=L->nLength; i>=nPos; i--)
{
L->pElem[i] = L->pElem[i-1];
}
L->pElem[nPos-1] = data;
L->nLength++;
return 1;
}
main()
{
ElemType elem;
struct List L;
int nLength = -1;
system("cls");
InitList(&L,3);
InsertElemByPos(&L,1,5);
InsertListFirst(&L,10);
InsertListLast(&L,4);
TraverseList(&L);
nLength = ListLength(&L);
printf("Length:%d/n",nLength);
InsertListFirst(&L,1);
TraverseList(&L);
GetElemByPos(&L,3,&elem);
printf("Elem:%d/n",elem);
nLength = FindElemPos(&L,10);
printf("POS:%d/n",nLength);
UpdateList(&L,2,21);
TraverseList(&L);
InsertListFirst(&L,11);
InsertElemByPos(&L,2,5);
TraverseList(&L);
DelFirstElemByValue(&L,5);
DelLastElem(&L);
DelFirstElem(&L);
TraverseList(&L);
DelElemByPos(&L,2);
TraverseList(&L);
InsertListFirst(&L,111);
InsertListLast(&L,100);
InsertElemByPos(&L,1,110);
TraverseList(&L);
DelElemByPos(&L,1);
TraverseList(&L);
getch();
}