数据结构上机实验答案
华农数据结构上机实验答案

华农数据结构上机实验答案数据结构上机答案1.1顺序线性表的基本操作#include<stdio.h>#include<malloc.h>#define OK 1#define ERROR 0#define LIST_INIT_SIZE 100#define LISTINCREMENT 10#define ElemType inttypedef 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("\n");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)*size of(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");elseprintf("The Element %d is Successfully Inserted!\n",x);break;case 2: scanf("%d",&i);if(!ListDelete_Sq(T,i,e))printf("Delete Error!\n");elseprintf("The Element %d is Successfully Deleted!\n",e);break;case 3: Load_Sq(T);break;case 0: return 1;}}}1.2合并顺序表#include<stdio.h>#include<malloc.h>#define OK 1#define ERROR 0#define LIST_INIT_SIZE 100#define LISTINCREMENT 10#define ElemType inttypedef 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<L.length;i++)printf("%d ",L.elem[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)*size of(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<stdio.h>#include<malloc.h>#define OK 1#define ERROR 0#define LIST_INIT_SIZE 100#define LISTINCREMENT 10#define ElemType inttypedef 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.length;i++)printf("%d ",L.elem[i]);}printf("\n");return OK;}int ListInsert_Sq(SqList &L,int i,int e){ElemType *newbase,*p,*q;if(L.length>=L.listsize){newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*size of(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<n+1;i++){scanf("%d",&x);ListInsert_Sq(T,i,x);}printf("The List is:");Load_Sq(T);swap(T,n);printf("The turned List is:");Load_Sq(T);return 0;}1.4链式线性表的基本操作#include<stdio.h>#include<malloc.h>#define ERROR 0#define OK 1#define ElemType inttypedef 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<n;i++){scanf("%d",&e);p=(LinkList)malloc(sizeof(LNode));p->data=e;p->next=q->next;q->next=p;q=q->next;}return OK;}int LoadLink_L(LinkList &L){LinkList p=L->next;if(!p)printf("The List is empty!");else{printf("The LinkList is:");while(p){printf("%d ",p->data);p=p->next;}}printf("\n");return OK;}int LinkInsert_L(LinkList &L,int i,ElemType e) {LNode *p=L,*s;int j=0;while(p&&j<i-1){p=p->next;j++;}if(!p||j>i-1)return ERROR;s=(LinkList)malloc(sizeof(LNode));s->data=e;s->next=p->next;p->next=s;return OK;}int LinkDelete_L(LinkList &L,int i,ElemType &e){LNode *p=L,*q;int j=0;while(p->next&&j<i-1){p=p->next;j++;}if(!(p->next)||j<i-1)return ERROR;q=p->next;p->next=q->next;e=q->data;free(q);return OK;}int main(){LinkList T;int a,n,i;ElemType x,e;printf("Please input the init size of the linklist:\n");scanf("%d",&n);printf("Please input the %d element of the linklist:\n",n);if(CreateLink_L(T,n)){printf("A Link List Has Created.\n");LoadLink_L(T);}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(!LinkInsert_L(T,i,x))printf("Insert Error!\n");elseprintf("The Element %d is Successfully Inserted!\n",x);break;case 2:scanf("%d",&i);if(!LinkDelete_L(T,i,e))printf("Delete Error!\n");elseprintf("The Element %d is Successfully Deleted!\n",e);break;case 3:LoadLink_L(T);break;case 0:return 1;}}}1.5合并链表#include<stdio.h>#include<malloc.h>#define ERROR 0#define OK 1#define ElemType inttypedef 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<n;i++){scanf("%d",&e);p=(LinkList)malloc(sizeof(LNode));p->data=e;p->next=q->next;q->next=p;q=q->next;}return OK;}int LoadLink_L(LinkList &L){LinkList p=L->next;if(!p)printf("The List is empty!");else{while(p){printf("%d ",p->data);p=p->next;}}printf("\n");return OK;}void MergeList_L(LinkList &La,LinkList &Lb,LinkList &Lc) {LinkList pa,pb,pc;pa=La->next;pb=Lb->next;Lc=pc=La;while(pa&&pb){if(pa->data<=pb->data){pc->next=pa;pc=pa;pa=pa->next;}else{pc->next=pb;pc=pb;pb=pb->next;}}pc->next=pa?pa:pb;free(Lb);}int main(){LinkList La,Lb,Lc;int n;scanf("%d",&n);CreateLink_L(La,n);printf("List A:");LoadLink_L(La);scanf("%d",&n);CreateLink_L(Lb,n);printf("List B:");LoadLink_L(Lb);MergeList_L(La,Lb,Lc);printf("List C:");LoadLink_L(Lc);return 0;}1.6线性链表逆置#include<stdio.h>#include<malloc.h>#define OK 1#define ERROR 0#define ElemType inttypedef 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<n;i++){scanf("%d",&e);p=(LinkList)malloc(sizeof(LNode));p->data=e;p->next=q->next;q->next=p;q=q->next;}return OK;}int LoadLink_L(LinkList &L){LinkList p=L->next;if(!p)printf("The List is Empty!");elsewhile(p){printf("%d ",p->data);p=p->next;}printf("\n");return OK;}int inversion(LinkList &L){LinkList p=L->next,q;L->next=NULL;while(p){q=p->next;p->next=L->next;L->next=p;p=q;}return OK;}int main(){LinkList T;int n;scanf("%d",&n);CreateLink_L(T,n);printf("The List is:");LoadLink_L(T);inversion(T);printf("The turned List is:");LoadLink_L(T);return 0;}2.1顺序栈的基本操作#include<stdio.h>#include<malloc.h>#include<stdlib.h>#define OK 1#define ERROR 0#define STACK_INIT_SIZE 100#define STACKINCREMENT 10typedef int SElemType;typedef int Status;struct SqStack{SElemType *base;SElemType *top;int stacksize;};Status InitStack(SqStack &S){S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));if(!S.base)return ERROR;S.top=S.base;S.stacksize=STACK_INIT_SIZE;return OK;}Status Push(SqStack &S,SElemType e){if(S.top-S.base>=S.stacksize){S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*si zeof(SElemType));if(S.base)return ERROR;S.top=S.base+S.stacksize;S.stacksize+=STACKINCREMENT;}*S.top++=e;return OK;}Status Pop(SqStack &S,SElemType &e){if(S.top==S.base)return ERROR;e=*--S.top;return OK;}Status GetTop(SqStack S,SElemType &e){if(S.top==S.base)return ERROR;e=*(S.top-1);return OK;}int StackLength(SqStack S){int i=0;while(S.top!=S.base){i++;S.top--;}return i;}Status StackTraverse(SqStack S){SElemType *p=(SElemType*)malloc(sizeof(SElemType));p=S.top;if(S.top==S.base)printf("The Stack is Empty!");else{printf("The Stack is:");p--;S.base--;while(p!=S.base){printf("% d",*p);p--;}}printf("\n");return OK;}int main(){int a;SqStack S;SElemType x,e;if(InitStack(S))printf("A Stack Has Created.\n");while(1){printf("1:Push\n2:Pop\n3:Get the Top\n4:Return the Length of the Stack\n5:Load the Stack\n0:Exit\nPlease choose:\n");scanf("%d",&a);switch(a){case 1:scanf("%d",&x);if(!Push(S,x))printf("Push Error!\n");elseprintf("The Element %d is Successfully Pushed!\n",x);break;case 2:if(!Pop(S,e))printf("Pop Error!\n");elseprintf("The Element %d is Successfully Poped!\n",e);break;case 3:if(!GetTop(S,e))printf("GetTop Error!\n");elseprintf("The Top Element is %d!\n",e);break;case 4:printf("The Length of the Stack is %d!\n",StackLength(S));break;case 5:StackTraverse(S);break;case 0:return 1;}}}2.2循环队列的基本操作#include<stdio.h>#include<malloc.h>#define OK 1#define ERROR 0typedef int Status;typedef int QElemType;#define MAXQSIZE 100typedef struct{QElemType *base;int front;int rear;}SqQueue;Status InitQueue(SqQueue &Q){Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));if(!Q.base)return ERROR;Q.front=Q.rear=0;return OK;}Status EnQueue(SqQueue &Q,QElemType e){if((Q.rear+1)%MAXQSIZE==Q.front)return ERROR;Q.base[Q.rear]=e;Q.rear=(Q.rear+1)%MAXQSIZE;return OK;}Status DeQueue(SqQueue &Q,QElemType &e){if(Q.front==Q.rear)return ERROR;e=Q.base[Q.front];Q.front=(Q.front+1)%MAXQSIZE;return OK;}Status GetHead(SqQueue Q,QElemType &e){if(Q.front==Q.rear)return ERROR;e=Q.base[Q.front];return OK;}int QueueLength(SqQueue Q){return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE; }Status QueueTraverse(SqQueue Q){int i;i=Q.front;if(Q.front==Q.rear)printf("The Queue is Empty!");else{printf("The Queue is:");while(i!=Q.rear){printf("% d",Q.base[i]);i=i+1;}}printf("\n");return OK;}int main(){int a;SqQueue S;QElemType x,e;if(InitQueue(S))printf("A Queue Has Created.\n");while(1){printf("1:Enter \n2:Delete \n3:Get the Front \n4:Return the Length of the Queue\n5:Load the Queue\n0:Exit\nPlease choose:\n");scanf("%d",&a);switch(a){case 1: scanf("%d",&x);if(!EnQueue(S,x))printf("Enter Error!\n");elseprintf("The Element %d is Successfully Entered!\n",x);break;case 2: if(!DeQueue(S,e))printf("Delete Error!\n");elseprintf("The Element %d is Successfully Deleted!\n",e);break;case 3: if(!GetHead(S,e))printf("Get Head Error!\n");elseprintf("The Head of the Queue is %d!\n",e);break;case 4: printf("The Length of the Queue is %d!\n",QueueLength(S));break;case 5: QueueTraverse(S);break;case 0: return 1;}}}2.3栈的应用——进制转换#include<stdio.h>#include<malloc.h>#define ERROR 0#define OK 1#define STACK_INIT_SIZE 100#define STACKINCREMENT 10typedef int SElemType;typedef int Status;struct SqStack{SElemType *base;SElemType *top;int stacksize;};Status InitStack(SqStack &S){S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));if(!S.base)return ERROR;S.top=S.base;S.stacksize=STACK_INIT_SIZE;return OK;}Status Push(SqStack &S,SElemType e){if(S.top-S.base>=S.stacksize){S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*si zeof(SElemType));if(S.base)return ERROR;S.top=S.base+S.stacksize;S.stacksize+=STACKINCREMENT;}*S.top++=e;return OK;}Status Pop(SqStack &S,SElemType &e){if(S.top==S.base)return ERROR;e=*--S.top;return OK;}Status StackEmpty(SqStack &S){if(S.top==S.base)return 0;elsereturn 1;}int main(){int N,e;SqStack S;InitStack(S);scanf("%d",&N);while(N){Push(S,N%8);N=N/8;}while(StackEmpty(S)){Pop(S,e);printf("%d",e);}return 0;}2.4括号匹配检验typedef char SElemType;#include<malloc.h>#include<stdio.h>#include<math.h>#include<process.h>#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0typedef int Status;#define STACK_INIT_SIZE 10#define STACKINCREMENT 2struct SqStack{SElemType *base;SElemType *top;int stacksize;};Status InitStack(SqStack &S){S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));if(!S.base)return 0;S.top=S.base;S.stacksize=STACK_INIT_SIZE;return OK;}Status StackEmpty(SqStack S){if(S.top==S.base)return TRUE;elsereturn FALSE;}Status Push(SqStack &S,SElemType e){if(S.top-S.base>=S.stacksize){S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*si zeof(SElemType));if(!S.base)return 0;S.top=S.base+S.stacksize;S.stacksize+=STACKINCREMENT;}*S.top++=e;return OK;}Status Pop(SqStack &S,SElemType &e){if(S.top==S.base)return ERROR;e=*--S.top;return OK;}void check(){SqStack s;SElemType ch[80],*p,e;if(InitStack(s)){gets(ch);p=ch;while(*p)switch(*p){case '(':case '[':Push(s,*p++);break;case ')':case ']':if(!StackEmpty(s)){Pop(s,e);if(*p==')'&&e!='('||*p==']'&&e!='[') {printf("isn't matched pairs\n");return ;}else{p++ ;break;}}else{printf("lack of left parenthesis\n");return ;}default: p++;}if(StackEmpty(s))printf("matching\n");elseprintf("lack of right parenthesis\n");}}int main(){check();return 1;}2.5行编辑程序typedef char SElemType;#include<malloc.h>#include<stdio.h>#include<math.h>#include<process.h>#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0typedef int Status;#define STACK_INIT_SIZE 10#define STACKINCREMENT 2struct SqStack{SElemType *base;SElemType *top;int stacksize;};FILE *fp;Status InitStack(SqStack &S){S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));if(!S.base)return 0;S.top=S.base;S.stacksize=STACK_INIT_SIZE;return OK;}Status StackEmpty(SqStack S){if(S.top==S.base)return TRUE;elsereturn FALSE;}Status ClearStack(SqStack &S){S.top=S.base;return OK;}Status DestroyStack(SqStack &S){free(S.base);S.base=NULL;S.top=NULL;S.stacksize=0;return OK;}Status Push(SqStack &S,SElemType e){if(S.top-S.base>=S.stacksize){S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*si zeof(SElemType));if(!S.base)return 0;S.top=S.base+S.stacksize;S.stacksize+=STACKINCREMENT;}*S.top++=e;return OK;}Status Pop(SqStack &S,SElemType &e){if(S.top==S.base)return ERROR;e=*--S.top;return OK;}Status StackTraverse(SqStack S,Status(*visit)(SElemType)) {while(S.top>S.base)visit(*S.base++);printf("\n");return OK;}Status visit(SElemType c){printf("%c",c);return OK;}void LineEdit(){SqStack s;char ch,c;int n,i;InitStack(s);scanf("%d",&n);ch=getchar();for(i=1;i<=n;i++){ch=getchar();while(ch!='\n'){switch(ch){case '#': Pop(s,c);break;case '@': ClearStack(s);break;default:Push(s,ch);}ch=getchar();}StackTraverse(s,visit);ClearStack(s);}DestroyStack(s);}int main(){LineEdit();return 1;}2.6表达式求值#include<stdio.h>#include<malloc.h>#define OK 1#define ERROR 0#define STACK_INIT_SIZE 100#define STACKINCREMENT 10typedef int Status;struct SqStack_T{char *base;char *top;int stacksize;};struct SqStack_N{int *base;int *top;int stacksize;};Status InitStack_T(SqStack_T &S){S.base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));if(!S.base)return ERROR;S.top=S.base;S.stacksize=STACK_INIT_SIZE;return OK;}Status InitStack_N(SqStack_N &S){S.base=(int*)malloc(STACK_INIT_SIZE*sizeof(int));if(!S.base)return ERROR;S.top=S.base;S.stacksize=STACK_INIT_SIZE;return OK;}int Push_T(SqStack_T &S,char e){if(S.top-S.base>=S.stacksize){S.base=(char*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof( char));if(!S.base)return ERROR;S.top=S.base+S.stacksize;S.stacksize+=STACKINCREMENT;}*S.top++=e;return OK;}int Push_N(SqStack_N &S,int e){if(S.top-S.base>=S.stacksize){S.base=(int*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(i nt));if(!S.base)return ERROR;S.top=S.base+S.stacksize;S.stacksize+=STACKINCREMENT;}*S.top++=e;return OK;}int Pop_T(SqStack_T &S,char &e){if(S.top==S.base)return ERROR;e=*--S.top;return OK;}int Pop_N(SqStack_N &S,int &e){if(S.top==S.base)return ERROR;e=*--S.top;return OK;}char GetTop_T(SqStack_T S){char e;if(S.top==S.base)return ERROR;e=*(S.top-1);return e;}int GetTop_N(SqStack_N S){int e;if(S.top==S.base)return ERROR;e=*(S.top-1);return e;}char Precede(char theta1,char theta2) {int a,b;switch(theta1){case '+': a=2; break;case '-': a=2; break;case '*': a=4; break;case '/': a=4; break;case '(': a=0; break;case ')': a=6; break;case '=': a=-1; break;}switch(theta2){case '+': b=1; break;case '-': b=1; break;case '*': b=3; break;case '/': b=3; break;case '(': b=6; break;case ')': b=0; break;case '=': b=-1; break;}if(a<b)return '<';elseif(a==b)return '=';elsereturn '>';}char precede(char e,char c){if(c=='+'||c=='-'){if(e=='+'||e=='-'||e==')'||e=='=') return '>';elsereturn '<';}if(c=='*'||'/'){if(e=='(')return '<';elsereturn '>';}if(c=='('){if(e==')')return '=';elsereturn '<';}if(c==')')return '>';if(c=='='){if(e=='=')return '=';elsereturn '<';}}int In(char c){if(c>='0'&&c<='9')return 1;elsereturn 0;}int Operate(int a,char theta,int b){int s;switch(theta){case '+': s=a+b; break;case '-': s=a-b; break;case '*': s=a*b; break;case '/':if(b!=0)s=a/b;elseprintf("Input error");break;}return s;}int main(){int k=0,m,y,a,b;SqStack_T OPTR;SqStack_N OPND;char c,theta;InitStack_T(OPTR); Push_T(OPTR,'=');InitStack_N(OPND); c=getchar();while(c!='='||GetTop_T(OPTR)!='='){if(In(c)){m=c-'0';if(k==1){Pop_N(OPND,y);y=m+y*10;Push_N(OPND,y);k=1;c=getchar();}else{y=m;Push_N(OPND,y);c=getchar();k=1;}}else{k=0;switch(Precede(GetTop_T(OPTR),c)){case '<': Push_T(OPTR,c); c=getchar(); break;case '=': Pop_T(OPTR,c); c=getchar(); break;case '>':Pop_T(OPTR,theta);Pop_N(OPND,b);Pop_N(OPND,a);Push_N(OPND,Operate(a,theta,b));break;}}}printf("%d",GetTop_N(OPND));return 0;}2.7队列的应用——银行客户平均等待时间#include<malloc.h>#include<stdio.h>#define OK 1#define ERROR 0typedef int Status;typedef int QElemType;#define MAXQSIZE 100typedef struct{QElemType *base;int front;int rear;}SqQueue;Status InitQueue(SqQueue &Q){Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));if(!Q.base)return ERROR;Q.front=Q.rear=0;return OK;}Status EnQueue(SqQueue &Q,QElemType e){if((Q.rear+1)%MAXQSIZE==Q.front)return ERROR;Q.base[Q.rear]=e;Q.rear=(Q.rear+1)%MAXQSIZE;return OK;}Status DeQueue(SqQueue &Q,QElemType &e){if(Q.front==Q.rear)return ERROR;e=Q.base[Q.front];Q.front=(Q.front+1)%MAXQSIZE;return OK;}Status GetHead(SqQueue Q,QElemType &e){if(Q.rear==Q.front)return ERROR;e=Q.base[Q.front];return OK;}int QueueLength(SqQueue Q){return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE; }Status QueueTraverse(SqQueue Q){int i;i=Q.front;if(Q.rear==Q.front)printf("The Queue is Empty!");else{printf("The Queu is:");while(i!=Q.rear){printf("%d",Q.base[i]);i=(i+1)%MAXQSIZE;}}printf("\n");return OK;}int main(){int i,a;SqQueue S;int p,q,e,r;float t,s=0;InitQueue(S);scanf("%d",&a);getchar();for(i=1;i<=a*2;i++){scanf("%d",&e);getchar();EnQueue(S,e);}p=S.base[S.front];while(S.rear>S.front){q=p+S.base[S.front+1];DeQueue(S,e);DeQueue(S,e);if(S.front==S.rear)break;r=q-S.base[S.front];if(r<0){r=0;p=S.base[S.front];continue;}s=s+r;p=q;}t=s/a;printf("%.2f\n",t);return OK;}3.1计算next值#include<stdio.h>#include<stdlib.h>#include<iostream.h>#define MAXSTRLEN 255typedef unsigned char SString[MAXSTRLEN+1];void get_next(SString T,int next[]){int i=1,j=0;next[1]=0;while(i<T[0]){if(j==0||T[i]==T[j]){i++;j++;next[i]=j;}elsej=next[j];}}。
数据结构上机答案

