(C 언어) 선형 표 순서 로 저 장 된 첨삭 소스 코드
19692 단어 데이터 구조
#include
#include
/*
List MakeEmpty();//
ElementType FindKth(int K,List L);// K ,
int Find(ElementType X,List L);//
void Insert(ElementType X,int i,List L); i X
void Delete(int i,List L);// i
int Length(List L);//
*/
#define MAXSIZE 100
#define TRUE 1
#define FALSE -1
typedef struct LNode *List;
typedef int ElementType;
struct LNode{
ElementType Data[MAXSIZE];
int Last;
};
struct LNode L;
List MakeEmpty(){
List PtrL;
PtrL = (List)malloc(sizeof(struct LNode));
PtrL->Last = -1;// -1
return PtrL;
}
int Find(ElementType X,List PtrL){
int i = 0;
while(i <= PtrL->Last && PtrL->Data[i]!=X)
i++;
if(i>PtrL->Last) return FALSE;
else return i+1;
}
void Insert(ElementType X,int i,List PtrL){
int j;
if(PtrL->Last == MAXSIZE-1){
printf(" ");
return ;
}
if(i<1 || i>PtrL->Last+2){
printf(" ");
return ;
}
for (j=PtrL->Last;j>=i-1;j--)
PtrL->Data[j+1] = PtrL->Data[j];
PtrL->Data[i-1]=X;
PtrL->Last++;
return ;
}
void Delete(int i,List PtrL){
int j;
if(i<1||i>PtrL->Last+1){
printf(" %d ",i);
return ;
}
for(j=i;j<=PtrL->Last;j++)
PtrL->Data[j-1] = PtrL->Data[j];
PtrL->Last--;
return ;
}
ElementType FindKth(int K,List PtrL)// K ,
{
if(K<1 || K>PtrL->Last+1){
printf(" %d ",K);
return FALSE;
}
return PtrL->Data[K+1];
}
void PrintL(List PtrL){
for(int i = 0;i<=PtrL->Last;i++){
printf(" %d",PtrL->Data[i]);
}
printf("
");
}
int Length(List PtrL)//
{
return PtrL->Last;
}
void Alter(ElementType X,int i,List PtrL){
// i
if(i<1||i>PtrL->Last+1){
printf(" %d ",i);
return ;
}
PtrL->Data[i]=X;
}
int main()
{
List L = MakeEmpty();
Insert(1,1,L);
Insert(2,2,L);
Insert(3,3,L);
Insert(4,4,L);
Insert(5,5,L);
Insert(6,6,L); //
PrintL(L); //
Insert(100,3,L);
PrintL(L);
Delete(2,L);//
PrintL(L);
int m = Find(100,L);//
if(m!=-1){
printf("The number was found!
");
}
Alter(200,2,L);
PrintL(L);
return 0;
}