#include <stdio.h>
#include <stdlib.h>
/*
typedef struct Student {
char* name;
int nID;
}ElemType;
*/
typedef int ElemType;
#define SIZE 50
struct List {
ElemType elem[SIZE];
int nLength;
};
void InitList(struct List *L)
{
L->nLength = 0;
}
void DelListElem(struct List *L)
{
L->nLength = 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->elem[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->elem[nPos-1];
}
*/
void TraverseList(struct List *L)
{
int i;
for ( i= 0; i<L->nLength; i++ )
{
printf("%d/n",L->elem[i]);
}
printf("/n");
}
int FindElemInList(struct List *L,ElemType data)
{
int i;
for ( i=0; i<L->nLength; i++ )
{
if ( L->elem[i] == data )
return i+1;
}
return -1;
}
int UpdateListElem(struct List *L,int nPos,ElemType data)
{
if ( nPos<1 || nPos >L->nLength )
return 0;
L->elem[nPos-1] = data;
return 1;
}
/*
void InsertListFirst(struct List *L,ElemType data)
{
int i;
if ( L->nLength == SIZE )
return ;
for ( i=L->nLength;i>0;i-- )
{
L->elem[i] =L->elem[i-1];
}
L->elem[0] = data;
L->nLength++;
}
*/
void InsertListFirst(struct List *L,ElemType data)
{
int i;
int a;
if ( L->nLength == SIZE )
{
printf("The list is full,continue insert?");
scanf("%d",&a);
if ( a == 0 )
return ;
for ( i=L->nLength-1;i>0;i-- )
{
L->elem[i] =L->elem[i-1];
}
L->elem[0] = data;
/* L->nLength++; */
}
for ( i=L->nLength;i>0;i-- )
{
L->elem[i] =L->elem[i-1];
}
L->elem[0] = data;
L->nLength++;
}
void InsertListLast(struct List *L,ElemType data)
{
if ( L->nLength == SIZE )
return ;
L->elem[L->nLength] = data;
L->nLength++;
}
int DelFirstElemByValue(struct List* L,ElemType data)
{
int i;
for (i=0; i<L->nLength; i++)
{
if ( L->elem[i] == data )
{
int j;
for ( j=i; j<L->nLength-1; j++)
{
L->elem[j] = L->elem[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->elem[nPos];
for (i=nPos; i<L->nLength; i++)
{
L->elem[i-1] = L->elem[i];
}
L->nLength--;
return j;
}
ElemType DelLastElem(struct List* L)
{
ElemType temp;
if ( L->nLength <= 0 )
exit(1);
temp = L->elem[L->nLength-1];
L->nLength--;
return temp;
}
ElemType DelFirstElem(struct List* L)
{
int i;
ElemType temp;
if ( L->nLength <= 0 )
exit(1);
temp = L->elem[0];
for ( i=0; i<L->nLength-1; i++ )
{
L->elem[i] = L->elem[i+1];
}
L->nLength--;
return temp;
}
void InsertElemByOrder(struct List* L,ElemType data)
{
int i;
if ( L->nLength == SIZE )
return ;
for ( i=0; i<L->nLength; i++ )
{
if ( L->elem[i]<data && L->elem[i+1]>data )
{
int j;
for ( j=L->nLength; j>i; j-- )
{
L->elem[j] = L->elem[j-1];
}
L->elem[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 == SIZE )
return 0;
for (i=L->nLength; i>=nPos; i--)
{
L->elem[i] = L->elem[i-1];
}
L->elem[nPos-1] = data;
L->nLength++;
return 1;
}
main()
{
ElemType elem;
int nLength = -1;
struct List L;
clrscr();
InitList(&L);
nLength = ListLength(&L);
printf("Length:%d/n",nLength);
InsertElemByPos(&L,1,8);
InsertElemByPos(&L,2,5);
InsertListFirst(&L,1);
InsertListLast(&L,10);
nLength = ListLength(&L);
printf("Length:%d/n",nLength);
TraverseList(&L);
GetElemByPos(&L,2,&elem);
printf("%d/n",elem);
nLength = FindElemInList(&L,5);
printf("%d/n",nLength);
UpdateListElem(&L,3,7);
TraverseList(&L);
InsertElemByPos(&L,3,7);
TraverseList(&L);
DelFirstElemByValue(&L,7);
TraverseList(&L);
InsertElemByPos(&L,4,12);
TraverseList(&L);
DelElemByPos(&L,2);
TraverseList(&L);
DelLastElem(&L);
TraverseList(&L);
DelFirstElem(&L);
TraverseList(&L);
DelListElem(&L);
TraverseList(&L);
getch();
}
1.线性表删除一个元素只是将线性表的长度减1,并没有真正把分配的内存销毁,内存中的值也
没有改变。
2.线性表的的两种实现中销毁线性表的差别最大,一种可以真正释放内存空间,一种只是将长度
置零。
3.错误一般是因为for循环的起始值和结束值没有设置正确或循环体内的赋值语句的数组标号不
正确。