主题:[讨论]C语言:线性表问题
初学者,线性表习题,程序编出来了,漏洞百出,更关键的是自己找不到,卡在这2天了,有高手又有空的话能不能帮忙详细分析讲解一下我的代码……感激不尽
#include<stdio.h>
#include<stdlib.h>
struct List{ int *data;
int s;
int e;
};
void initList(struct List *L,int s1)
{ if(s1>0){
L->s=s1;
L->e=0;
L->data=(int*)malloc(sizeof((int*)L->s));
}
}
void FreeList(struct List *L){free(L->data);}
Bool ListEmpty(struct List *L){return((L->e<=0)?TRUE:FALSE);}
Bool ListFull(struct List *L){return((L->e==L->s)?TRUE:FALSE);}
int searchElem(struct List *L,int data)
{
int i;
for(i=0;i<L->e;i++)
if(L->data[i]==data)return i;
return -1;
}
Bool InsertElem(struct List *L,int data,int i)
{
int j;
if(i<0||i>L->e-1||L-e==L->s)return FALSE;
else
{
for(j=L->e-1;j>=i;j--)
L->data[j+1]=L->data[i];
L->data[i]=data;
L->e++;
return TRUE;
}
Bool DeleteElem(struct List *L,int i)
{
int j;
if(i<0||i>L->e||L->e==0)return FALSE;
else
{
for(j=i;i<L->e;i++)
L-data[j]=L->data[j+1];
L->e--;
return TRUE;
}
void printout(struct List *L)
{
int i;
for(i=0;i<L->e;i++)
printf("%d",L-data[i]);
printf("\n");
}
void main()
{ struct List *L;
InitList(L,5);
FreeList(L);
printf("The List is empty: ");
printf(ListEmpty(L));
printf("\n");
printf("The List is full: ");
printf(ListFull(L));
printf("\n");
InsertElem(L,5,0);
InsertElem(L,4,0);
InsertElem(L,3,0);
InsertElem(L,2,0);
InsertElem(L,1,0);
printf("The List:\n");
printout(L);
printf("The test of search:\n");
printf("The index of element(3) is %d\n",searchElem(L,3));
printf("The index of element(5) is %d\n",searchElem(L,5));
printf("The test of delete:\n");
DeleteElem(L,0);
printf("Delete the elment of index(0),the list is:\n");
printout(L);
DeleteElem(L,1);
printf("Delete the elment of index(1),the list is:\n");
printout(L);
}
#include<stdio.h>
#include<stdlib.h>
struct List{ int *data;
int s;
int e;
};
void initList(struct List *L,int s1)
{ if(s1>0){
L->s=s1;
L->e=0;
L->data=(int*)malloc(sizeof((int*)L->s));
}
}
void FreeList(struct List *L){free(L->data);}
Bool ListEmpty(struct List *L){return((L->e<=0)?TRUE:FALSE);}
Bool ListFull(struct List *L){return((L->e==L->s)?TRUE:FALSE);}
int searchElem(struct List *L,int data)
{
int i;
for(i=0;i<L->e;i++)
if(L->data[i]==data)return i;
return -1;
}
Bool InsertElem(struct List *L,int data,int i)
{
int j;
if(i<0||i>L->e-1||L-e==L->s)return FALSE;
else
{
for(j=L->e-1;j>=i;j--)
L->data[j+1]=L->data[i];
L->data[i]=data;
L->e++;
return TRUE;
}
Bool DeleteElem(struct List *L,int i)
{
int j;
if(i<0||i>L->e||L->e==0)return FALSE;
else
{
for(j=i;i<L->e;i++)
L-data[j]=L->data[j+1];
L->e--;
return TRUE;
}
void printout(struct List *L)
{
int i;
for(i=0;i<L->e;i++)
printf("%d",L-data[i]);
printf("\n");
}
void main()
{ struct List *L;
InitList(L,5);
FreeList(L);
printf("The List is empty: ");
printf(ListEmpty(L));
printf("\n");
printf("The List is full: ");
printf(ListFull(L));
printf("\n");
InsertElem(L,5,0);
InsertElem(L,4,0);
InsertElem(L,3,0);
InsertElem(L,2,0);
InsertElem(L,1,0);
printf("The List:\n");
printout(L);
printf("The test of search:\n");
printf("The index of element(3) is %d\n",searchElem(L,3));
printf("The index of element(5) is %d\n",searchElem(L,5));
printf("The test of delete:\n");
DeleteElem(L,0);
printf("Delete the elment of index(0),the list is:\n");
printout(L);
DeleteElem(L,1);
printf("Delete the elment of index(1),the list is:\n");
printout(L);
}