数据结构笔试题答案.docx

上传人:b****5 文档编号:6012478 上传时间:2023-01-03 格式:DOCX 页数:65 大小:35.04KB
下载 相关 举报
数据结构笔试题答案.docx_第1页
第1页 / 共65页
数据结构笔试题答案.docx_第2页
第2页 / 共65页
数据结构笔试题答案.docx_第3页
第3页 / 共65页
数据结构笔试题答案.docx_第4页
第4页 / 共65页
数据结构笔试题答案.docx_第5页
第5页 / 共65页
点击查看更多>>
下载资源
资源描述

数据结构笔试题答案.docx

《数据结构笔试题答案.docx》由会员分享,可在线阅读,更多相关《数据结构笔试题答案.docx(65页珍藏版)》请在冰豆网上搜索。

数据结构笔试题答案.docx

数据结构笔试题答案

数据结构笔试题答案

一、选择题

1.C 

插入排序从后面插入的时候,只要把8和9交换一下就行了,遍历到前面都不再有任何操作。

 冒泡排序第一次循环把9沉到最后面,然后第二次循环发现没有任何交换操作,说明已经排好序了。

2.A

3.D

已知前序和后序不能唯一确定

4.B

5.D

二、填空题

1. 

(1)!

=null 

(2)p->next (3)r!

=null (4)r->datadata (5)r->next (6)p->next 

题中p指向无序区第一个记录,q指向最小值结点,一趟排序结束,p和q所指结点值交换,同时向后移p指针。

 

2.EACBDGF

3.

sort_b_tree(&((*tree)->right),s)

sort_b_tree(&((*tree)->left),s)

三、简答题

1.数组和链表的区别,请详细解释。

从逻辑结构来看:

a)数组必须事先定义固定的长度(元素个数),不能适应数据动态地增减的情况。

当数据增加时,可能超出原先定义的元素个数;当数据减少时,造成内存浪费;数组可以根据下标直接存取。

b)链表动态地进行存储分配,可以适应数据动态地增减的情况,且可以方便地插入、删除数据项。

(数组中插入、删除数据项时,需要移动其它数据项,非常繁琐)链表必须根据next指针找到下一个元素

从内存存储来看:

a)(静态)数组从栈中分配空间,对于程序员方便快速,但是自由度小

b)链表从堆中分配空间,自由度大但是申请管理比较麻烦

从上面的比较可以看出,如果需要快速访问数据,很少或不插入和删除元素,就应该用数组;相反,如果需要经常插入和删除元素就需要用链表数据结构了。

2.排序算法有哪些?

排序算法有很多,每种算法有不同的时间和空间复杂度,效率也有差别,那么针对使用上也有不同的场合。

原则上说,数据结构是一门领域,跟语言没有绝对的联系,很多时候同样的算法可以用很多种语言实现。

下面列一些常见的算法:

插入排序,冒泡排序,选择排序,快速排序,堆排序,归并排序,基数排序,希尔排序等。

3.怎么理解哈希表,哈希表是什么

摘自XX:

散列表(Hashtable,也叫哈希表),是根据关键码值(Keyvalue)而直接进行访问的数据结构。

也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。

这个映射函数叫做散列函数,存放记录的数组叫做散列表。

给定表M,存在函数f(key),对任意给定的关键字值key,代入函数后若能得到包含该关键字的记录在表中的地址,则称表M为哈希(Hash)表,函数f(key)为哈希(Hash)函数

4.请写出以下算法的时间复杂度

冒泡排序法插入排序法堆排序法二叉树排序法

O(n^2)O(n^2) O(nlog2n)最差O(n2)平均O(n*log2n)

快速排序法希尔排序法

最差O(n2)平均O(n*log2n)O(nlog n)不稳定

5.数据结构,二叉树的相关知识,开销量,为何使用二叉树等。

在计算机科学中,二叉树是每个结点最多有两个子树的有序树。

通常根的子树被称作“左子树”(leftsubtree)和“右子树”(rightsubtree)。

二叉树常被用作二叉查找树和二叉堆或是二叉排序树。

二叉树的每个结点至多只有二棵子树(不存在出度大于2的结点),二叉树的子树有左右之分,次序不能颠倒。

文件系统和数据库系统一般都采用树(特别是B树)的数据结构数据,主要为排序和检索的效率。

二叉树是一种最基本最典型的排序树,用于教学和研究树的特性,本身很少在实际中进行应用,因为缺点太明显了(看看教科书怎么说的)。

就像冒泡排序一样,虽然因为效率问题并不实用,单不失一种教学例子的好手段。

四、编程题

1.编写一个程序,把一个有序整数数组放在二叉树中。

#include

#include

#include

structstudent

{

intvalue;

structstudent*lchild;

structstudent*rchild;

};

voidarraytotree(int*a,intlen,structstudent**p)

