logo资料库

华农数据结构实验答案.docx

第1页 / 共95页
第2页 / 共95页
第3页 / 共95页
第4页 / 共95页
第5页 / 共95页
第6页 / 共95页
第7页 / 共95页
第8页 / 共95页
资料共95页,剩余部分请下载后查看
数据结构上机答案 1.1 顺序线性表的基本操作 #include #include #define OK 1 #define ERROR 0 #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) else { printf("The List is empty!"); printf("The List is:"); for(i=0;iL.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("A 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",&a); switch(a) { case 1: scanf("%d%d",&i,&x);
if(!ListInsert_Sq(T,i,x)) printf("Insert Error!\n"); else printf("The Element %d is Successfully Inserted!\n",x); break; case 2: scanf("%d",&i); if(!ListDelete_Sq(T,i,e)) printf("Delete Error!\n"); else printf("The Element %d is Successfully Deleted!\n",e); break; case 3: Load_Sq(T); break; case 0: return 1; } } } 1.2 合并顺序表 #include #include #define OK 1 #define ERROR 0 #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; for(i=0;i
printf("\n"); return OK; } int ListLength(SqList L) { return L.length; } int GetElem(SqList L,int i,ElemType &e) { e=L.elem[i-1]; return OK; } int ListInsert_Sq(SqList &L,int i,int e) { if(i<1||i>L.length+1) return ERROR; ElemType *p,*q,*newbase; if(L.listsize<=L.length) { 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; } void MergeList(SqList La,SqList Lb,SqList &Lc) { int i,j,k,La_len,Lb_len,ai,bj; i=j=1; k=0; InitList_Sq(Lc); La_len=ListLength(La); Lb_len=ListLength(Lb); while((i<=La_len)&&(j<=Lb_len)) {
GetElem(La,i,ai); GetElem(Lb,j,bj); if(ai<=bj) { ListInsert_Sq(Lc,++k,ai); i++; } else { } ListInsert_Sq(Lc,++k,bj); j++; } while(i<=La_len) { GetElem(La,i++,ai); ListInsert_Sq(Lc,++k,ai); } while(j<=Lb_len) { GetElem(Lb,j++,bj); ListInsert_Sq(Lc,++k,bj); } Load_Sq(Lc); } int main() { int an,bn,i,e; SqList La,Lb,Lc; InitList_Sq(La); scanf("%d",&an); for(i=1;i<=an;i++) { scanf("%d",&e); ListInsert_Sq(La,i,e); } printf("List A:"); Load_Sq(La); InitList_Sq(Lb); scanf("%d",&bn); for(i=1;i<=an;i++) { scanf("%d",&e);
ListInsert_Sq(Lb,i,e); } printf("List B:"); Load_Sq(Lb); printf("List C:"); MergeList(La,Lb,Lc); return 0; } 1.3 顺序表逆置 #include #include #define OK 1 #define ERROR 0 #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)); if(!L.elem) { printf("NO1"); return ERROR; } L.length=0; L.listsize=LIST_INIT_SIZE; return OK; } int Load_Sq(SqList &L) { int i; if(!L.length) { printf("This List is empty!\n"); return ERROR; }
else { for(i=0;i=L.listsize) { newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType)); if(!newbase) { printf("NO2"); return ERROR; } 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 swap(SqList &L,int n) { int i,j,temp; for(i=0,j=n-1;j>i;i++,j--) { temp=L.elem[i]; L.elem[i]=L.elem[j]; L.elem[j]=temp; } return OK; } int main() {
SqList T; int n,i; ElemType x; scanf("%d",&n); InitList_Sq(T); for(i=1;i #include #define ERROR 0 #define OK 1 #define ElemType int typedef struct LNode { int data; struct LNode *next; }LNode,*LinkList; int CreateLink_L(LinkList &L,int n) { LinkList p,q; int i; ElemType e; L=(LinkList)malloc(sizeof(LNode)); L->next=NULL; q=(LinkList)malloc(sizeof(LNode)); q=L; for(i=0;i
分享到:
收藏