数据结构上机考试(含答案)

合集下载

数据结构实验上机题答案

数据结构实验上机题答案

实验一#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。

数据结构考试题及答案

数据结构考试题及答案

数据结构考试题及答案一、选择题(每题2分,共20分)1. 以下哪个不是线性数据结构?A. 数组B. 链表C. 树D. 图2. 在一个单链表中,删除一个节点的操作需要知道该节点的:A. 地址B. 值C. 索引D. 前驱节点的引用3. 栈(Stack)是一种:A. 线性表B. 树状结构C. 图结构D. 散列表4. 哈希表解决冲突最常用的方法是:A. 排序B. 链地址法C. 再散列D. 除留余数法5. 以下哪个排序算法是稳定的?A. 快速排序B. 冒泡排序C. 选择排序D. 堆排序二、简答题(每题10分,共30分)1. 简述数组和链表的区别。

2. 解释二叉搜索树的基本概念及其优势。

3. 什么是递归?请给出一个简单的递归算法例子。

三、计算题(每题25分,共50分)1. 给定一个无序数组,请写出一个时间复杂度为O(n log n)的排序算法,并说明其工作原理。

2. 描述如何使用队列来实现一个简单的文本编辑器的撤销和重做功能。

四、编程题(共30分)编写一个函数,该函数接受一个整数数组作为参数,返回数组中所有元素的和。

如果数组为空,返回0。

答案一、选择题1. 答案:C(树和图都是非线性结构)2. 答案:D(需要前驱节点的引用来删除节点)3. 答案:A(栈是一种后进先出的特殊线性表)4. 答案:B(链地址法是解决哈希冲突的常用方法)5. 答案:B(冒泡排序是稳定的排序算法)二、简答题1. 数组和链表的区别:- 数组是连续的内存空间,链表是非连续的。

- 数组的索引访问速度快,链表需要遍历。

- 数组的大小固定,链表动态可变。

2. 二叉搜索树的基本概念及其优势:- 二叉搜索树是一种特殊的二叉树,左子树上所有节点的值小于它的根节点的值,右子树上所有节点的值大于它的根节点的值。

- 优势:支持快速的查找、插入和删除操作。

3. 递归是函数自己调用自己的过程。

例如,计算n的阶乘的递归算法: ```cint factorial(int n) {if (n <= 1) return 1;return n * factorial(n - 1);}```三、计算题1. 快速排序算法:- 选择一个元素作为“基准”(pivot)。

全国计算机等级考试(2级)上机考试题库及解析004

全国计算机等级考试(2级)上机考试题库及解析004

(1)下列数据结构中,属于非线性结构的是()。

A)循环队列B)带链队列C)二叉树D)带链栈(2)下列数据结构中,能够按照“先进后出”原则存取数据的是()。

A)循环队列B)栈C)队列D)二叉树(3)对于循环队列,下列叙述中正确的是()。

A)队头指针是固定不变的B)队头指针一定大于队尾指针C)队头指针一定小于队尾指针D)队头指针可以大于队尾指针,也可以小于队尾指针(4)算法的空间复杂度是指()。

A)算法在执行过程中所需要的计算机存储空间B)算法所处理的数据量C)算法程序中的语句或指令条数D)算法在执行过程中所需要的临时工作单元数(5)软件设计中划分模块的一个准则是()。

A)低内聚低耦合B)高内聚低耦合C)低内聚高耦合D)高内聚高耦合(6)下列选项中不属于结构化程序设计原则的是()。

A)可封装B)自顶向下C)模块化D)逐步求精(7)软件详细设计产生的图如下:该图是()。

A)N-S图B)PAD图C)程序流程图D)E-R图(8)数据库管理系统是()。

A)操作系统的一部分B)在操作系统支持下的系统软件C)一种编译系统D)一种操作系统(9)在E-R图中,用来表示实体联系的图形是()。

A)椭圆形B)矩形C)菱形D)三角形(10)有三个关系R,S和T如下:其中关系T由关系R和S通过某种操作得到,该操作为()。

A)选择B)投影C)交D)并(11)下列符号中可以用做C++标识符的是()。

