1、/*按顺序存储结构代码如下:*/#include#include#define MAXSIZE 1024#define TRUE 1#define FALSE -1/定义元素为整形typedef int ElemType;typedef structElemType dataMAXSIZE;int len;SeqList1,*SeqList;SeqList SeqListInit( SeqList L) /顺序表初始化/构造一个空链表L-len=0;return L; int SeqListFull(SeqList L) /判断是否表满,满则返回 TRUE if(L-len=MAXSIZE)
2、printf(“full listn“);return TRUE;elsereturn FALSE;void SeqListInsert(SeqList L,int i,ElemType x) /顺序表插入函数/顺序表中的第 i 个位置上插入元素 xint j;if(SeqListFull(L)=1) exit (0);if(iL-len+1) printf(“wrong positionn“);exit(0);for(j=L-len-1;j=i-1;j-)L-dataj+1=L-dataj;L-datai-1=x;L-len+;int SeqListAdd(SeqList L) /顺序表在第
3、一个位置添加元素int m;printf(“Input the new elementn“);scanf(“%d“, SeqListInsert(L,1,m);return TRUE;int SeqListEmpty(SeqList L) /判断顺序表是否为空,空则返回 TRUEif(L-len=0) printf(“List NULLn“);return TRUE;else return FALSE;void SeqListTraverse(SeqList L) /打印所有元素int i;if(SeqListEmpty(L)=1)exit(0);elsefor(i=1;ilen;i+)pri
4、ntf(“%dn“,L-datai-1);void SeqListOrder(SeqList L) /将元素按由小到大排列int i,j,l;for(i=1;ilen;i+)for(j=i+1;jlen;j+)if(L-datai-1L-dataj-1)l=L-datai-1;L-datai-1=L-dataj-1;L-dataj-1=l; SeqListTraverse(L);int SeqListPosi(SeqList L,int i)/判断元素位置是否正确,不正确则返回 FALSEif(iL-len)printf(“wrong Positionn“);return FALSE;else
5、 return TRUE;void SeqListDelete(SeqList L,int i)/删除函数/删除第 i 个位置上的元素int j;if(SeqListPosi(L,i)=-1)exit(0);for(j=i;jlen;j+)L-dataj-1=L-dataj; L-len-;int SeqListLocate(SeqList L,ElemType x) /查找 x/在顺序表中查找元素 x 并返回 x 的位置int i=1;while(ilenif(ilen) return i;else return FALSE;int SeqListGet(SeqList L,int i) /
6、返回第 i 个位置上的元素int j;if(SeqListPosi(L,i)=-1)exit(0);return L-datai-1;SeqList SeqListClear(SeqList L) /清空顺序表 L-len=0;return L;int SeqListLength(SeqList L) /求顺序表的长度return L-len;int SeqListPrior(SeqList L,int x)/求 x 的前驱int i;i=SeqListLocate(L,x);if(i=-1)printf(“There is no x in SeqListLocaten“);elseif(i=
7、1)printf(“The first element hasnt priorn“);elsereturn L-datai-2; int SeqListNext(SeqList L,int x)/求 x 的后继int i;i=SeqListLocate(L,x);if(i=-1)printf(“There is no x in SeqListLocaten“);elseif(i=L-len)printf(“The last element hasnt nextn“);elsereturn L-datai;void Window()printf(“nType 1 to Add new node“
8、);printf(“nType 2 to Insert one“);printf(“nType 3 to Print All“);printf(“nType 4 to delete one whose position is i“);printf(“nType 5 to put element in order“);printf(“nType 6 to Get one by i“);printf(“nType 7 to Get ones position“);printf(“nType 8 to Get ones Prior“);printf(“nType 9 to Get ones Next
9、“);printf(“nType 0 to Clear List“);printf(“nType 10 to get lists Length“);printf(“nType 11 to break“);printf(“nType yout choice:“);void MainWindow(SeqList L)int a=12;int x,i;while(a!=11)printf(“n“);Window();scanf(“%d“,switch(a)case 1:SeqListAdd(L);break;case 2:printf(“Input the element you want and
10、its position:n“);scanf(“%d%d“,SeqListInsert(L,i,x);break;case 3:SeqListTraverse(L);break;case 4:printf(“Type the position you want to delete:n“);scanf(“%d“,SeqListDelete(L,i);break;case 5:SeqListOrder(L);break;case 6:printf(“Type the position you want to get:n“);scanf(“%d“,printf(“n%d“,SeqListGet(L,
11、i);break;case 7:printf(“nType the element:n“);scanf(“%d“,printf(“n%d“,SeqListLocate(L,x);break;case 8:printf(“nType the element:n“);scanf(“%d“,printf(“n%d“,SeqListPrior(L,x);break;case 9:printf(“nType the element:“);scanf(“%d“,printf(“n%d“,SeqListNext(L,x);break;case 0:SeqListClear(L);break;case 10:printf(“The length is %d“,SeqListLength(L);break; default:break;