# include<stdio.h>
# include<malloc.h>
# include<stdlib.h>

typedef struct SQLIST
{
	int * pBase;
    int size;
	int MAXSIZE;
} L ;

void Init_SqList(L * pList, int maxsize)
{
	pList->pBase=(int *)malloc(sizeof(int)*maxsize);
	if(pList->pBase==NULL)
	{
		printf("内存分配失败!\n");
		exit(-1);
	}
	else
	{
		printf("\n顺序表初始化成功!分配地址为:%p\n",pList->pBase);
		pList->MAXSIZE=maxsize;
		pList->size=0;
	}
}

bool Is_Full(L * pList)
{
	if(pList->size==pList->MAXSIZE)
		return true;
	else
		return false;
}

bool Is_Empty(L * pList)
{
	if(pList->size==0)
		return true;
	else
		return false;
}

int Append_SqList(L * pList, int x)
{
	if( Is_Full(pList) )
		return false;
	else
		pList->pBase[pList->size]= x;
	    pList->size++;
}

bool Search_SqList(L * pList, int x, int * pos)
{

	int i=0;
	while(i<pList->size)
		if(pList->pBase[i]==x)
		{
			* pos=i+1;
			return true;
		}
		else
			i++;
		return false;

}

bool Insert_SqList(L * pList, int x, int pos)
{
	if( Is_Full(pList) || pos<1 || pos>pList->size+1)
		return false;
	else
		for(int i= pList->size-1; i>=pos-1; i--)
        	pList->pBase[i+1]=pList->pBase[i];
		pList->pBase[pos-1]=x;
		pList->size++;
		return true;
}

bool Delete_SqList(L * pList, int pos, int * pe)
{
	if( Is_Empty(pList) || pos<1 || pos>pList->size+1)
		return false;
	else
		* pe=pList->pBase[pos-1];
		for(int i=pos-1; i<pList->size-1; i++)
			pList->pBase[i]=pList->pBase[i+1];
		pList->size--;
		return true;
}

bool Show_SqList(L * pList)
{
	if(pList->size==0)
	{
		printf("\n顺序表为空!");
		return false;
	}
	else
	{
		for(int i=0; i<pList->size; i++)
			printf("%d ", pList->pBase[i]);
		printf("\n");
		return true;
	}
}

int main()
{
	L list;
	int ms;
	int s=list.size;
	int n;
	int e;
	int p;
printf("      <网工1101 朱旭宏 2011011317 >\n************顺序表的相关操作************\n");
    printf("\n输入顺序表长度:");
	scanf("%d",&ms);
	Init_SqList(& list, ms);//初始化创建顺序表list

	printf("\n输入顺序表元素个数:");
	scanf("%d",&s);

    printf("\n输入顺序表元素:");
	for(int i=0; i<s; i++)
		{
          scanf("%d", &list.pBase[i]);
          Append_SqList(&list, list.pBase[i]);
		}
    printf("\n初始建立顺序表为:");
	Show_SqList(& list);
	 	
			 printf("\n输入要查找的元素:");
			 scanf("%d", &e);
				 if(Search_SqList(&list, e ,&p))
					 printf("\n找到 %d在第%d个位置\n", e, p);
				 else
					 printf("\n没有找到%d!\n", e);

			 printf("\n输入要插入的元素和位置:");
			 scanf("%d %d", &e, &p);
			 if(Insert_SqList(&list, e, p))
			 {
				 printf("\n插入%d后顺序表为:", e);
			     Show_SqList(& list);
			 }
			 else
				 printf("\n插入失败!\n");
		     
			 printf("\n输入要删除元素的位置:");
			 scanf("%d", &p);
			 if( Delete_SqList(&list, p, &e) )
			 {
				 printf("\n删除%d后顺序表为:", e);
				 Show_SqList(& list);
			 }
			 else
				 printf("\n删除失败!");
printf("\n********操作结束,欢迎体验!************\n");
	return 0;
}