数据结构上机习题
- 格式:pdf
- 大小:4.95 MB
- 文档页数:23


实验一#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;}}} 222222222222222222222222222222222222222222222222222222222222222222222222222222#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;}3333333333333333333333333333333333333333333333333333333333 33333333333333333333#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;}4444444444444444444444444444444444444444444444444444444444 44444444444444444444#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;}}}55555555555555555555555555555555555555555555555555555555555 5555555555555555555#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;}6666666666666666666666666666666666666666666666666666666666 66666666666666666666#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;}实验二实验二实验二实验二实验二实验二实验二实验二实验二实验二实验二实验二实验二#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)*s izeof(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;}}}2222222222222222222222222222222222222222222222222222222222 22222222222222222222#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)*s izeof(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;}3333333333333333333333333333333333333333333333333333333333 33333333333333333333typedef 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)*s izeof(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;}4444444444444444444444444444444444444444444444444444444444 44444444444444444444typedef 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)*s izeof(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;} 55555555555555555555555555555555555555555555555555555555555 5555555555555555555#include<stdio.h>#include<malloc.h>#define OK 1#define ERROR 0#define STACK_INIT_SIZE 100#define STACKINCREMENT 10。
数据结构上机答案(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,该链表只有一个字符。
算法与数据结构实践考核上机练习题1-5(201103)算法与数据结构上机考试练习题1-5(201103)(一)顺序表基本操作及应用一、顺序表基本操作1、考核要求实现顺序表动态存储结构的创建、查找、插入、删除、遍历等基本操作。
2、数据结构定义参考typedef struct {int *elem; // 存储空间基址int length; // 当前长度int listsize; // 当前分配的存储容量}SqList;3、基本操作定义参考bool InitList(SqList &L) // 构造一个空的顺序表int LocateElem(SqList L, int e)// 查找表中第1个与e相等的元素,返回元素的位序bool ListInsert(SqList &L,int i, int e)// 在顺序表中第i个位置之前插入数据元素ebool ListDelete(SqList &L,int i)// 删除顺序表中的第i个数据元素bool ListTraverse(SqList L)//遍历顺序表4、程序验收从键盘录入基础数据,测试各基本操作模块的功能。
测试流程:(1)创建空表,输出初始化结构数据。
(2)在表尾依次插入1 2 3 4 5 6 7 8 9 10,并遍历输出。
(3)查找值为12的元素,并给出查询结果。
(4)删除值为9的元素,并遍历输出。
5、程序运行结果参考初始化L后:L.elem=2086 L.length=0 L.listsize=10在L的表尾依次插入1~10后:L.elem=1 2 3 4 5 6 7 8 9 10 没有值为12的元素依次输出L的元素:L.elem =1 2 3 4 5 6 7 8 9 10二、顺序表基本应用1、考核要求应用顺序表的基本操作实现直接插入排序。
2、程序验收例如:初始数据 1 6 5 3 4 2排序后 1 2 3 4 5 6一、单链表基本操作1、考核要求实现单链表结构的创建、查找、插入、删除、遍历等基本操作。