A)_radiusB)foo~barC)elseD)3room(12)下列各组类型声明符中,含义相同的一组是()。

A)unsigned long int和longB)signed short int和shortC)unsigned short和shortD)short int和int(13)必须用一对大括号括起来的程序段是()。

A)switch语句中的case标号语句B)if语句的分支C)循环语句的循环体D)函数的函数体(14)语句int *p = &k;定义了指针p,与这个语句等效的语句序列是()。

数据结构上机答案(c语言版)

数据结构上机答案(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,该链表只有一个字符。

数据结构上机考题

数据结构上机考题

05信管《数据结构》上机考题(A卷)
学号:姓名:成绩:
试题:建立一个数据为整型的单链表L,然后将该链表中数据域值最小的那个结点移到链表的最前端。

要求与评分标准:
第一步:建立单链表(30分)
第二步:显示该单链表(10分)
第三步:查找链表中数据域值最小的结点,并将它移到链表的最前端(50分)第四步:显示该单链表,检查上述操作是否成功(10分)
05信管《数据结构》上机考题(B卷)
学号:姓名:成绩:
试题:在一个递增有序的顺序表中插入一个元素,使插入之后仍有序。

要求与评分标准:
第一步:建立一个递增有序的顺序表,注:可以在输入数据时按递增的顺序输入(30分)
第二步:显示该顺序表(10分)
第三步:在顺序表中找到合适的位置插入指定的元素,使插入之后仍有序(50分)第四步:显示该顺序表,检查上述操作是否成功(10分)
05信管《数据结构》上机考题(C卷)
学号:姓名:成绩:
试题:已知单链表L中的元素递增有序,请用高效的办法删除L中元素值大于mink且小于maxk的所有结点(注:mink和maxk由形参给出,它们与链表的数据域同类型且mink且小于maxk)
要求与评分标准:
第一步:建立递增有序的单链表L(30分)
第二步:显示该单链表(10分)
第三步:用高效的办法删除L中元素值大于mink且小于maxk的所有结点(50分)
第四步:显示该单链表,检查上述操作是否成功(10分)。

数据结构试题及答案

数据结构试题及答案

数据结构试题及答案一、选择题(每题2分,共20分)1. 在数据结构中,线性结构的特点是元素之间存在一对一的线性关系。

以下哪个数据结构不属于线性结构?A. 栈B. 队列C. 树D. 链表答案:C2. 栈(Stack)是一种后进先出(LIFO)的数据结构,以下哪个操作不是栈的基本操作?A. PushB. PopC. TopD. Sort答案:D3. 在二叉树的遍历中,前序遍历的顺序是:A. 根-左-右B. 左-根-右C. 右-根-左D. 根-右-左答案:A4. 哈希表的冲突可以通过多种方法解决,以下哪个不是解决哈希表冲突的方法?A. 链地址法B. 开放地址法C. 再散列法D. 排序法答案:D5. 以下哪个排序算法是稳定的?A. 快速排序B. 堆排序C. 归并排序D. 选择排序答案:C6. 在图的遍历中,深度优先搜索(DFS)使用的是哪种数据结构来实现?A. 队列B. 栈C. 链表D. 哈希表答案:B7. 以下哪个是图的存储方式?A. 顺序存储B. 链式存储C. 散列表D. 矩阵存储答案:D8. 动态数组(如C++中的vector)在插入元素时可能需要进行的操作是:A. 原地扩展B. 复制元素C. 重新分配内存D. 释放内存答案:C9. 以下哪个不是算法的时间复杂度?A. O(1)B. O(log n)C. O(n^2)D. O(n!)答案:D10. 在查找算法中,二分查找法要求被查找的数据必须是:A. 无序的B. 有序的C. 随机分布的D. 唯一元素答案:B二、简答题(每题5分,共30分)1. 简述链表和数组的区别。

答案:链表和数组都是存储数据的线性数据结构,但它们在内存分配、访问方式、插入和删除操作等方面存在差异。

数组在内存中是连续存储的,可以通过索引快速访问任意元素,但插入和删除元素时可能需要移动大量元素。

链表在内存中是非连续存储的,每个元素包含数据和指向下一个元素的指针,不支持通过索引快速访问,但插入和删除操作只需要改变指针,不需要移动其他元素。

数据结构试题及答案(10套)

数据结构试题及答案(10套)

数据结构试题及答案(10套)数据结构试题及答案(10套)根据您的需求,我为您准备了10套数据结构试题及答案。

每套试题包含以下几个部分:选择题、填空题、编程题及答案解析。

下面是试题的具体内容:第一套试题:选择题:1. 在数据结构中,什么是栈?A. 先进先出(FIFO)的数据结构B. 后进先出(LIFO)的数据结构C. 随机访问的数据结构D. 无序排列的数据结构2. 以下哪种操作与队列的特性不相符?A. 入队操作B. 出队操作C. 查找操作D. 获取队首元素填空题:1. ______ 是一种动态集合,支持插入、删除和查找等操作。

2. 在二叉搜索树中,中序遍历的结果是________。

编程题:实现一个栈的数据结构,并包含以下操作:- push(x):将元素 x 压入栈中- pop():删除栈顶的元素并返回该元素- top():获取栈顶元素的值- empty():检查栈是否为空答案解析:选择题:B、C填空题:1. 集合 2. 升序序列编程题:略第二套试题:选择题:1. 以下哪个数据结构是一种广度优先搜索的应用?A. 栈B. 队列C. 堆D. 链表2. 在链表中,如果要删除一个节点,只给出该节点的指针,那么需要通过什么方式完成删除操作?A. 直接删除该节点B. 指向该节点的前一个节点的指针C. 指向该节点的后一个节点的指针D. 无法完成删除操作填空题:1. 树是一种________的数据结构。

2. 二叉树每个节点最多有______个子节点。

编程题:实现一个队列的数据结构,并包含以下操作:- enqueue(x):将元素 x 入队- dequeue():删除队首的元素并返回该元素- peek():获取队首元素的值- is_empty():检查队列是否为空答案解析:选择题:B、B填空题:1. 分层组织 2. 2编程题:略(以下部分省略)通过以上的题目,您可以对数据结构的知识点进行综合练习和复习。

每套试题包含了不同难度和类型的题目,能够帮助您全面了解和掌握数据结构的概念和操作。

数据结构试题库及答案

数据结构试题库及答案

数据结构试题库及答案一、选择题(每题2分,共20分)1. 在数据结构中,线性表的顺序存储结构通常使用()来存储。

A. 链表B. 栈C. 队列D. 数组答案:D2. 以下哪个算法不是排序算法?A. 快速排序B. 归并排序C. 深度优先搜索D. 堆排序答案:C3. 在二叉树的遍历算法中,先访问根节点,然后遍历左子树,最后遍历右子树的遍历方式是()。

A. 先序遍历B. 中序遍历C. 后序遍历D. 层序遍历答案:A4. 哈希表的冲突解决方法不包括以下哪种?A. 链地址法B. 线性探测法C. 二分查找法D. 再散列法答案:C5. 在图的遍历算法中,广度优先搜索(BFS)使用的辅助数据结构是()。

A. 栈B. 队列C. 堆D. 链表答案:B6. 下列关于堆的描述中,错误的是()。

A. 堆是一种特殊的完全二叉树B. 堆中的每个节点的值都大于其子节点的值C. 堆可以用于实现优先队列D. 堆的插入操作的时间复杂度为O(log n)答案:B7. 在一个长度为n的数组中,使用二分查找算法查找一个元素的最坏情况下的时间复杂度是()。

A. O(1)B. O(n)C. O(n^2)D. O(log n)答案:D8. 以下哪个数据结构不是线性结构?A. 链表B. 栈C. 队列D. 二叉树答案:D9. 以下哪个算法是动态查找表?A. 直接索引B. 顺序查找C. 二分查找D. 哈希表答案:D10. 在图的表示方法中,邻接矩阵表示法的缺点是()。

A. 占用空间大B. 占用空间小C. 插入和删除操作复杂D. 遍历操作复杂答案:A二、填空题(每题2分,共20分)1. 在一个长度为n的数组中,使用顺序查找算法查找一个元素的时间复杂度为________。

答案:O(n)2. 一个具有n个节点的完全二叉树的高度为________。

答案:log2(n) + 1(向上取整)3. 一个长度为n的链表,删除一个节点的时间复杂度为________。

答案:O(1)4. 在图的表示方法中,邻接表表示法的缺点是________。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

《数据结构》上机练习题1、设有两个有序序列,利用归并排序将它们排成有序表,并输出。

2、设有一有序序列,从键盘输入一个数,判别是否在序列中,如果在输出“YSE”;否则,将它插入到序列中使它仍然有序,并输出排序后的序列。

3、设有一有序序列,从键盘输入一个数,判别是否在序列中,如果不在,则输出“NO”,否则,将它从序列中删除它,并输出删除后的序列。

4、从键盘输入一组任意数据,建立一个有序链表,并从链头开始输出该链,使输出结果是有序的。

5、从键盘输入一组任意数据,建立一个包含所有输入数据的单向循环链表,并从链表的任意开始,依次输出该链表中的所有结点。

10、设有一个链表,(自己建立,数据从键盘输入),再从键盘输入一个数,判别是否在链表中,如果不在,则输出“NO“,否则,将它从链表中删除,并输出删除后的链表。

11、设有一个链表,(自己建立,数据从键盘输入),再从键盘输入一个数,判别是否在链表中,如果在输出“YSE”,否则,将它从插入到链头,并输出插入后的链表。

12、设有一个链表,(自己建立,数据从键盘输入),再从键盘输入一个数,判别是否在链表中,如果在输出“YSE”,否则,将它从插入到链尾,并输出插入后的链表。

13、编写栈的压栈push、弹栈pop函数,从键盘输入一组数据,逐个元素压入堆栈,然后再逐个从栈中弹出它们并输出。

14、编写栈的压栈push、弹栈pop函数,用它判别()的匹配问题。

15、按类似先序遍历结果输入一序列,建立一棵二叉树(算法6、4),输出二叉树中序遍历的结果。

16、按类似先序遍历结果输入一序列,建立一棵二叉树(算法6、4),输出二叉树先序遍历的结果。

17、按类似先序遍历结果输入一序列,建立一棵二叉树(算法6、4),输出二叉树后序遍历的结果。

18、按类似先序遍历结果输入一序列,建立一棵二叉树(算法6、4),输出二叉树的总结点数。

19、按类似先序遍历结果输入一序列,建立一棵二叉树(算法6、4),输出二叉树叶子结点数。

20、按类似先序遍历结果输入一序列,建立一棵二叉树(算法6、4),输出此二叉树的高度。

21、给出一个无向图的邻接矩阵,输出各个顶点的度。

22、给出一个有向图的邻接矩阵,输出各个顶点的入度与出度。

23、输入一个有序序列,利用折半查找来查找一个数是否在序列中,如在,则输出其位置,否则输出“NO”。

24、用插入排序方法对一组数据进行排序,并输出每趟排序的结果。

25、用选择排序方法对一组数据进行排序,并输出每趟排序的结果。

26、用希尔(SHELL)排序方法对一组数据进行排序,并输出每趟排序的结果。

27、用快速排序方法对一组数据进行排序,并输出每趟排序的结果。

.答案:1. #include <stdio.h>#include <stdlib.h>#define N 5#define NULL 0//链表的存储结构typedef struct LNode{int data;struct LNode *next;}LNode,*list;//顺序创建链表void creatList(list &l,int n){int i;list p,q;l=(list)malloc(sizeof(LNode)); //开辟头结点p=l; //指针p指向头结点for(i=0;i<n;i++){q=(list)malloc(sizeof(LNode)); //新的结点scanf("%d",&q->data);p->next=q; //p的下一个结点指向新开辟的结点qp=q; //将p指针指向q}p->next=NULL;}//归并排序void mergeList(list &la,list &lb,list &lc){ //将已经排好序的la,lb中的数重新排列成有序(非递减)list pa,pb,pc;pa=la->next;pb=lb->next;lc=pc=la; //默认将la做为lc的头结点(lb亦可)while(pa&&pb){ //让pc接到数据小的结点上,直到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; //如果最后la有剩余结点,即将其直接加入到lc中,反之将lb的剩余结点加到lc中free(lb);}void printList(list l){list p;p=l->next;while(p){ printf("%d ",p->data);p=p->next;}}void main(){list la,lb,lc;printf("创建两个含%d个元素的链表,请输入:\n",N);creatList(la,N);creatList(lb,N);mergeList(la,lb,lc);printList(lc);}2. #include <stdio.h>#include <stdlib.h>#define N 5#define NULL 0#define OK 1#define ERROR 0//链表的存储结构typedef struct LNode{int data;struct LNode *next;}LNode,*list;//创建链表void creatList(list &l,int n){list p,q;l=(list)malloc(sizeof(LNode));p=l;for(int i=0;i<n;i++){q=(list)malloc(sizeof(LNode));scanf("%d",&q->data);p->next=q;}p->next=NULL;}//判断元素e是否在链表中int inList(list l,int e){list p;p=l->next;while(p){if(p->data==e)return OK; //发现在里面,返回真值p=p->next; //否则指针后移,继续找}return ERROR; //未找到,返回假值(没有执行return OK;语句)}//插入元素void insertList(list &l,int &e){list p,q,s; //q为新插入的元素开辟一个存储空间的指针,s为p前一个指针,方便插入p=l->next;s=l;while(p){if(e<=p->data){//发现要插入的元素e比后面的小,开辟空间,并将e放入空间的数据域中q=(list)malloc(sizeof(LNode));q->data=e;while(s->next!=p) s=s->next; //找到p前的一个指针q->next=p; // 画图好好理解--->s--->p--->s->next=q; // q--->break;}p=p->next;}}//输出链表void printList(list l){list p;while(p){ printf("%d ",p->data); p=p->next;}}void main(){list l;int e;printf("创建%d个元素的链表,请输入%d个元素:\n",N,N);creatList(l,N);printf("请输入要判断的元素:");scanf("%d",&e);if(inList(l,e))printf("YES ");else{insertList(l,e);printList(l);}}3. #include <stdio.h>#include <stdlib.h>#define N 5#define NULL 0#define OK 1#define ERROR 0//链表的存储结构typedef struct LNode{int data;struct LNode *next;}LNode,*list;//创建链表void creatList(list &l,int n){list p,q;l=(list)malloc(sizeof(LNode));p=l;for(int i=0;i<n;i++){q=(list)malloc(sizeof(LNode));scanf("%d",&q->data);p->next=q;p=q;}p->next=NULL;}//判断元素e是否在链表中int insertDeleteList(list l,int e){list p,q;p=l->next; q=l;while(p){if(p->data==e){while(q->next!=p) q=q->next; //找到p前一个结点,方便删除操作q->next=p->next; //删除结点pfree(p);return OK;} //发现在里面,返回真值p=p->next; //否则指针后移,继续找}return ERROR; //未找到,返回假值(没有执行return OK;语句)}//输出链表void printList(list l){list p;p=l->next;while(p){ printf("%d ",p->data); p=p->next;}}void main(){list l;int e;printf("创建%d个元素的链表,请输入%d个元素:\n",N,N);creatList(l,N);printf("请输入要判断的元素");scanf("%d",&e);if(!insertDeleteList(l,e))printf("NO ");elseprintList(l);}4. #include <stdio.h>#include <stdlib.h>#define N 5#define NULL 0#define OK 1#define ERROR 0//链表的存储结构typedef struct LNode{int data;struct LNode *next;}LNode,*list;//创建链表void creatList(list &l,int n){list p,q;l=(list)malloc(sizeof(LNode));p=l;for(int i=0;i<n;i++){q=(list)malloc(sizeof(LNode));scanf("%d",&q->data);p->next=q;p=q;}p->next=NULL;}//链表排序void sortList(list &l){list p,q,r; //p标记排序的轮数int chang; //用于交换结点中的数据p=l->next;while(p->next!=NULL){q=l->next; //每次比较从首结点开始while(q->next!=NULL){r=q->next;if(q->data>r->data) //发现前一个比后一个大,交换数据{ chang=q->data;q->data=r->data;r->data=chang; }q=q->next; //相邻间下一个比较}p=p->next; //下一轮比较}}//输出链表void printList(list l){list p;p=l->next;while(p){ printf("%d ",p->data); p=p->next;}}void main(){list l;printf("创建%d个元素的链表,请输入%d个元素:\n",N,N);creatList(l,N);sortList(l);printList(l);}5. #include <stdio.h>#include <stdlib.h>#define N 5#define NULL 0#define OK 1#define ERROR 0//链表的存储结构typedef struct LNode{int data;struct LNode *next;}LNode,*list;//创建链表void creatList(list &l,int n){list p,q;l=(list)malloc(sizeof(LNode));scanf("%d",&l->data); //头结点也添加元素,方便输出p=l;for(int i=1;i<n;i++){q=(list)malloc(sizeof(LNode));scanf("%d",&q->data);p->next=q;p=q;}p->next=l; //让最后一个p->next指针指向头结点,构成循环链表}//输出链表void printList(list l,int pos){list p,q;int i;p=l;for(i=1;i<pos-1;i++) p=p->next; //找到指定位置的前一个位置q=p->next;do{if(pos==1) {printf("%d ",p->data); p=p->next;} //如果指定位置为1,即按原样输出else {p=p->next; printf("%d ",p->data);} //不然,p先移到指定的位置,输出其数据}while(p->next!=q); //结束条件(p移到的下一个位置不是q,即不是最初的p,完成循环输出)}void main(){list l;int pos;printf("创建%d个元素的循环链表,请输入%d个元素:\n",N,N);creatList(l,N);printf("请指明从第几个位置输出循环链表中的元素:");scanf("%d",&pos);while(pos<=0||pos>N){printf("输入的位置不存在,请重新输入... ");scanf("%d",&pos);}printList(l,pos);}11#include <stdio.h>#include <stdlib.h>#define N 5#define NULL 0#define OK 1#define ERROR 0//链表的存储结构typedef struct LNode{int data;struct LNode *next;}LNode,*list;//创建链表void creatList(list &l,int n){list p,q;l=(list)malloc(sizeof(LNode));scanf("%d",&l->data); //头结点也添加元素,方便输出p=l;for(int i=1;i<n;i++){q=(list)malloc(sizeof(LNode));scanf("%d",&q->data);p->next=q;p=q;}p->next=l; //让最后一个p->next指针指向头结点,构成循环链表}//输出链表void printList(list l,int pos){list p,q;int i;for(i=1;i<pos-1;i++) p=p->next; //找到指定位置的前一个位置q=p->next;do{if(pos==1) {printf("%d ",p->data); p=p->next;} //如果指定位置为1,即按原样输出else {p=p->next; printf("%d ",p->data);} //不然,p先移到指定的位置,输出其数据}while(p->next!=q); //结束条件(p移到的下一个位置不是q,即不是最初的p,完成循环输出)}void main(){list l;int pos;printf("创建%d个元素的循环链表,请输入%d个元素:\n",N,N);creatList(l,N);printf("请指明从第几个位置输出循环链表中的元素:");scanf("%d",&pos);while(pos<=0||pos>N){printf("输入的位置不存在,请重新输入... ");scanf("%d",&pos);}printList(l,pos);}12#include <stdio.h>#include <stdlib.h>#define N 5#define NULL 0#define OK 1#define ERROR 0//链表的存储结构typedef struct LNode{int data;struct LNode *next;}LNode,*list;//创建链表void creatList(list &l,int n){l=(list)malloc(sizeof(LNode));p=l;for(int i=0;i<n;i++){q=(list)malloc(sizeof(LNode));scanf("%d",&q->data);p->next=q;p=q;}p->next=NULL;}//判断元素e是否在链表中int inList(list l,int e){list p,q;q=p=l->next;while(p){if(p->data==e)return OK; //发现在里面,返回真值p=p->next; //否则指针后移,继续找}//没有执行return OK;语句,说明未找到while(q->next!=p) q=q->next; //找到链尾p=(list)malloc(sizeof(LNode)); //为链尾重新开辟空间p->data=e; //接到链尾p->next=q->next;q->next=p;return ERROR; //未找到,返回假值}//输出链表void printList(list l){list p;p=l->next;while(p){ printf("%d ",p->data); p=p->next;}}void main(){list l;int e;printf("创建%d个元素的链表,请输入%d个元素:\n",N,N);creatList(l,N);printf("请输入要判断的元素:");scanf("%d",&e);if(inList(l,e))printf("YES ");elseprintList(l);}13#include <stdio.h>#include <stdlib.h>#define OK 1#define Error 0#define NULL 0#define maxSize 100//栈的存储结构typedef struct{int *base;int *top;int size;}stack;//栈的初始化(顺序存储)int initStack(stack &s){ //开辟maxSize大小的空间,base和top都指向基地址,同时判断是否开辟成功,不成功返回0if(!(s.base=s.top=(int*)malloc(maxSize*sizeof(int)))) return Error;s.size=maxSize; //栈的大小为maxSizereturn OK;}//进栈操作int push(stack &s,int e){*s.top=e; //先将元素e赋值给s.top所指的存储空间s.top++; //top指针上移return OK;}//出栈操作int pop(stack &s,int &e){if(s.base==s.top) return Error; //如果栈为空,返回0s.top--; //top指针先后移e=*s.top; //将其所指的元素值赋给e return e;}void main(){stack s;int n,e;printf("请输入要创建栈的元素的个数:");scanf("%d",&n);initStack(s);for(int i=0;i<n;i++){scanf("%d",&e);push(s,e);}while(s.base!=s.top){printf("%d ",pop(s,e));}}14#include <stdlib.h>#include <stdio.h>#include <stdio.h>#include <stdlib.h>#define stackincrement 8#define OK 1#define Error 0#define NULL 0#define maxSize 100//栈的存储结构typedef struct{char *base; //由于要存放括号,所以为char类型char *top;int size;}stack;//栈的初始化(顺序存储)int initStack(stack &s){ //注意开辟的空间为char类型if(!(s.base=s.top=(char*)malloc(maxSize*sizeof(char)))) return Error;s.size=maxSize; //栈的大小为maxSizereturn OK;}//进栈操作int push(stack &s,int e){*s.top=e; //先将元素e赋值给s.top所指的存储空间s.top++; //top指针上移return OK;}int isEmpty(stack s){return s.base==s.top?OK:Error;}//出栈操作char pop(stack &s,char &e){if(isEmpty(s)) return Error; //如果栈为空,返回0s.top--; //top指针先后移e=*s.top; //将其所指的元素值赋给ereturn e;}//括号匹配int match(){stack s;initStack(s);char ch[100],e;int flag=1,i=0 ,lenth; //flag用于标记,如果匹配,值为1,否则为0scanf("%c",&ch[i]);while(ch[i]!='\n') scanf("%c",&ch[++i]); //先将所有输入的括号存放在数组ch[]中lenth=i-1; //数组的长度,不包括'\n'i=0;push(s,ch[i]); //先将第一个括号压栈if(ch[i]==']'||ch[i]==')'||ch[i]=='}') flag=0; //如果第一个压入的是右括号,则肯定不匹配,flag=0else while(i<lenth)//||!emptystack(s){i++;char t;if(ch[i]==']'||ch[i]==')'||ch[i]=='}'){ t=pop(s,e); //弹出先前压入的元素,将后继输入的括号与先前压入的比较if((t!=ch[i]-1)&&(t!=ch[i]-2)) {flag=0;break;} //左右小括号与左右大括号的ASCII码都相差1,左右中括号相差2,如果不满足,则不匹配,直接退出循环}else push(s,ch[i]); //输入的是左括号,直接压入}if(!isEmpty(s)) flag=0; //通过不断的压栈和弹栈,如果最后栈不为空,则肯定是左括号多于右括号,不匹配return flag;}void main(){int result;printf("判断输入的各种括号是否匹配:\n");result=match();if(result) printf("括号匹配正确^_^\n");else printf("括号匹配错误*.*\n");}15#include "stdio.h"#include "stdlib.h"#define stackinitsize 100#define OK 1#define ERROR 0//二叉树的二叉链表存储结构typedef struct BiTNode{int data;struct BiTNode *lchild,*rchild; //左右孩子指针}BiTnode,*BiTree;int CreateBiTree(BiTree &T){//按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树。

相关文档
最新文档