//for(int i = LIST_INIT_SIZE-1; i >= N ;i--)//因为C语言不能这样在函数内部声明,只能再函数头部声明,但是C++支持这种,你可以将文件后缀改成.cpp再编译就没问题了
//望采纳
#include
#include
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct{
int *elem ;
int length ;
int listsize ;
}SqList ;
void InitList_Sq(SqList *L)
{
L->elem = (int *)malloc(LIST_INIT_SIZE *sizeof(int)) ;
if(!L->elem)
{
exit(-1) ;
}
L->length = 0 ;
L->listsize = LIST_INIT_SIZE ;
printf("InitList Succeed!\n") ;
}
void ListInsert_Sq(SqList *L, int N)
{
printf("Please input the NO.%d element:", N) ;
for(int i = LIST_INIT_SIZE-1; i >= N ;i--)//因为C语言不能这样在函数内部声明,只能再函数头部声明,但是C++支持这种,你可以将文件后缀改成.cpp再编译就没问题了
{
L->elem[i] =L->elem[i-1] ;
}
scanf("%d", &L->elem[N-1]) ;
}
void ListDelete_Sq(SqList * L, int N)
{
printf("The element you want to delete is: %d\n", N) ;
for(int i = 0; i < LIST_INIT_SIZE; i++)
{
if(L->elem[i] == N)
{
for(int j = i; j < LIST_INIT_SIZE-1; j++)
{
L->elem[j] = L->elem[j+1] ;
}
return ;
}
}
}
void LocateElem(SqList *L, int N)
{
int flag = 1 ;
for(int i = 0; i < LIST_INIT_SIZE; i++)
{
if(L->elem[i] == N)
{
printf("The element :%d is at the NO.%d position!\n", N, i+1) ;
flag = 0 ;
}
}
if(flag)
{
printf("The element :%d isn't in this list!\n", N) ;
}
}
void printf_Sq(SqList *L, int N)
{
printf("Now the List is:") ;
for(int i = 0; i{
if(i)
printf(" ") ;
printf("%d", L->elem[i]) ;
}
printf("\n\n") ;
}
int main()
{
SqList *L ;
InitList_Sq(L) ;
int n = 5 ;
for(int i = 1; i <= n; i++)
{
ListInsert_Sq(L, i) ;
}
printf("Insert Finished!\n") ;
printf_Sq(L, n) ;
int del = 6 ;
ListDelete_Sq(L, del) ;
printf_Sq(L, n-1) ;
int pos = 2 ;
ListInsert_Sq(L, pos) ;
printf_Sq(L, n) ;
int judge = 8 ;
LocateElem(L, judge) ;
printf_Sq(L, n) ;
return 0 ;
}