#include"stdio.h"#defineMAXSIZE30typedefint Type;typedefstruct{
Type elem[MAXSIZE];
Type last;}Lnode,*List;voidInitList(List L){
L->last=0;}
Type ListInsert(List L,int i,int e){int j;if(L->last>30)return-1;if(i<0||i>L->last+1)return-2;for(j=L->last; j >= i;--j)
L->elem[j]=L->elem[j-1];
L->elem[i-1]=e;
L->last++;return1;}
Type ListPrint(List L){int i;
i=ListEmpty(L);if(i==-1){printf("the table is empty\n");return1;}printf("Elements in the table\n");for(i =0; i < L->last;++i)printf("%d->", L->elem[i]);printf("\n");return1;}
Type ListEmpty(List L){if(L->last==0)return-1;}
Type ListDelete(List L,int i){int temp;
temp=ListEmpty(L);if(temp==-1)return-1;if(i<0||i>L->last)return-2;for(; i <L->last ;++i)
L->elem[i-1]=L->elem[i];
L->last--;}
Type LocateElem(List L,int e){int i=0;while(i<=L->last&&L->elem[i]!=e)
i++;if(i>L->last)return-1;elsereturn i+1;}voidListLenth(List L){printf("Table length is%d\n", L->last);}
Type GetElem(List L,int i){int temp;
temp=ListEmpty(L);if(temp==-1)return-2;if(i<0||i>L->last)return-1;return L->elem[i-1];}
Type ClearList(List L){if(L->last==0)printf("Table was empty\n");
L->last=0;}
Type PriorElem(List L,int cur_e){int temp;
temp=ListEmpty(L);if(temp==-1)return-2;if(L->elem[0]==cur_e)return-1;
temp=LocateElem(L,cur_e);if(temp==-1)return-3;return temp-2;}
Type NextElem(List L,int cur_e){int temp;
temp=ListEmpty(L);if(temp==-1)return-2;if(L->elem[L->last-1]==cur_e)return-1;
temp=LocateElem(L,cur_e);if(temp==-1)return-3;return temp;}intmain(){int n,value,temp,loc,i=1,k=1;
Lnode L;InitList(&L);printf("This code is the static allocation method of sequence table\n");printf("Please enter the sequence table you want to create (-1 ends,Space separated)\n");scanf("%d",&n);while(n!=-1){ListInsert(&L,i,n);
i++;scanf("%d",&n);}while(k!=-1){printf("Please select an action\n");printf("1.Show all elements\n");printf("2.Insert an element\n");printf("3.Delete an element\n");printf("4.Find an element (by value)\n");printf("5.Find an element (by location)\n");printf("6.Length of table\n");printf("7.Clear table\n");printf("8.Find Prev\n");printf("9.Find next\n");printf("10.exit\n");scanf("%d",&n);switch(n){case1:ListPrint(&L);break;case2:{printf("Please enter the location to insert and the element to insert\n");printf("Format: position element; Example: 3 10\n");scanf("%d %d",&loc,&value);
temp=ListInsert(&L,loc,value);if(temp==-1)printf("Insert failed,Table full\n");elseif(temp==-2)printf("Insert failed,Illegal insertion\n");else{printf("Insert successful\n");ListPrint(&L);}break;}case3:{printf("Please enter the location to delete\n");scanf("%d",&loc);
temp=ListDelete(&L,loc);if(temp==-1)printf("Deletion failed, the table is empty\n");elseif(temp==-2)printf("Deletion failed,Illegal deletion\n");else{printf("Deletion successful\n");ListPrint(&L);}break;}case4:{printf("Please enter the element to find\n");scanf("%d",&value);
loc=LocateElem(&L,value);if(loc==-1)printf("Find failed,The %d is not in the table\n",value);else{printf("Find successful\n");printf("The position of the element is:%d\n",loc);}break;}case5:{printf("Please enter the location you want to find\n");scanf("%d",&loc);
value=GetElem(&L,loc);if(value==-1)printf("Find failed,Illegal search\n");elseif(value==-2)printf("Find failed,the table is empty\n");else{printf("Find successful\n");printf("The element of the location to find is %d\n",value);}break;}case6:ListLenth(&L);break;case7:ClearList(&L);break;case8:{printf("The precursor of which element do you want to find\n");scanf("%d",&value);
temp=PriorElem(&L,value);if(temp==-1)printf("Find failed,The current element has no precursor because it is the first\n");elseif(temp==-2)printf("Find failed,the table is empty\n");elseif(temp==-3)printf("Find failed,The %d is not in the table\n",value);elseprintf("%d\n",L.elem[temp]);break;}case9:{printf("Which element do you want to find the successor of\n");scanf("%d",&value);
temp=NextElem(&L,value);if(temp==-1)printf("Find failed,The current element has no precursor because it is the last\n");elseif(temp==-2)printf("Find failed,the table is empty\n");elseif(temp==-3)printf("Find failed,The %d is not in the table\n",value);elseprintf("%d\n",L.elem[temp]);break;}default:k=-1;break;}}return0;}