{

if(len)

{

*p=(structstudent*)malloc(sizeof(structstudent));

(*p)->value=a[len/2];

arraytotree(a,len/2,&((*p)->lchild));

arraytotree(a+len/2+1,len-len/2-1,&((*p)->rchild));

}

else

{

*p=NULL;

}

}

voiddisplay_tree(structstudent*head)

{

if(head->lchild)display_tree(head->lchild);

printf("%d,",head->value);

if(head->rchild)display_tree(head->rchild);

}

intmain()

{

inta[]={1,2,3,4,9,10,33,56,78,90};

structstudent*tree;

arraytotree(a,sizeof(a)/sizeof(a[0]),&tree);

printf("Afterconvert:

\n");

display_tree(tree);

printf("\n");

return0;

}

2.在二叉查找树中查找某一个值所在的位置。

intfind_btree(btree*bt,datatypex)

{

if(bt){

if(bt->key==x)

returnTRUE;

if(!

find_btree(bt->lchild,x))

if(!

find_btree(bt->rchild,x))

returnFALSE;

}

}

3.二叉树,比父节点大的元素,都在右子树,比父节点小的元素都在左子树。

也就是排序二叉树,写出插入一个节点或者删除一个节点。

#include

#include

#include

#include

#defineTRUE1

#defineFALSE0

typedefintstatus;

typedefintdatatype;

typedefstructnode

{

datatypekey;

structnode*lchild,*rchild;

}btree;

voidbst_insert(btree**bt,datatypekey)//二叉排序树的插入

{

if(NULL==*bt)

{

btree*node=malloc(sizeof(btree));

node->key=key;

node->lchild=node->rchild=NULL;

*bt=node;

}

elseif(key>(*bt)->key)

{

bst_insert(&(*bt)->rchild,key);

}

else

{

bst_insert(&(*bt)->lchild,key);

}

}

statusbst_delete(btree**bt,datatypekey)//二叉排序树的删除

{

btree*tmp=NULL;

if(!

*bt)

{

returnFALSE;

}

else

{

if(key<(*bt)->key)//在左子树中找

{

bst_delete(&(*bt)->lchild,key);

}

elseif(key>(*bt)->key)//在右子树中找

{

bst_delete(&(*bt)->rchild,key);

}

else//找到了

{

tmp=*bt;

if(!

(*bt)->lchild)//左子树为空

{

*bt=(*bt)->rchild;

}

elseif(!

(*bt)->rchild)//右子树为空

{

*bt=(*bt)->lchild;

}

free(tmp);

returnTRUE;

}

}

}

voidpreorder_btree(btree*bt)//二叉树的先序遍历

{

if(bt!

=NULL){

printf("%d,",bt->key);

preorder_btree(bt->lchild);

preorder_btree(bt->rchild);

}

}

voidinorder_btree(btree*bt)//二叉树的中序遍历

{

if(bt!

=NULL){

inorder_btree(bt->lchild);

printf("%d,",bt->key);

inorder_btree(bt->rchild);

}

}

intmain(intargc,char*argv[])

{

btree*bt=NULL;

intn;

while

(1)

{

scanf("%d",&n);

if(n==0)//输入0则结束

{

break;

}

else

{

bst_insert(&bt,n);

}

}

preorder_btree(bt);

putchar('\n');

bst_delete(&bt,8);

preorder_btree(bt);

putchar('\n');

return0;

}

4.实现单向链表:

创建链表、插入链表、查询链表、删除特定序号链表节点、遍历剩余链表节点

简单的链表操作,这里假设链表无头结点。

可以自己完善有头结点的单向链表。

头文件:

/*list.h--headerfileforasimplelisttype*/

#ifndef__LIST_H_

#define__LIST_H_

#include

#include

#include

#include/*C99feature*/

#ifdef__cplusplus

extern"C"

#endif/*__cplusplus*/

/*program-specificdeclarations*/

typedefintdataType;

/*generaltypedefinitions*/

typedefdataTypeItem;

typedefstructnode

{

Itemitem;

structnode*next;

}Node;

typedefNode*List;

 

/*functionprototypes*/

voidListCreate(List*plist);

boolListIsEmpty(constList*plist);

boolListIsFull(constList*plist);

unsignedintListItemCount(constList*plist);

boolAddItem(Itemitem,List*plist);

voidEmptyTheList(List*plist);

voidTraverseTheList(List*plist);

unsignedintListItemSearch(constList*plist,constItemitem);

voiddeleteTheListNode(List*plist,unsignedintnum);

#ifdef__cplusplus

}

#endif/*__cplusplus*/

#endif/*__LIST_H_*/

.c文件

/*list.c--functionssupportinglistoperations*/

#include

#include

#include"list.h"

 

/*localfunctiondefinition*/

staticvoidCopyToNode(constItemitem,Node*pnode);

staticvoiddisplayTheNOde(constNode*pnode);

staticboolItemIsEqu(constItemItem,constNode*pnode);

 

/*initaemptylist*/

voidListCreate(List*plist)