*/
/* maxk from the single sorted LinkList with head pointer L.*/ { LinkList p,q,pre; pre=L; while(pre->next&&pre->next->data<=mink) pre=pre->next; p=pre->next; while(p&&p->data<maxk) { q=p; p=p->next; free(q); } pre->next=p; } 21 void Inverse(SqList &L) { int i,j; ElemType p; for(i=0,j=L.length-1;i<j;i++,j--) { p=L.elem[j]; L.elem[j]=L.elem[i]; L.elem[i]= p; } } 21 void Inverse(LinkList &L) /* 对带头结点的单链表L实现就地逆置 */ { LinkList p,q,s; p=L->next; q=p->next; if(p->next!=NULL&&q->next!=NULL)
数据结构上机例题及答案

习题二⒉1描述以下四个概念的区别:头指针变量,头指针,头结点,首结点(第一个结点)。
解:头指针变量和头指针是指向链表中第一个结点(头结点或首结点)的指针;在首结点之前附设一个结点称为头结点;首结点是指链表中存储线性表中第一个数据元素的结点。
若单链表中附设头结点,则不管线性表是否为空,头指针均不为空,否则表示空表的链表的头指针为空。
2.2简述线性表的两种存储结构有哪些主要优缺点及各自使用的场合。
解:顺序存储是按索引直接存储数据元素,方便灵活,效率高,但插入、删除操作将引起元素移动,降低了效率;而链式存储的元素存储采用动态分配,利用率高,但须增设表示结点之间有序关系的指针域,存取数据元素不如顺序存储方便,但结点的插入和删除十分简单。
顺序存储适用于线性表中元素数量基本稳定,且很少进行插入和删除,但要求以最快的速度存取线性表中的元素的情况;而链式存储适用于频繁进行元素动态插入或删除操作的场合。
2.3 在头结点为h的单链表中,把值为b的结点s插入到值为a的结点之前,若不存在a,就把结点s插入到表尾。
Void insert(Lnode *h,int a,int b){Lnode *p,*q,*s;s=(Lnode*)malloc(sizeof(Lnode));s->data=b;p=h->next;while(p->data!=a&&p->next!=NULL){q=p;p=p->next;}if (p->data==a){q->next=s;s->next=p;}else{p->next=s;s->next=NULL;}}2.4 设计一个算法将一个带头结点的单链表A分解成两个带头结点的单链表A和B,使A 中含有原链表中序号为奇数的元素,而B中含有原链表中序号为偶数的元素,并且保持元素原有的相对顺序。
Lnode *cf(Lnode *ha){Lnode *p,*q,*s,*hb;int t;p=ha->next;q=ha;t=0;hb=(Lnode*)malloc(sizeof(Lnode));s=hb;while(p->next!=NULL){if (t==0){q=p;p=p->next;t=1;}else{q->next=p->next;p->next=s->next; s->next=p; s=p;p=p->next; t=0;}}s->next=NULL;return (hb);}2.5设线性表中的数据元素是按值非递减有序排列的,试以不同的存储结构,编写一算法,将x插入到线性表的适当位置上,以保持线性表的有序性。
数据结构实验报告答案

数据结构实验报告答案数据结构实验报告答案引言:数据结构是计算机科学中的重要概念,它涉及组织和管理数据的方法和技术。
在本次实验中,我们将研究和实践几种常见的数据结构,包括数组、链表、栈和队列。
通过这些实验,我们将深入理解数据结构的原理和应用。
一、数组数组是一种线性数据结构,它由一系列相同类型的元素组成。
数组的特点是可以通过索引来访问和修改元素,具有随机访问的能力。
在本次实验中,我们将实现一个简单的数组类,并进行一些基本操作,如插入、删除和查找。
首先,我们定义一个数组类,包含以下成员变量和方法:- size:数组的大小- elements:存储元素的数组- insert(index, element):在指定位置插入元素- remove(index):删除指定位置的元素- get(index):获取指定位置的元素- search(element):查找元素在数组中的位置通过实现上述方法,我们可以对数组进行各种操作。
例如,我们可以在数组的末尾插入一个元素,然后在指定位置删除一个元素。
我们还可以通过元素的值来查找其在数组中的位置。
二、链表链表是另一种常见的线性数据结构,它由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。
链表的特点是插入和删除操作的效率较高,但随机访问的效率较低。
在本次实验中,我们将实现一个简单的单向链表,并进行一些基本操作。
首先,我们定义一个节点类,包含以下成员变量和方法:- data:节点的数据元素- next:指向下一个节点的指针然后,我们定义一个链表类,包含以下成员变量和方法:- head:链表的头节点- insert(element):在链表的末尾插入一个节点- remove(element):删除链表中指定的节点- search(element):查找链表中指定元素的节点通过实现上述方法,我们可以对链表进行各种操作。
例如,我们可以在链表的末尾插入一个节点,然后删除链表中指定的节点。
数据结构上机答案(c语言版)

数据结构上机答案(c语言版)实习一:1、编写一个读入一个字符串,把它存入一个链表,并按相反的次序打印的程序。
2、设有一个单位的人员工资有如下信息:name、department、base pay、allowance、total。
现从键盘输入一组人员工资数据并将它们存储到名为paydata的文件中;再从paydata取出工资数据并给每个人的base pay增加100元,增加后将工资数据显示于屏幕(每行1人)。
请编写能够完成上述工作的程序。
代码如下:1.#include#include#includevoid main(){char x;struct node //定义个结构node{char c;struct node *next;};struct node *head,*pb,*pf,*p,*s,*t; //定义指针printf("请输入字符串,按Enter结束!\n");for(int i=0;x!='\n';i++){pb=(struct node *)malloc(sizeof(struct node));//动态分配n字节的内存空间scanf("%c",&pb->c); //输入字符x=pb->c;if(i==0){ //输入的首个字符作为头结点pfhead=pb;pf=head;}else if(pb->c!='\n'){ //如果输入的是Enter,输入终止,否则把字符依次存入链表pf->next=pb; //把输入的字符pb存在pf后,pb后为空pb->next=NULL;pf=pb;//pb赋给pf,重复上述操作p=head;}}for(;p!=NULL;p=p->next)s=p; //把指向链表的最后一个字符的指针赋给sprintf("输出结果为:\n");printf("%c",s->c);//输出链表的最后一个字符for(p=head;s!=head;)//若s==head,该链表只有一个字符。
数据结构实验答案

# include
# define maxnum 20
typedef int DataType ;
typedef struct
{ DataType data[maxnum] ;
int length ;
}SeqList ;
int MergeQL(SeqList la , SeqList lb , SeqList *lc)
实验一 线性表的顺序存储实验
一,实验目的
1,掌握用Visual C++6.0上机调试顺序表的基本方法
2,掌握顺序表的基本操作,插入,删除,查找,以及有序顺序表的合并等算法的实现
二,实验内容
1,顺序表基本操作的实现
[问题描述] 当我们要在顺序表的第i个位置上插入一个元素时,必须先将顺序表中第i个元素之后的所有元素依次后移一个位置,以便腾空一个位置,再把新元素插入到该位置.若是欲删除第i个元素时,也必须把第i个元素之后的所有元素前移一个位置.
ListNode *s,*r; /*工作指针*/
r=head; /*尾指针初值也指向头结点*/
while((ch=getchar())!='\n')
{
s=(ListNode *)malloc(sizeof(ListNode));
s->data=ch;
r->next=s;
r=s;
}
(*L)->next=NULL;
}
int List_Length(ListNode *L )
{
int n=0;ListNode *p=L->next;
while(p!=NULL)
《数据结构习题解析与上机指导》参考答案

参考答案第1章一、选择题1. B2. C3. B4. C二、填空题1. 数据、数据2. 基本单位3. 数据项、数据项4. 相互关系、组织形式5. 逻辑关系、逻辑关系、数学模型6. 存储结构、存储结构7. 数据的运算、数据的运算、数据的运算8. 集合、集合9. 线性结构10. 树型结构11. 多对多12. 非线性结构、线性结构、非线性结构13. 顺序存储14. 链接存储15. 稠密索引、稀疏索引、稠密索引、稀疏索引、稠密索引、稀疏索引16. 散列存储17. 有限序列18. 有穷19. 确定、相同20. 可行、有限、具体实现21. 正确性、可读性、健壮性、效率22. 运行时间、所占据空间23. 存储空间三、判断题1. 错误:树型结构也可以用顺序方式迚行存储。
2. 错误:数据元素是数据的基本单位,数据项是数据的最小单位。
3. 错误:算法用各种计算机语言描述表现为一个程序,但是不等于程序,程序逻辑不一定能满足有穷性。
4. 正确。
5. 正确。
6. 正确。
7. 正确。
8. 正确。
四、综合题1. 该算法的时间复杂度为:O(m×n)。
2. 该算法的时间复杂度为:3. 该算法的时间复杂度为:O(m×n×t)。
4. 该算法的时间复杂度为:log3(n)。
5. 该算法的时间复杂度为:。
第2章一、选择题1. A2. C3. A4. D5. D6. A7. B8. D9. B10. C11. C 12. C 13. D 14. D二、填空题1. 一对一2. 直接前驱、直接后继3. 有限序列、长度、空表4. 顺序存储结构、逻辑顺序、地址相邻5. 仸意、仸意、不连续、逻辑关系6. 数据域、指针域、链域7. 非顺序、非顺序映像8. 循环链表9. 双向链表10. 所指向的结点本身11. P->next=p->next->next12. P->next->prior=P->prior13. 线性表14. 双链表15. n-i+116. n-i17. S->next=P->next; P->next=S18. p->prior->next=S;s->prior=p->prior;s->next=p;p->prior=s;19. head(tail(tail((head(tail(head(A))))))20. O(n)21. (L==L->Next) && (L==L->Prior)22. 线性23. 顶三、判断题1. 错误:链表存储中,结点之间可以连续也可以不连续,但结点内部是连续的。
数据结构(含上机实训)课后题答案

*e =Q->elem[tmpfront];//用e返回队头元素
}
一、填空题
1.串中的元素为字符型数据
2.两个串的长度相等,并且各个对应位置的字符都相等
3.定长顺序存储,堆存储,块链存储,堆存储
4.1100
str2[k ++] = str1[i ++];
else
{
if (Isoperator (str1[i]) == -1)//如果读入字符不是操作符,则出错
return ERROR;
switch (Compare (GetTop (OPTR), str[i]))//比较操作符优先级高低
{
case '<'://读入字符优先级高于栈顶字符优先级
OPTR栈
OPND栈
当前读入字符
步骤
OPTR栈
OPND栈
当前读入字符
1
#
35
8
# - /
35 50
2
2
#
35
-
9
# - /
35 50 2
+
3
# -
35
5
10
# -
35 25
+
4
# -
35 5
*
11
#
10
+
5
# - *
35 5
10
12
# +
10
5
6
# - *
35 5 10
/
13
# +
10 5
#
7
# -
break;
default:
Push(&S,ch);//输入其他字符直接进栈
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
《数据结构实验指导书》答案实验一:1、请编写函数int fun(int *a, int *b),函数的功能是判断两个指针a和b所指存储单元的值的符号是否相同;若相同函数返回1,否则返回0。
这两个存储单元中的值都不为0。
在主函数中输入2个整数、调用函数fun、输出结果。
#include <stdio.h>int fun(int *a, int *b){if (*a*(*b)>0) return(1);else return(0);}main(){int x,y;scanf("%d%d",&x,&y);if (fun(&x,&y)) printf("yes\n");else printf("no");}2、计算1+2+3+……+100,要求用指针进行设计。
即设计函数int fun(int *n)实现求1+2+3+……+*n,在主函数中输入、调用、输出结果。
#include <stdio.h>int fun(int *n){int i,sum=0;for (i=1;i<=*n;i++)sum+=i;return(sum);}main(){int x,sum;scanf("%d",&x);printf("the sum is %d\n",fun(&x));}3、函数的功能是求数组a中最大数的位置(位序号)。
在主函数中输入10个整数、调用函数fun、输出结果。
#define N 10#include <stdio.h>void input(int *a,int n){int i;for (i=0;i<n;i++)scanf("%d",a+i); /*scanf("%d",&a[i]);*/}int fun(int *a,int n){int i,*max;max=a;for (i=1;i<n;i++)if (a[i]>*max) max=a+i;return(max-a);}main(){int a[N],maxi;input(a,N);maxi=fun(a,N);printf("\n the max position is %d\n",maxi);}4、请编写函数fun(int *a,int n, int *odd, int *even),函数的功能是分别求出数组a 中所有奇数之和和所有偶数之和。
形参n给出数组中数据的个数;利用指针odd和even分别返回奇数之和和偶数之和。
在主函数中输入10个整数、调用函数fun、输出结果。
#define N 10#include <stdio.h>void input(int *a,int n){int i;for (i=0;i<n;i++)scanf("%d",a+i); /*scanf("%d",&a[i]);*/}void fun(int *a,int n, int *odd, int *even){int i,sum1=0,sum2=0;for (i=0;i<n;i++){if (a[i]%2==0) sum1+=a[i];else sum2+=a[i];}*odd=sum1;*even=sum2;}main(){int a[N],odd,even;input(a,N);fun(a,N, &odd, &even);printf("the odd is %d\tthe even is %d\n",odd,even);}5、请编写函数int fun(int *a, int *b,int n),函数的功能是把数组a中所有为偶数的数,放在另一个数组中b。
在主函数中输入10个整数、调用函数fun、输出结果。
#define N 10#include <stdio.h>void input(int *a,int n){int i;for (i=0;i<n;i++)scanf("%d",a+i); /*scanf("%d",&a[i]);*/}void output(int *a,int n){int i;printf("\nthe odd is:\n");for (i=0;i<n;i++)printf("%5d",*(a+i)); /*printf("%d",a[i]);*/}int fun(int *a, int *b,int n){int i,j=0;for (i=0;i<n;i++)if (a[i]%2==0) { b[j]=a[i]; j++;}return(j);main(){int a[N],b[N],m;input(a,N);m=fun(a,b,N);output(b,m);}6、请编写函数int fun(int *a,,int n),函数的功能是把数组a中最大数和最小数交换。
在主函数中输入10个整数、调用函数fun、输出结果。
#define N 10#include <stdio.h>void input(int *a,int n){int i;for (i=0;i<n;i++)scanf("%d",a+i); /*scanf("%d",&a[i]);*/}void output(int *a,int n){int i;printf("\nthe result is:\n");for (i=0;i<n;i++)printf("%5d",*(a+i)); /*printf("%d",a[i]);*/}void fun(int *a,int n){int i,*max,*min,temp;max=min=a;for (i=1;i<n;i++){if (a[i]>*max) max=a+i;if (a[i]<*min) min=a+i;}printf("the max is %d,the position is %d\n",*max,max-a);printf("the min is %d,the position is %d\n",*min,min-a);if (max!=min){temp=*max;*max=*min;*min=temp;}}main(){int a[N],m;input(a,N);fun(a,N);output(a,N);}7、请编写函数int fun(int a[4][4]),函数的功能是把矩阵a转置。
在主函数中输入、调用函数fun、输出结果。
#define N 4#include <stdio.h>void input(int a[][4],int n){int i,j;for (i=0;i<n;i++)for (j=0;j<n;j++)scanf("%d",&a[i][j]);}void output(int a[][4],int n){int i,j;printf("\nthe result is:\n");for (i=0;i<n;i++){printf("\n");for (j=0;j<n;j++)printf("%4d",*(*(a+i)+j)); /*printf("%d",a[i][j]);*/}}void fun(int a[][4],int n){int i,j,temp;for (i=0;i<n;i++)for (j=0;j<i;j++){temp=a[i][j];a[i][j]=a[j][i];a[j][i]=temp;}}main(){int a[N][N];input(a,N);fun(a,N);output(a,N);}8、请编写函数int fun(char *a),函数的功能是分别求出字符串a 的长度。
在主函数中输入1个字符串、调用函数fun、输出结果。
#include <stdio.h>int fun(char *a){int i=0;char *p;p=a;while (*p){i++;p++;}return(i);}main(){char str[20],*cp;cp=str;gets(cp);printf("the length of %s is %d\n",cp,fun(cp));}9、10、#include <stdio.h>#include <string.h>#define N 2typedef struct student /*定义数据结构(数据类型)*/{long num;char name[10];int score[3]; /*存放三门课成绩 */float average; /*/平均成绩*/} stu;void intput(stu s[],int n) /*输入数据 */{int i,j; /*i表示处理学生的下标,J表示成绩编号 */for (i=0;i<n;i++){printf("input num:\n");scanf("%ld",&s[i].num);printf("input name:\n");scanf("%s",s[i].name);printf("input 3 score:\n");for (j=0;j<3;j++)scanf("%d",&s[i].score[j]);}}void stu_av(stu s[],int n)/*求每个学生的平均成绩*/ {int i,j,sum;for (i=0;i<n;i++){sum=0;for (j=0;j<3;j++)sum+=s[i].score[j] ;s[i].average=sum/3.0;}}float av(stu s[],int n)/*求总平均成绩*/{int i;float sum=0.0,ave;for (i=0;i<n;i++)sum+=s[i].average;ave=sum/n;return(ave);}int max(stu s[],int n)/*求平均成绩最高学生的下标*/ {int i,maxi=0;for (i=1;i<n;i++)if (s[i].average>s[maxi].average)maxi=i;return(maxi);}main(){int maxi,j;stu a[N];/*定义变量 */intput(a,N);stu_av(a,N);printf("the score average is %f\n", av(a,N));maxi=max(a,N);printf("the max average student data:\n");printf("%10ld",a[maxi].num);printf("%10s",a[maxi].name);for (j=0;j<3;j++)printf("%5d",a[maxi].score[j]);printf("%5.1f",a[maxi].average);getch();}实验二1、#include <stdio.h>#define MaxLen 50typedef int elemtype;struct datatype{elemtype *elem;int length;};typedef struct datatype sqlist;void create (sqlist *a){int i,n;a->elem=(elemtype *)malloc(MaxLen*sizeof(elemtype)); printf("创建一个顺序表\n");printf("输入元素个数\n");scanf("%d",&a->length);for (i=0;i<a->length;i++){printf("输入第%d个元素值:",i+1);scanf("%d",a->elem+i);}}void invert(sqlist *a){int m=a->length/2,i;elemtype temp;for (i=0;i<m;i++){temp=*(a->elem+i);*(a->elem+i)=*(a->elem+a->length-1-i);*(a->elem+a->length-1-i)=temp; }}void disp(sqlist *a){ int i;for (i=0;i<a->length;i++)printf("%5d:%d\n",i+1,*(a->elem+i));getch();}void main(){sqlist a;create(&a);disp(&a);invert(&a);disp(&a);}2、#include <stdio.h>#include <malloc.h>#define NULL 0typedef int elemtype;typedef struct linknode{elemtype data;struct linknode *next;}nodetype;nodetype *create(){elemtype d;nodetype *h,*s,*t;int i=1;h=NULL;printf("建立一个单链表\n");while (1){printf("输入第%d节点data域值:",i);scanf("%d",&d);if (d==0) break ; /*以0表示输入结束*/if(i==1) /*建立第一个结点*/ {h=(nodetype *)malloc(sizeof(nodetype));h->data=d;h->next=NULL;t=h;}else{s=(nodetype *)malloc(sizeof(nodetype));s->data=d;s->next=NULL;t->next=s;t=s; /*t始终指向生成的单链表的最后一个结点*/}i++;}return h;}void disp(nodetype *h){nodetype *p=h;printf("输出一个单链表:\n");if (p==NULL) printf("空表");while (p!=NULL){printf("%d",p->data);p=p->next;}printf("\n");getch();}int len(nodetype *h){int i=0;nodetype *p=h;while (p){i++;p=p->next;}return(i);}nodetype *invert(nodetype *h){nodetype *p,*q,*r;if (len(h)<=1){printf("逆置的单链表至少有2个节点\n"); return(NULL);}else{p=h;q=p->next;while (q!=NULL){r=q->next;q->next=p;p=q;q=r;}h->next=NULL;h=p;return h;}}void main(){nodetype *head;head=create();disp(head);head=invert(head);disp(head);}4、(1)#include <stdio.h>#define MaxLen 50typedef struct{long num;int score;}elemtype;typedef struct datatype{elemtype *elem;int length;}sqlist;void create (sqlist *a){long i=0,n;int s;a->elem=(elemtype *)malloc(MaxLen*sizeof(elemtype));printf("创建一个顺序表\n");printf("输入学生学号和成绩:0作为结束标志\n");scanf("%ld%d",&n,&s);while (n!=0){(a->elem)[i].num=n;(a->elem)[i].score=s;i++;scanf("%ld%d",&n,&s);}a->length=i;}void disp(sqlist *a){ int i;for (i=0;i<a->length;i++)printf("%5d:%ld %d\n",i+1,(a->elem)[i].num,(a->elem)[i].score); getch();}void main(){sqlist a;create(&a);disp(&a);}(2)见5(2)5、(1)#include <stdio.h>#define MaxLen 50typedef struct{long num;int score;}elemtype;typedef struct datatype{elemtype *elem;int length;}sqlist;void create (sqlist *a){long i=0,n;int s;a->elem=(elemtype *)malloc(MaxLen*sizeof(elemtype));printf("创建一个顺序表\n");printf("输入学生学号和成绩:0作为结束标志\n");scanf("%ld%d",&n,&s);while (n!=0){(a->elem)[i].num=n;(a->elem)[i].score=s;i++;scanf("%ld%d",&n,&s);}a->length=i;}void sort(sqlist *a){ int i,j,k,s;long n;for (i=0;i<a->length-1;i++){k=i;for (j=i+1;j<a->length;j++)if ((a->elem)[j].score<(a->elem)[k].score) k=j;if (k!=i){n=(a->elem)[i].num;(a->elem)[i].num=(a->elem)[k].num;(a->elem)[k].num=n;s=(a->elem)[i].score;(a->elem)[i].score=(a->elem)[k].score; (a->elem)[k].score=s;}}}void disp(sqlist *a){ int i;for (i=0;i<a->length;i++)printf("%5d:%ld %d\n",i+1,(a->elem)[i].num,(a->elem)[i].score); getch();}void main()sqlist a;create(&a);disp(&a);sort(&a);disp(&a);}(2)#include <stdio.h>#include <malloc.h>#define NULL 0typedef struct linknode{long num;int score;struct linknode *next;}nodetype;nodetype *create(){long n;int sc;nodetype *h,*s,*t;h=(nodetype *)malloc(sizeof(nodetype));;h->next=NULL;t=h;printf("创建一个顺序表\n");printf("输入学生学号和成绩:0作为结束标志\n"); scanf("%ld%d",&n,&sc);while (n!=0){s=(nodetype *)malloc(sizeof(nodetype));s->num=n;s->score=sc;t->next=s;t=s;scanf("%ld%d",&n,&sc);}t->next=NULL;return h;}void disp(nodetype *h)nodetype *p=h->next;printf("输出一个单链表:\n");while (p!=NULL){printf("%ld %d\n",p->num,p->score);p=p->next; }printf("\n");getch();}nodetype *insertorder(nodetype *h,nodetype *s) {nodetype *p,*q;q=h;p=q->next;while (p!=NULL && s->score>p->score){q=p;p=p->next;}s->next=p;q->next=s;return(h);}nodetype *sort(nodetype *h){nodetype *p,*s;p=h->next;h->next=NULL;while (p!=NULL){s=p->next;h=insertorder(h,p);p=s;}return h;}void main(){nodetype *head;head=create();disp(head);head=sort(head);disp(head);}实验三:7、试写一个算法,识别依次读入的一个以为结束符的字符序列是否为形如‘序列1&序列2’模式的字符序列。