数据结构上机答案
1.1顺序线性表的基本操作
#include<stdio.h> #include
#define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 #define ElemType int
typedef struct { int *elem,length,listsize; }SqList;
int InitList_Sq(SqList &L) { L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType)); L.length=0;
?L.listsize=LIST_INIT_SIZE; ?return OK; }
int Load_Sq(SqList &L) { ?int i; if(L.length==0) ?printf(\The List is empty!\?else ?{
? printf("The List is:\; ?for(i=0;i<L.length;i++) ??printf(\d",L.elem[i]); } printf(\"); ?return OK; }
int ListInsert_Sq(SqList &L,int i,int e) { if(i<1||i>L.length+1) return ERROR; ElemType *newbase,*q,*p; if(L.length>=L.listsize)
{ ?newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType)); ??L.elem=newbase;
? L.listsize+=LISTINCREMENT; }
?q=&(L.elem[i-1]);
?for(p=&(L.elem[L.length-1]);p>=q;--p) *(p+1)=*p; ?*q=e; ++L.length; return OK; }
int ListDelete_Sq(SqList &L,int i,int &e) {
?ElemType *q,*p; ?if(i<1||i>L.length) ?return ERROR; ?p=&(L.elem[i-1]); e=*p; q=L.elem+L.length-1; for(++p;p<=q;p++) ? *(p-1)=*p; L.length--; return OK; }
int main() { SqList T; ?int a,i;
?ElemType e,x; if(InitList_Sq(T)) { printf(\Sequence List Has Created.\\n\); } while(1) { printf(\1:Insert element\\n2:Delete element\n3:Load all elements\n0:Exit\\nPlease choose:\\n\); ??scanf("%d\); ?switch(a) ?{