{

*plist=NULL;

}

/*returnstrueiflistisempty*/

boolListIsEmpty(constList*plist)

{

if(*plist==NULL)

returntrue;

else

returnfalse;

}

/*returnstrueiflistisfull*/

boolListIsFull(constList*plist)

{

Node*pt;

boolfull;

pt=(Node*)malloc(sizeof(Node));

if(pt==NULL)

full=true;

else

full=false;

free(pt);

returnfull;

}

/*returnsnumberofnodes*/

unsignedintListItemCount(constList*plist)

{

unsignedintcount=0;

Node*pnode=*plist;

while(pnode!

=NULL)

{

++count;

pnode=pnode->next;

}

returncount;

}

/*searchiteminthelist*/

unsignedintListItemSearch(constList*plist,constItemitem)

{

intnum=0;

Node*pnode=*plist;

while(pnode!

=NULL)

{

num++;

if(ItemIsEqu(item,pnode))

returnnum;

pnode=pnode->next;

}

return0;

}

 

/*createsnodetoholditemandaddsittotheendoflist*/

boolAddItem(Itemitem,List*plist)

{

Node*pnew=NULL;

Node*scan=*plist;

pnew=(Node*)malloc(sizeof(Node));

if(pnew==NULL)

returnfalse;

CopyToNode(item,pnew);

pnew->next=NULL;

if(scan==NULL)/*emptylist,soplace*/

*plist=pnew;/*pnewatheadoflist*/

else

{

while(scan->next!

=NULL)

scan=scan->next;/*findendoflist*/

scan->next=pnew;/*addpnewtoend*/

}

returntrue;

}

/*deletethenodebynum*/

voiddeleteTheListNode(List*plist,unsignedintnum)

{

Node*pnode=*plist;

Node*psave=NULL;

if(ListIsEmpty(plist))

return;

if(1==num)

{

psave=*plist;

*plist=psave->next;

free(psave);

return;

}

--num;

while(pnode->next!

=NULL&&num>1)

{

--num;

pnode=pnode->next;

}

if(pnode->next!

=NULL&&1==num)

{

psave=pnode->next;

pnode->next=psave->next;

free(psave);

}

}

/*TraverseTheList*/

voidTraverseTheList(List*plist)

{

Node*pnode=*plist;

while(pnode!

=NULL)

{

displayTheNOde(pnode);

pnode=pnode->next;

}

putchar('\n');

}

 

/*freememoryallocatedbymalloc()*/

voidEmptyTheList(List*plist)

{

Node*psave=NULL;

while(*plist!

=NULL)

{

psave=(*plist)->next;/*saveaddressofnextnode*/

free(*plist);/*freecurrentnode*/

*plist=psave;/*advancetonextnode*/

}

}

/*localfunctiondefinition*/

/*copiesanitemintoanode*/

staticvoidCopyToNode(constItemitem,Node*pnode)

{

pnode->item=item;/*structurecopy*/

}

 

/*displayanodedata*/

staticvoiddisplayTheNOde(constNode*pnode)

{

printf("%d",pnode->item);

}

/*Todeterminethenode*/

staticboolItemIsEqu(constItemItem,constNode*pnode)

{

returnItem==pnode->item?

true:

false;

}

5.编程实现判断一个链表是否是递增的

假设链表无头结点.若链表为空默认为递增。

思路之前结点的数据与之后的数据依次比较

typedefintdataType;

/*generaltypedefinitions*/

typedefdataTypeItem;

typedefstructnode

{

Itemitem;

structnode*next;

}Node;

typedefNode*List;

boollistItemIsIncrease(List*plist)

{

Node*preptr=*plist;

Node*curptr=NULL;

if(NULL==*plist)

returntrue;

curptr=preptr->next;

while(curptr!

=NULL)

{

if(preptr->item>curptr->item)/*如果是严格递增,则判断用>=*/

break;

preptr=curptr;

curptr=curptr->next;

}

return(curptr==NULL)?

true:

false;

}

6.编程实现删除链表中的重复节点

假设链表无头结点,挨个遍历链表数据,与之后的结点依次比较,发现重复的就删除

typedefintdataType;

/*generaltypedefinitions*/

typedefdataTypeItem;

typedefstructnode

{

Itemitem;

structnode*next;

}Node;

typedefNode*List;

voiddelRepeatedNodeOnce(List*plist)

{

Node*p=*plist,*q,*u,*y;

while(p)

{

q=p->next;

u=p;

while(q)

{

if(q->item==p->item)

{

u->next=q->next;y=q;

q=q->next;

free(y);

}

else

{

u=q;

q=q->next;

}

}

p=p->next;

}

}

7.只遍历一次链表,实现链表的倒序

假设链表无头节点,链表头插入,直到遍历到链表末尾

typedefintdataType;

/*generaltypedefinitions*/

typedefdataTypeItem;

typedefstructnode

{

Itemitem;

structnode*next;

}Node;

typedefN

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1