数据结构与应用教程(C++版,马石安)课后习题参考答案
数据结构(c )课后习题答案

数据结构(c )课后习题答案数据结构(C)课后习题答案在学习数据结构(C)课程时,课后习题是非常重要的一部分。
通过完成这些习题,我们可以加深对数据结构的理解,提高编程能力,并且更好地掌握C语言的运用。
下面我们就来看看一些常见的数据结构(C)课后习题答案。
1. 链表的创建和遍历链表是数据结构中常见的一种,它由一系列节点组成,每个节点包含数据和指向下一个节点的指针。
在C语言中,我们可以通过动态内存分配来创建链表,并且可以使用指针来进行遍历。
下面是一个简单的链表创建和遍历的示例代码:```c#include <stdio.h>#include <stdlib.h>struct Node{int data;struct Node *next;};void printList(struct Node *node){while (node != NULL){printf("%d ", node->data);node = node->next;}}int main(){struct Node *head = NULL;struct Node *second = NULL;struct Node *third = NULL;head = (struct Node *)malloc(sizeof(struct Node)); second = (struct Node *)malloc(sizeof(struct Node)); third = (struct Node *)malloc(sizeof(struct Node)); head->data = 1;head->next = second;second->data = 2;second->next = third;third->data = 3;third->next = NULL;printList(head);return 0;}```2. 栈的实现栈是一种后进先出(LIFO)的数据结构,可以使用数组或链表来实现。
数据结构与应用教程(C++版,马石安)课后习题参考答案

1.名词解释数据:信息的载体,在计算科学中指所有能被输入到计算机中并能被计算机程序识别和处理的符号集合。
数据元素:数据的基本单位,有时也称为元素,节点,顶点,记录。
数据结构:是指数据元素之间的相互关系,即数据的组织形式。
逻辑结构:是从逻辑关系上描述数据,是面向问题的,不涉及数据在计算机的存储,独立于计算机的。
包括线性结构和非线性结构两大类。
存储结构:又称为物理结构,是指数据在计算机内的表示方法,是逻辑结构的具体实现。
包括顺序存储,链接存储,索引存储,散列存储四大类。
线性结构:有且仅有一个开始结点和一个终端结点,并且所有的节点都最多只有一个直接前驱和一个直接后继。
非线性结构:是一个结点可能有多个直接前驱和直接后继。
2.填空题1.线性结构非线性结构2.顺序存储链接存储索引存储散列存储3.一对一一对多多对多4.对数据施加的操作(检索.插入.删除.更新.排序)5.算法6.算法的时间复杂度算法的空间复杂度7. O(n*n) O(1)8. O(nlog2(n))9. O(n)10.O(n*n)三.选择题1-5 D C A C A 6-10 B D D D B四.简答题1.逻辑结构:是从逻辑关系上描述数据,是面向问题的,不涉及数据在计算机的存储,独立于计算机的。
包括线性结构和非线性结构两大类。
存储结构:又称为物理结构,是指数据在计算机内的表示方法,是逻辑结构的具体实现。
包括顺序存储,链接存储,索引存储,散列存储四大类。
运算:是对数据的施加的操作。
包括检索,插入,删除,更新,排序等。
2.线性结构有且仅有一个开始结点和一个终端结点,并且所有的节点都最多只有一个直接前驱和一个直接后继。
非线性结构是一个结点可能有多个直接前驱和直接后继。
3.逻辑结构:是从逻辑关系上描述数据,是面向问题的,不涉及数据在计算机的存储,独立于计算机的。
包括线性结构和非线性结构两大类。
存储结构:又称为物理结构,是指数据在计算机内的表示方法,是逻辑结构的具体实现。
数据结构(c语言版)课后习题答案完整版

数据结构(c语言版)课后习题答案完整版数据结构(C语言版)课后习题答案完整版一、数据结构概述数据结构是计算机科学中一个重要的概念,用来组织和存储数据,使之可以高效地访问和操作。
在C语言中,我们可以使用不同的数据结构来解决各种问题。
本文将提供完整版本的C语言数据结构的课后习题答案。
二、顺序表1. 顺序表的定义和基本操作顺序表是一种线性表,其中的元素在物理内存中连续地存储。
在C 语言中,我们可以通过定义结构体和使用指针来实现顺序表。
以下是顺序表的一些基本操作的答案:(1)初始化顺序表```ctypedef struct{int data[MAX_SIZE];int length;} SeqList;void InitList(SeqList *L){L->length = 0;}```(2)插入元素到顺序表中```cbool Insert(SeqList *L, int pos, int elem){if(L->length == MAX_SIZE){return false; // 顺序表已满}if(pos < 1 || pos > L->length + 1){return false; // 位置不合法}for(int i = L->length; i >= pos; i--){L->data[i] = L->data[i-1]; // 向后移动元素 }L->data[pos-1] = elem;L->length++;return true;}```(3)删除顺序表中的元素```cbool Delete(SeqList *L, int pos){if(pos < 1 || pos > L->length){return false; // 位置不合法}for(int i = pos; i < L->length; i++){L->data[i-1] = L->data[i]; // 向前移动元素 }L->length--;return true;}```(4)查找顺序表中的元素```cint Search(SeqList L, int elem){for(int i = 0; i < L.length; i++){if(L.data[i] == elem){return i + 1; // 找到元素,返回位置 }}return -1; // 未找到元素}```2. 顺序表习题解答(1)逆置顺序表```cvoid Reverse(SeqList *L){for(int i = 0; i < L->length / 2; i++){int temp = L->data[i];L->data[i] = L->data[L->length - 1 - i]; L->data[L->length - 1 - i] = temp;}}```(2)顺序表元素去重```cvoid RemoveDuplicates(SeqList *L){for(int i = 0; i < L->length; i++){for(int j = i + 1; j < L->length; j++){if(L->data[i] == L->data[j]){Delete(L, j + 1);j--;}}}}```三、链表1. 单链表单链表是一种常见的链式存储结构,每个节点包含数据和指向下一个节点的指针。
数据结构c 版第二版课后习题答案

数据结构c 版第二版课后习题答案数据结构是计算机科学中的重要概念,它研究如何组织和存储数据,以便能够高效地进行操作和检索。
C语言是一种广泛应用于软件开发的编程语言,而数据结构C版第二版是一本经典的教材,它介绍了C语言中常用的数据结构和算法。
在学习这本教材时,课后习题是检验自己理解和掌握程度的重要方式。
下面我将为大家提供一些课后习题的答案,希望对大家的学习有所帮助。
1. 第一章:引论习题1:数据结构是什么?它的作用是什么?答案:数据结构是一种组织和存储数据的方式,它可以帮助我们更高效地进行数据操作和检索。
它的作用是提供一种合理的数据组织方式,使得我们可以快速地找到和处理需要的数据。
习题2:请举例说明数据结构的应用场景。
答案:数据结构可以应用于各个领域,比如在图像处理中,我们可以使用二维数组来表示图像的像素点;在网络通信中,我们可以使用链表来存储和管理网络节点之间的连接关系;在数据库中,我们可以使用树结构来组织数据的层次关系等等。
2. 第二章:算法分析习题1:什么是时间复杂度和空间复杂度?它们分别表示什么?答案:时间复杂度是衡量算法执行时间的度量,它表示随着输入规模的增加,算法执行时间的增长趋势。
空间复杂度是衡量算法所需内存空间的度量,它表示随着输入规模的增加,算法所需内存空间的增长趋势。
习题2:请解释最坏情况时间复杂度和平均情况时间复杂度的区别。
答案:最坏情况时间复杂度是指在最不利的情况下,算法执行的时间复杂度。
平均情况时间复杂度是指在所有可能输入情况下,算法执行的平均时间复杂度。
最坏情况时间复杂度是对算法性能的保证,而平均情况时间复杂度更能反映算法的平均性能。
3. 第三章:线性表习题1:请实现一个线性表的顺序存储结构。
答案:可以使用数组来实现线性表的顺序存储结构。
定义一个固定大小的数组,然后使用一个变量来记录线性表中元素的个数,通过数组下标来访问和操作元素。
习题2:请实现一个线性表的链式存储结构。
数据结构c 版习题答案

数据结构c 版习题答案数据结构是计算机科学中的重要基础课程,它主要研究数据的组织、存储和管理方式。
C语言是一种常用的编程语言,也是学习数据结构的重要工具。
在学习数据结构的过程中,习题是不可或缺的一部分,通过解答习题可以帮助我们巩固所学的知识。
下面是一些常见的数据结构C版习题的答案,供大家参考。
一、线性表1. 实现一个顺序表的插入操作。
答案:```cvoid insert(SeqList *list, int index, int value) {if (index < 0 || index > list->length) {printf("插入位置不合法\n");return;}if (list->length >= MAX_SIZE) {printf("顺序表已满\n");return;}for (int i = list->length - 1; i >= index; i--) {list->data[i + 1] = list->data[i];}list->data[index] = value;list->length++;}```2. 实现一个顺序表的删除操作。
答案:```cvoid remove(SeqList *list, int index) {if (index < 0 || index >= list->length) {printf("删除位置不合法\n");return;}for (int i = index; i < list->length - 1; i++) { list->data[i] = list->data[i + 1];}list->length--;}```二、栈和队列1. 实现一个栈的入栈操作。
数据结构(C语言版)课后习题答案

第1章绪论1.简述下列概念:数据、数据元素、数据项、数据对象、数据结构、逻辑结构、存储结构、抽象数据类型。
答案:数据:是客观事物的符号表示,指所有能输入到计算机中并被计算机程序处理的符号的总称。
如数学计算中用到的整数和实数,文本编辑所用到的字符串,多媒体程序处理的图形、图像、声音、动画等通过特殊编码定义后的数据。
数据元素:是数据的基本单位,在计算机中通常作为一个整体进行考虑和处理。
在有些情况下,数据元素也称为元素、结点、记录等。
数据元素用于完整地描述一个对象,如一个学生记录,树中棋盘的一个格局(状态)、图中的一个顶点等。
数据项:是组成数据元素的、有独立含义的、不可分割的最小单位。
例如,学生基本信息表中的学号、姓名、性别等都是数据项。
数据对象:是性质相同的数据元素的集合,是数据的一个子集。
例如:整数数据对象是集合N={0,±1,±2,…},字母字符数据对象是集合C={‘A’,‘B’,…,‘Z’,‘a’,‘b’,…,‘z’},学生基本信息表也可是一个数据对象。
数据结构:是相互之间存在一种或多种特定关系的数据元素的集合。
换句话说,数据结构是带“结构”的数据元素的集合,“结构”就是指数据元素之间存在的关系。
逻辑结构:从逻辑关系上描述数据,它与数据的存储无关,是独立于计算机的。
因此,数据的逻辑结构可以看作是从具体问题抽象出来的数学模型。
存储结构:数据对象在计算机中的存储表示,也称为物理结构。
抽象数据类型:由用户定义的,表示应用问题的数学模型,以及定义在这个模型上的一组操作的总称。
具体包括三部分:数据对象、数据对象上关系的集合和对数据对象的基本操作的集合。
2.试举一个数据结构的例子,叙述其逻辑结构和存储结构两方面的含义和相互关系。
答案:例如有一张学生基本信息表,包括学生的学号、姓名、性别、籍贯、专业等。
每个学生基本信息记录对应一个数据元素,学生记录按顺序号排列,形成了学生基本信息记录的线性序列。
数据结构C语言版习题详细答案资料

InitComplex(&C,re,im) 操作结果:构造一个复数 C,其实部和虚部分别为 re 和 im
DestroyCmoplex(&C) 操作结果:销毁复数 C
Get(C,k,&e) 操作结果:用 e 返回复数 C 的第 k 元的值
int Time(int n) { count = 0; x=2; while(x<n/2) { x *= 2; count++; } return count;
}
解: o(log 2 n)
count= log 2 n 2
1.11 已知有实现同一功能的两个算法,其时间复杂度分别为 O 2n 和 O n10 ,假设现实计算机可连续
1.14 判断下列各对函数 f n 和 g n ,当 n 时,哪个函数增长更快?
(1) f n 10n2 ln n!10n3 , gn 2n4 n 7
1.6 在程序设计中,常用下列三种不同的出错处理方式: (1) 用 exit 语句终止执行并报告错误; (2) 以函数的返回值区别正确返回或错误返回; (3) 设置一个整型变量的函数参数以区别正确返回或某种错误返回。
试讨论这三种方法各自的优缺点。 解:(1)exit 常用于异常错误处理,它可以强行中断程序的执行,返回操作系统。 (2)以函数的返回值判断正确与否常用于子程序的测试,便于实现程序的局部控制。 (3)用整型函数进行错误处理的优点是可以给出错误类型,便于迅速确定错误。
} 解:(1) n-1 (2) n-1 (3) n-1
n(n 1)
(4) n+(n-1)+(n-2)+...+1=
数据结构c语言版课后习题答案

数据结构c语言版课后习题答案数据结构是计算机科学中的一个重要概念,它涉及到组织、管理和存储数据的方式,以便可以有效地访问和修改数据。
C语言是一种广泛使用的编程语言,它提供了丰富的数据结构实现方式。
对于学习数据结构的C语言版课程,课后习题是巩固理论知识和提高实践能力的重要手段。
数据结构C语言版课后习题答案1. 单链表的实现在C语言中,单链表是一种常见的线性数据结构。
它由一系列节点组成,每个节点包含数据部分和指向下一个节点的指针。
实现单链表的基本操作通常包括创建链表、插入节点、删除节点、遍历链表等。
答案:- 创建链表:定义一个链表结构体,然后使用动态内存分配为每个节点分配内存。
- 插入节点:根据插入位置,调整前后节点的指针,并将新节点插入到链表中。
- 删除节点:找到要删除的节点,调整其前后节点的指针,然后释放该节点的内存。
- 遍历链表:从头节点开始,使用指针遍历链表,直到达到链表尾部。
2. 二叉树的遍历二叉树是一种特殊的树形数据结构,其中每个节点最多有两个子节点。
二叉树的遍历是数据结构中的一个重要概念,常见的遍历方式有前序遍历、中序遍历、后序遍历和层序遍历。
答案:- 前序遍历:先访问根节点,然后递归遍历左子树,最后递归遍历右子树。
- 中序遍历:先递归遍历左子树,然后访问根节点,最后递归遍历右子树。
- 后序遍历:先递归遍历左子树,然后递归遍历右子树,最后访问根节点。
- 层序遍历:使用队列,按照从上到下,从左到右的顺序访问每个节点。
3. 哈希表的实现哈希表是一种通过哈希函数将键映射到表中一个位置来访问记录的数据结构。
它提供了快速的数据访问能力,但需要处理哈希冲突。
答案:- 哈希函数:设计一个哈希函数,将键映射到哈希表的索引。
- 哈希冲突:使用链地址法、开放地址法或双重哈希法等解决冲突。
- 插入操作:计算键的哈希值,将其插入到对应的哈希桶中。
- 删除操作:找到键对应的哈希桶,删除相应的键值对。
4. 图的表示和遍历图是一种复杂的非线性数据结构,由顶点(节点)和边组成。
数据结构(C++版)课后作业1-6章带答案

第1 章绪论课后习题讲解1. 填空(1) 从逻辑关系上讲,数据结构主要分为()、()、()和()。
(2) 数据的存储结构主要有()和()两种基本方法,不论哪种存储结构,都要存储两方面的内容:()和()。
(3)算法在发生非法操作时可以作出处理的特性称为()。
2. 选择题⑴顺序存储结构中数据元素之间的逻辑关系是由()表示的,链接存储结构中的数据元素之间的逻辑关系是由()表示的。
A 线性结构B 非线性结构C 存储位置D 指针⑵假设有如下遗产继承规则:丈夫和妻子可以相互继承遗产;子女可以继承父亲或母亲的遗产;子女间不能相互继承。
则表示该遗产继承关系的最合适的数据结构应该是()。
A 树B 图C 线性表D 集合3. 判断题(1) 每种数据结构都具备三个基本操作:插入、删除和查找。
第2 章线性表课后习题讲解1. 填空⑵顺序表中第一个元素的存储地址是100,每个元素的长度为2,则第5个元素的存储地址是()。
第5个元素的存储地址=第1个元素的存储地址+(5-1)×2=108⑶设单链表中指针p 指向结点A,若要删除A的后继结点(假设A存在后继结点),则需修改指针的操作为()。
【解答】p->next=(p->next)->next⑸非空的单循环链表由头指针head指示,则其尾结点(由指针p所指)满足()。
p->next=head⑹在由尾指针rear指示的单循环链表中,在表尾插入一个结点s的操作序列是();删除开始结点的操作序列为()。
【解答】s->next =rear->next; rear->next =s; rear =s; q=rear->next->next; rear->next->next=q->next; delete q;2. 选择题⑴线性表的顺序存储结构是一种()的存储结构,线性表的链接存储结构是一种()的存储结构。
A 随机存取B 顺序存取C 索引存取D 散列存取【解答】A,B 【分析】参见2.2.1。
数据结构c 版课后习题解析

【解答】自然语言,程序设计语言,流程图,伪代码,伪代码
(7)在一般情况下,一个算法的时间复杂度是()的函数。
【解答】问题规模
(8)设待处理问题的规模为n,若一个算法的时间复杂度为一个常数,则表示成数量级的形式为(),若为2n*log25n+ 8n,则表示成数量级的形式为()。
1.知识结构图
本章的知识结构如图2-1所示,其中第二层的椭圆代表本章的学习主线。
2. 学习要点
本章虽然讨论的是线性表,但涉及的许多问题都具有一定的普遍性,因此,本章是本课程的重点之一,也是其它后续章节的重要基础。
对于本章的学习要从两条明线、一条暗线出发。两条明线是线性表的逻辑结构和存储结构,一条暗线是算法(即基本操作的实现)。注意线性表的ADT定义、顺序表类定义和单链表类定义三者之间的关系;注意在不同的存储结构下,相同操作的不同实现算法;注意对顺序表和链表从时间性能和空间性能等方面进行综合对比,在实际应用中能为线性表选择或设计合适的存储结构。
【分析】操作示意图如图2-9所示:
⑺一个具有n个结点的单链表,在指针p所指结点后插入一个新结点的时间复杂度为();在给定值为x的结点后插入一个新结点的时间复杂度为()。
【解答】Ο(1),Ο(n)
【分析】在p所指结点后插入一个新结点只需修改指针,所以时间复杂度为Ο(1);而在给定值为x的结点后插入一个新结点需要先查找值为x的结点,所以时间复杂度为Ο(n)。
(5)可以用()定义一个完整的数据结构。
A数据元素B数据对象C数据关系D抽象数据类型
【解答】D
【分析】抽象数据类型是一个数据结构以及定义在该结构上的一组操作的总称。
数据结构(C语言版)(第2版)课后习题答案

数据结构(C语言版)(第2版)课后习题答案数据结构(C语言版)(第2版)课后习题答案1. 简介数据结构是计算机科学领域中非常重要的一门学科,它研究的是数据的组织、存储和管理方式。
本文将针对《数据结构(C语言版)(第2版)》的课后习题提供答案,帮助读者更好地理解和应用数据结构。
2. 第一章: 绪论在第一章中,主要介绍了数据结构的基本概念、分类和基本操作。
以下是部分习题的答案:2.1 习题1习题描述:什么是数据结构?答案:数据结构是指数据对象中元素之间的关系,以及对这些关系进行操作的方法和技术的集合。
2.2 习题2习题描述:数据结构的分类有哪些?答案:数据结构可以分为线性结构和非线性结构。
线性结构包括线性表、栈、队列等;非线性结构包括树、图等。
3. 第二章: 线性表第二章介绍了线性表的定义、分类和实现。
以下是部分习题的答案:3.1 习题1习题描述:什么是线性表?答案:线性表是由n个数据元素a1, a2, ..., an组成的有限序列,其中元素之间存在着一一对应的关系。
3.2 习题2习题描述:线性表的分类有哪些?答案:线性表可以分为顺序表和链表。
顺序表是用一段地址连续的存储单元一次存储线性表的所有元素,而链表是采用链式存储结构,通过每个元素存储其后继元素的地址来实现元素之间的逻辑关系。
4. 第三章: 栈与队列第三章讲解了栈和队列的定义、特性和实现。
以下是部分习题的答案:4.1 习题1习题描述:栈和队列有什么区别?答案:栈是一种后进先出的线性表,只能在表尾进行插入和删除操作;队列是一种先进先出的线性表,只能在表的一端进行插入和删除操作。
4.2 习题2习题描述:栈的应用有哪些?答案:栈在计算机科学中有广泛的应用,如函数的调用和返回、括号匹配、表达式求值等。
5. 第四章: 串第四章讲解了串的定义、模式匹配和实现。
以下是部分习题的答案:5.1 习题1习题描述:什么是串?答案:串是由零个或多个字符组成的有限序列,串中的字符个数称为串的长度。
数据结构(c版)第二版课后习题答案

数据结构(c版)第二版课后习题答案在计算机科学中,数据结构是组织、管理和存储数据的方式,它对程序的效率和性能有着直接的影响。
《数据结构(C版)》第二版是一本广泛使用的教材,它详细介绍了各种基本的数据结构以及它们在C语言中的实现。
课后习题是帮助学生巩固理论知识和提高编程技能的重要部分。
以下是一些可能的习题答案示例,但请注意,实际的习题答案应根据具体习题内容来确定。
习题1:数组和链表问题:编写一个函数,该函数接受一个整数数组和它的大小,然后返回数组中的最大值。
答案:```c#include <stdio.h>int findMax(int arr[], int size) {int max = arr[0];for (int i = 1; i < size; i++) {if (arr[i] > max) {max = arr[i];}}return max;}int main() {int arr[] = {10, 20, 30, 40, 50};int size = sizeof(arr) / sizeof(arr[0]);printf("The maximum value in the array is: %d\n", findMax(arr, size));return 0;}```习题2:栈问题:实现一个栈的数据结构,并编写一个函数来检查一个字符串是否是回文。
答案:```c#include <stdio.h>#include <string.h>#include <stdbool.h>#define MAX_SIZE 100typedef struct {char data[MAX_SIZE];int top;} Stack;void initializeStack(Stack *s) {s->top = -1;}bool isEmpty(Stack s) {return s.top == -1;}void push(Stack *s, char item) {if (s->top < MAX_SIZE - 1) {s->top++;s->data[s->top] = item;}}char pop(Stack *s) {if (!isEmpty(*s)) {return s->data[s->top--];}return '\0';}bool isPalindrome(const char *str) {Stack s;initializeStack(&s);int length = strlen(str);for (int i = 0; i < length; i++) {push(&s, str[i]);}for (int i = 0; i < length / 2; i++) { char c1 = str[i];char c2 = pop(&s);if (c1 != c2) {return false;}}return true;}int main() {char str[] = "radar";if (isPalindrome(str)) {printf("The string is a palindrome.\n");} else {printf("The string is not a palindrome.\n");}return 0;}```习题3:队列问题:实现一个循环队列,并编写一个函数来模拟银行的顾客服务,顾客到达和离开。
数据结构C语言版部分习题及标准答案

数据结构C语言版部分习题及标准答案————————————————————————————————作者:————————————————————————————————日期:第二章习题与解答一判断题1.线性表的逻辑顺序与存储顺序总是一致的。
2.顺序存储的线性表可以按序号随机存取。
3.顺序表的插入和删除操作不需要付出很大的时间代价,因为每次操作平均只有近一半的元素需要移动。
4.线性表中的元素可以是各种各样的,但同一线性表中的数据元素具有相同的特性,因此是属于同一数据对象。
5.在线性表的顺序存储结构中,逻辑上相邻的两个元素在物理位置上并不一定紧邻。
6.在线性表的链式存储结构中,逻辑上相邻的元素在物理位置上不一定相邻。
7.线性表的链式存储结构优于顺序存储结构。
8.在线性表的顺序存储结构中,插入和删除时,移动元素的个数与该元素的位置有关。
9.线性表的链式存储结构是用一组任意的存储单元来存储线性表中数据元素的。
10.在单链表中,要取得某个元素,只要知道该元素的指针即可,因此,单链表是随机存取的存储结构。
二单选题 (请从下列A,B,C,D选项中选择一项)1.线性表是( ) 。
(A) 一个有限序列,可以为空;(B) 一个有限序列,不能为空;(C) 一个无限序列,可以为空;(D) 一个无序序列,不能为空。
2.对顺序存储的线性表,设其长度为n,在任何位置上插入或删除操作都是等概率的。
插入一个元素时平均要移动表中的()个元素。
(A) n/2 (B) n+1/2 (C) n -1/2 (D) n3.线性表采用链式存储时,其地址( ) 。
(A) 必须是连续的;(B) 部分地址必须是连续的;(C) 一定是不连续的;(D) 连续与否均可以。
4.用链表表示线性表的优点是()。
(A)便于随机存取(B)花费的存储空间较顺序存储少(C)便于插入和删除(D)数据元素的物理顺序与逻辑顺序相同5.某链表中最常用的操作是在最后一个元素之后插入一个元素和删除最后一个元素,则采用( )存储方式最节省运算时间。
数据结构(C语言版)(第2版)课后习题答案

精品文档考试教学资料施工组织设计方案数据结构(C语言版)(第2版)课后习题答案李冬梅2015.3目录第1章绪论 (1)第2章线性表 (5)第3章栈和队列 (13)第4章串、数组和广义表 (26)第5章树和二叉树 (33)第6章图 (42)第7章查找 (54)第8章排序 (65)第1章绪论1.简述下列概念:数据、数据元素、数据项、数据对象、数据结构、逻辑结构、存储结构、抽象数据类型。
答案:数据:是客观事物的符号表示,指所有能输入到计算机中并被计算机程序处理的符号的总称。
如数学计算中用到的整数和实数,文本编辑所用到的字符串,多媒体程序处理的图形、图像、声音、动画等通过特殊编码定义后的数据。
数据元素:是数据的基本单位,在计算机中通常作为一个整体进行考虑和处理。
在有些情况下,数据元素也称为元素、结点、记录等。
数据元素用于完整地描述一个对象,如一个学生记录,树中棋盘的一个格局(状态)、图中的一个顶点等。
数据项:是组成数据元素的、有独立含义的、不可分割的最小单位。
例如,学生基本信息表中的学号、姓名、性别等都是数据项。
数据对象:是性质相同的数据元素的集合,是数据的一个子集。
例如:整数数据对象是集合N={0,±1,±2,…},字母字符数据对象是集合C={‘A’,‘B’,…,‘Z’,‘a’,‘b’,…,‘z’},学生基本信息表也可是一个数据对象。
数据结构:是相互之间存在一种或多种特定关系的数据元素的集合。
换句话说,数据结构是带“结构”的数据元素的集合,“结构”就是指数据元素之间存在的关系。
逻辑结构:从逻辑关系上描述数据,它与数据的存储无关,是独立于计算机的。
因此,数据的逻辑结构可以看作是从具体问题抽象出来的数学模型。
存储结构:数据对象在计算机中的存储表示,也称为物理结构。
抽象数据类型:由用户定义的,表示应用问题的数学模型,以及定义在这个模型上的一组操作的总称。
具体包括三部分:数据对象、数据对象上关系的集合和对数据对象的基本操作的集合。
数据结构与算法分析C版答案

Data Structures and Algorithm 习题答案Preface ii1 Data Structures and Algorithms 12 Mathematical Preliminaries 53 Algorithm Analysis 174 Lists, Stacks, and Queues 235 Binary Trees 326 General Trees 407 Internal Sorting 468 File Processing and External Sorting 549Searching 5810 Indexing 6411 Graphs 6912 Lists and Arrays Revisited 7613 Advanced Tree Structures 82iii Contents14 Analysis Techniques 8815 Limits to Computation 94PrefaceContained herein are the solutions to all exercises from the textbook A Practical Introduction to Data Structures and Algorithm Analysis, 2nd edition.For most of the problems requiring an algorithm I have given actual code. Ina few cases I have presented pseudocode. Please be aware that the code presented in this manual has not actually been compiled and tested. While I believe the algorithmsto be essentially correct, there may be errors in syntax as well as semantics. Most importantly, these solutions provide a guide to the instructor as to the intendedanswer, rather than usable programs.1Data Structures and AlgorithmsInstructor’s note: Unlike the other chapters, many of the questions in this chapter are not really suitable for graded work. The questions are mainly intended to get students thinking about data structures issues.This question does not have a specific right answer, provided the student keeps to the spirit of the question. Students may have trouble with the concept of “operations.”This exercise asks the student to expand on their concept of an integer representation.A good answer is described by Project , where a singly-linkedlist is suggested. The most straightforward implementation stores each digitin its own list node, with digits stored in reverse order. Addition and multiplicationare implemented by what amounts to grade-school arithmetic. Foraddition, simply march down in parallel through the two lists representingthe operands, at each digit appending to a new list the appropriate partial sum and bringing forward a carry bit as necessary. For multiplication, combine the addition function with a new function that multiplies a single digitby an integer. Exponentiation can be done either by repeated multiplication (not really practical) or by the traditional Θ(log n)-time algorithm based on the binary representation of the exponent. Discovering this faster algorithm will be beyond the reach of most students, so should not be required.A sample ADT for character strings might look as follows (with the normal interpretation of the function names assumed).Chap. 1 Data Structures and AlgorithmsSomeIn C++, this is 1for s1<s2; 0 for s1=s2;int strcmp(String s1, String s2)One’s compliment stores the binary representation of positive numbers, and stores the binary representation of a negative number with the bits inverted. Two’s compliment is the same, except t hat a negative number has its bits inverted and then one is added (for reasons of efficiency in hardware implementation).This representation is the physical implementation of an ADTdefined by the normal arithmetic operations, declarations, and other support given by the programming language for integers.An ADT for two-dimensional arrays might look as follows.Matrix add(Matrix M1, Matrix M2);Matrix multiply(Matrix M1, Matrix M2);Matrix transpose(Matrix M1);void setvalue(Matrix M1, int row, int col, int val);int getvalue(Matrix M1, int row, int col);List getrow(Matrix M1, int row);One implementation for the sparse matrix is described in Section Another implementationis a hash table whose search key is a concatenation of the matrix coordinates.Every problem certainly does not have an algorithm. As discussed in Chapter 15,there are a number of reasons why this might be the case. Some problems don’t have a sufficiently clear definition. Some problems, such as the halting problem, are non-computable. For some problems, such as one typically studied by artificial intelligence researchers, we simply don’t know a solution.We must assume that by “algorithm” we mean something composed of steps areof a nature that they can be performed by a computer. If so, than any algorithm can be expressed in C++. In particular, if an algorithm can be expressed in any other computer programming language, then it can be expressed in C++, since all (sufficiently general) computer programming languages compute the same set of functions.The primitive operations are (1) adding new words to the dictionary and (2) searching the dictionary for a given word. Typically, dictionary access involves some sort of pre-processing of the word to arrive at the “root” of the word.A twenty page document (single spaced) is likely to contain about 20,000 words. A user may be willing to wait a few seconds between individual “hits” of mis-spelled words, or perhaps up to a minute for the whole document to be processed. This means that a check for an individual word can take about 10-20 ms. Users will typically insert individual words into the dictionary interactively, so this process cantake a couple of seconds. Thus, search must be much more efficient than insertion.The user should be able to find a city based on a variety of attributes (name, location,perhaps characteristics such as population size). The user should also be able to insertand delete cities. These are the fundamental operations of any database system: search, insertion and deletion.A reasonable database has a time constraint that will satisfy the patience of a typicaluser. For an insert, delete, or exact match query, a few seconds is satisfactory. If thedatabase is meant to support range queries and mass deletions, the entire operation may be allowed to take longer, perhaps on the order of a minute. However, the time spent to process individual cities within the range must be appropriately reduced. Inpractice, the data representation will need to be such that it accommodates efficient processing to meet these time constraints. In particular, it may be necessary to supportoperations that process range queries efficiently by processing all cities in the range as a batch, rather than as a series of operations on individual cities.Students at this level are likely already familiar with binary search. Thus, theyshould typically respond with sequential search and binary search. Binary search should be described as better since it typically needs to make fewer comparisons (and thus is likely to be much faster).The answer to this question is discussed in Chapter 8. Typical measures of cost will be number of comparisons and number of swaps. Tests should include running timings on sorted, reverse sorted, and random lists of various sizes.Chap. 1 Data Structures and AlgorithmsThe first part is easy with the hint, but the second part is rather difficult to do withouta stack.a) bool checkstring(string S) {int count = 0;for (int i=0; i<length(S); i++)if (S[i] == ’(’) count++;if (S[i] == ’)’) {if (count == 0) return FALSE;count--;}}if (count == 0) return TRUE;else return FALSE;}b) int checkstring(String Str) {Stack S;int count = 0;for (int i=0; i<length(S); i++)if (S[i] == ’(’)(i);if (S[i] == ’)’) {if ()) return i;();}}if ()) return -1;else return ();}Answers to this question are discussed in Section .This is somewhat different from writing sorting algorithms for a computer, since person’s “working space” is typically limited, as is their ability to physically manipulatethe pieces of paper. Nonetheless, many of the common sorting algorithms have their analogs to solutions for this problem. Most typical answers will be insertion sort, variations on mergesort, and variations on binsort.Answers to this question are discussed in Chapter 8.2Mathematical Preliminaries(a) Not reflexive if the set has any members. One could argue it is symmetric, antisymmetric, and transitive, since no element violate any ofthe rules.(b)Not reflexive (for any female). Not symmetric (consider a brother andsister). Not antisymmetric (consider two brothers). Transitive (for any3 brothers).(c)Not reflexive. Not symmetric, and is antisymmetric. Not transitive(only goes one level).(d)Not reflexive (for nearly all numbers). Symmetric since a+ b= b+ a,so not antisymmetric. Transitive, but vacuously so (there can be nodistinct a, b,and cwhere aRband bRc).(e)Reflexive. Symmetric, so not antisymmetric. Transitive (but sort of vacuous).(f)Reflexive – check all the cases. Since it is only true when x= y,itis technically symmetric and antisymmetric, but rather vacuous. Likewise,it is technically transitive, but vacuous.In general, prove that something is an equivalence relation by proving that it is reflexive, symmetric, and transitive.(a)This is an equivalence that effectively splits the integers into odd andeven sets. It is reflexive (x+ xis even for any integer x), symmetric(since x+ y= y+ x) and transitive (since you are always adding twoodd or even numbers for any satisfactory a, b,and c).(b)This is not an equivalence. To begin with, it is not reflexive for any integer.(c)This is an equivalence that divides the non-zero rational numbers into positive and negative. It is reflexive since x˙x>0. It is symmetric sincexy˙= yx˙. It is transitive since any two members of the given classsatisfy the relationship.5Chap. 2 Mathematical Preliminaries(d)This is not an equivalance relation since it is not symmetric. For example, a=1and b=2.(e)This is an eqivalance relation that divides the rationals based on their fractional values. It is reflexive since for all a,=0. It is symmetricsince if=xthen=.x. It is transitive since any two rationalswith the same fractional value will yeild an integer.(f)This is not an equivalance relation since it is not transitive. For example, 4.2=2and 2.0=2,but 4.0=4.A relation is a partial ordering if it is antisymmetric and transitive. (a)Not a partial ordering because it is not transitive.(b)Is a partial ordering bacause it is antisymmetric (if ais an ancestor ofb, then bcannot be an ancestor of a) and transitive (since the ancestorof an ancestor is an ancestor).(c)Is a partial ordering bacause it is antisymmetric (if ais older than b,then bcannot be older than a) and transitive (since if ais older than band bis older than c, ais older than c).(d)Not a partial ordering, since it is not antisymmetric for any pair of sisters.(e)Not a partial ordering because it is not antisymmetric.(f)This is a partial ordering. It is antisymmetric (no violations exist) and transitive (no violations exist).A total ordering can be viewed as a permuation of the elements. Since there are n!permuations of nelements, there must be n!total orderings.This proposed ADT is inspired by the list ADT of Chapter 4.void clear();void insert(int);void remove(int);void sizeof();bool isEmpty();bool isInSet(int);This proposed ADT is inspired by the list ADT of Chapter 4. Note that whileit is similiar to the operations proposed for Question , the behaviour is somewhat different.void clear();void insert(int);void remove(int);void sizeof();7bool isEmpty();long ifact(int n) {The iterative version requires careful examination to understand whatit does, or to have confidence that it works as claimed.(b)Fibr is so much slower than Fibi because Fibr re-computes thebulk of the series twice to get the two values to add. What is muchworse, the recursive calls to compute the subexpressions also re-computethe bulk of the series, and do so recursively. The result is an exponential explosion. In contrast, Fibicomputes each value in the seriesexactly once, and so its running time is proportional to n.void GenTOH(int n, POLE goal, POLE t1, POLE t2,POLE* curr) {if (curr[n] == goal) Put others on t1.GenTOH(n-1, t1, goal, t2, curr);move(t2, goal);GenTOH(n-1, goal, t1, t2, curr); In theory, this series approaches, but never reaches,0, so it will go on forever. In practice, the value should become computationally indistinguishable from zero, and terminate. However, this is terrible programming practice.Chap. 2 Mathematical Preliminariesvoid allpermute(int array[], int n, int currpos) {if (currpos == (n-1)} {printout(array);return;}for (int i=currpos; i<n; i++) {swap(array, currpos, i);allpermute(array, n, currpos+1);swap(array, currpos, i); The idea is the print out theelements at the indicated bit positions within the set. If we do this for values in the range 0 to 2n.1, we will get the entire powerset.void powerset(int n) {for (int i=0; i<ipow(2, n); i++) {for (int j=0; j<n; j++)if (bitposition(n, j) == 1) cout << j << " ";cout << endl;}Proof: Assume that there is a largest prime number. Call it Pn,the nthlargest prime number, and label all of the primes in order P1 =2, P2 =3,and so on. Now, consider the number Cformed by multiplying all of the nprime numbers together. The value C+1is not divisible by any of the nprime numbers. C+1is a prime number larger than Pn, a contradiction.Thus, we conclude that there is no largest prime number. .Note: This problem is harder than most sophomore level students can handle.√Proof: The proof is by contradiction. Assume that 2is rational. By definition, there exist integers pand qsuch that√p2=,qwhere pand qhave no common factors (that is, the fraction p/qis in lowestterms). By squaring both sides and doing some simple algebraic manipulation, we get2p2=2q222q= pSince p2 must be even, pmust be even. Thus,9222q=4(p)222q=2(p)2This implies that q2 is also even. Thus, pand qare both even, which contra√dicts the requirement that pand qhave no common factors. Thus, 2mustbe irrational. .The leftmost summation sums the integers from 1 to n. The second summation merely reverses this order, summing the numbers from n.1+1=ndown to n.n+1=1. The third summation has a variable substitution ofi, with a corresponding substitution in the summation bounds. Thus,it is also the summation of n.0=nto n.(n.1)=1.Proof:(a) Base case.For n=1, 12 = [2(1)3 +3(1)2 +1]/6=1. Thus, theformula is correct for the base case.(b) Induction Hypothesis.2(n.1)3 +3(n.1)2 +(n.1)i2 =.6i=1(c) Induction Step.i2 i2 +n2=i=1 i=12(n.1)3 +3(n.1)2 +(n.1)2=+n62n3 .6n2 +6n.2+3n2 .6n+3+n.1 2=+n62n3 +3n2 +n=.6Thus, the theorem is proved by mathematical induction. .Proof:(a) Base case.For n=1, 1/2=1.1/2=1/2. Thus, the formula iscorrect for the base case.(b) Induction Hypothesis.11=1..2i=1Chap. 2 Mathematical Preliminaries(c) Induction Step.1 11=+iin222i=1 i=111=1.+n221=1..n2Thus, the theorem is proved by mathematical induction. . Proof:(a) Base case. For n=0, 20 =21 .1=1. Thus, the formula is correctfor the base case.(b) Induction Hypothesis.2i=2n.1.i=0(c) Induction Step.2i=2i+2ni=0 i=0n=2n.1+2n+1 .1=2.Thus, the theorem is proved by mathematical induction. .The closed form solution is 3n+, which I deduced by noting that 3F (n).2n+1 .3F(n)=2F(n)=3. Now, to verify that this is correct, use mathematical induction as follows.For the base case, F(1)=3= .2The induction hypothesis is that =(3n.3)/2.i=1So,3i=3i+3ni=1 i=13n.3n= +32n+1 .33= .2Thus, the theorem is proved by mathematical induction.11nTheorem (2i)=n2 +n.i=1(a) Proof: We know from Example that the sum of the first noddnumbers is ith even number is simply one greater than the ithodd number. Since we are adding nsuch numbers, the sum must be n greater, or n2 +n. .(b) Proof: Base case: n=1yields 2=12 +1, which is true.Induction Hypothesis:2i=(n.1)2 +(n.1).i=1Induction Step: The sum of the first neven numbers is simply the sum of the first n.1even numbers plus the nth even number.2i=( 2i)+2ni=1 i=1=(n.1)2 +(n.1)+2n=(n2 .2n+1)+(n.1)+2n= n2 .n+2n= n2 +n.nThus, by mathematical induction, 2i=n2 +n. .i=1Proof:52Base case. For n=1,Fib(1) = 1 <n=2,Fib(2) = 1 <(5).3Thus, the formula is correct for the base case.Induction Hypothesis. For all positive integers i<n,5 iFib(i)<().3Induction Step. Fib(n)=Fib(n.1)+Fib(n.2)and, by the InductionHypothesis, Fib(n.1)<(5) and Fib(n.3355Fib(n) <() +()3355 5<() +()333Chap. 2 Mathematical Preliminaries85= ()3355<()2()33n5= .3Thus, the theorem is proved by mathematical induction. .Proof:12(1+1)23 =(a) Base case. For n=1, 1=1. Thus, the formula is correct4for the base case.(b) Induction Hypothesis.22(n.1)ni3 = .4i=0(c) Induction Step.n2(n.1)n2i33=+n4i=02n4 .2n3 +n3=+n4n4 +2n3 +n2=4n2(n2 +2n+2)=42n2(n+1)=4Thus, the theorem is proved by mathematical induction..(a) Proof: By contradiction. Assume that the theorem is false. Then, each pigeonhole contains at most 1 pigeon. Since there are nholes, there isroom for only npigeons. This contradicts the fact that a total of n+1pigeons are within the nholes. Thus, the theorem must be correct. .(b) Proof:i. Base case.For one pigeon hole and two pigeons, there must betwo pigeons in the hole.ii. Induction Hypothesis.For npigeons in n.1holes, some holemust contain at least two pigeons.13iii. Induction Step. Consider the case where n+1pigeons are in nholes. Eliminate one hole at random. If it contains one pigeon, eliminate it as well, and by the induction hypothesis some otherhole must contain at least two pigeons. If it contains no pigeons, then again by the induction hypothesis some other hole must contain at least two pigeons (with an extra pigeon yet to be placed). Ifit contains more than one pigeon, then it fits the requirements of the theorem directly..(a)When we add the nth line, we create nnew regions. But, we startwith one region even when there are no lines. Thus, the recurrence is F(n)=F(n.1)+n+1.(b) This is equivalent to the summation F(n)=1+ i=1 ni.(c) This is close to a summation we already know (equation .Base case: T(n.1)=1=1(1+1)/2.Induction hypothesis: T(n.1)=(n.1)(n)/2.Induction step:T(n)= T(n.1)+n=(n.1)(n)/2+n= n(n+1)/2.Thus, the theorem is proved by mathematical induction.If we expand the recurrence, we getT(n)=2T(n.1)+1=2(2T(n.2)+1)+1)=4T(n.2+2+1.Expanding again yieldsT(n)=8T(n.3)+4+2+1.From this, we can deduce a pattern and hypothesize that the recurrence is equivalent tonT(n)= .12i=2n.1.i=0To prove this formula is in fact the proper closed form solution, we use mathematical induction.Base case: T(1)=21 .1=1.14Chap. 2 Mathematical PreliminariesInduction hypothesis: T(n.1)= .1.Induction step:T(n)=2T(n.1)+1= 2 .1) + 1=2n.1.Thus, as proved by mathematical induction, this formula is indeed the correctclosed form solution for the recurrence.(a)The probability is for each choice.(b)The average number of “1” bits is n/2, since each position hasproba bility of being “1.”(c)The leftmost “1” will be the leftmost bit (call it position 0) with probability ; in position 1 with probability , and so on. The numberof positions we must examine is 1 in the case where the leftmost “1” isin position 0; 2 when it is in position 1, and so on. Thus, the expectedcost is the value of the summationni.2ii=1The closed form for this summation is 2 .n+2, or just less than two.2nThus, we expect to visit on average just less than two positions. (Studentsat this point will probably not be able to solve this summation,and it is not given in the book.)There are at least two ways to approach this problem. One is to estimate the volume directly. The second is to generate volume as a function of weight. This is especially easy if using the metric system, assuming that the human body is roughly the density of water. So a 50 Kilo person has a volumeslightly less than 50 liters; a 160 pound person has a volume slightly less than 20 gallons.(a) Image representations vary considerably, so the answer will vary as a result. One example answer is: Consider VGA standard size, full-color(24 bit) images, which is 3 ×640 ×480, or just less than 1 Mbyte perimage. The full database requires some 30-35 CDs.(b)Since we needed 30-35 CDs before, compressing by a factor of 10 isnot sufficient to get the database onto one CD.[Note that if the student picked a smaller format, such as estimating thesize of a “typical” gif image, the result might well fit onto a single CD.](I saw this problem in John Bentley’s Programming Pearls.) Approach 1:The model is Depth X Width X Flow where Depth and Width are in milesand Flow is in miles/day. The Mississippi river at its mouth is about 1/4 mile wide and 100 feet (1/50 mile) deep, with a flow of around 15 miles/hour =360 miles/day. Thus, the flow is about 2 cubic miles/day.Approach 2: What goes out must equal what goes in. The model is Area XRainfall where Area is in square miles and Rainfall is in (linear) miles/day. The Mississipi watershed is about 1000 X 1000 miles, and the average rainfalis about 40 inches/year ≈.1 inches/day ≈.000002 miles/day (2 X .Thus, the flow is about 2 cubic miles/day.Note that the student should NOT be providing answers that look like theywere done using a calculator. This is supposed to be an exercise in estimation! The amount of the mortgage is irrelevant, since this is a question about rates. However, to give some numbers to help you visualize the problem, pick a$100,000 mortgage. The up-front charge would be $1,000, and the savingswould be 1/4% each payment over the life of the mortgage. The monthlycharge will be on the remaining principle, being the highest at first and gradually reducing over time. But, that has little effect for the first few years.At the grossest approximation, you paid 1% to start and will save 1/4% each year, requiring 4 years. To be more precise, 8% of $100,000 is $8,000, while7 3/4% is $7,750 (for the first year), with a little less interest paid (and therefore saved) in following years. This will require a payback period of slightlyover 4 years to save $1000. If the money had been invested, then in 5 yearsthe investment would be worth about $1300 (at 5would be close to 5 1/2years.Disk drive seek time is somewhere around 10 milliseconds or a little lessin 2000. RAM memory requires around 50 nanoseconds – much less thana microsecond. Given that there are about 30 million seconds in a year, a machine capable of executing at 100 MIPS would execute about 3 billionbillion (3 .1018) instructions in a year.Typical books have around 500 pages/inch of thickness, so one million pages requires 2000 inches or 150-200 feet of bookshelf. This would be in excess of 50 typical shelves, or 10-20 bookshelves. It is within the realm of possibility that an individual home has this many books, but it is rather unusual.A typical page has around 400 words (best way to derive this is to estimatethe number of words/line and lines/page), and the book has around 500 pages,so the total is around 200,000 words.16Chap. 2 Mathematical PreliminariesAn hour has 3600 seconds, so one million seconds is a bit less than 300 hours.A good estimater will notice that 3600 is about 10% greater than 3333, so the actual number of hours is about 10% less than 300, or close to 270. (The real value is just under 278). Of course, this is just over 11 days.Well over 100,000, depending on what you wish to classify as a city or town. The real question is what technique the student uses.(a) The time required is 1 minute for the first mile, then 60/59 minutesfor the second mile, and so on until the last mile requires 60/1=60minutes. The result is the following summation.60 6060/i=60 1/i=60H60.i=1 i=1(b)This is actually quite easy. The man will never reach his destination,since his speed approaches zero as he approaches the end of the journey.3Algorithm AnalysisNote that nis a positive integer.5nlog nis most efficient for n=1.2nis most efficient when 2 ≤n≤4.10nis most efficient for all n>5. 20nand 2nare never moreefficient than the other choices.Both log3 nand log2 nwill have value 0 when n=1.Otherwise, 2 is the most efficient expression for all n>1.2/32 3n2 log3nlog2 nn20n4nn!.(a)n+6 inputs (an additive amount, independent of n).(b)8ninputs (a multiplicative factor).(c)64ninputs.100n.10n.√About (actually, 3 100n).n+6.(a)These questions are quite hard. If f(n)=2n= x, then f(2n)=22n=2(2n)2 = x.(b)The answer is 2(nlog2 3). Extending from part (a), we need some way tomake the growth rate even higher. In particular, we seek some way tolog2 3 =make the exponent go up by a factor of 3. Note that, if f(n)= n)=2log2 3log2 3 =3x y, then f(2nn. So, we combine this observationwith part (a) to get the desired answer.First, we need to find constants cand nosuch that 1 ≤c×1 for n>n0.This is true for any positive value c<1 and any positive value of n0 (since nplays no role in the equation).Next, we need to find constants cand n0 such that 1 ≤c×nfor n>n0.This is true for, say, c=1 and n0 =1.1718Chap. 3 Algorithm AnalysisOther values for n0 and care possible than what is given here.(a)The upper bound is O(n) for n0 >0 and c= c1. The lower bound isΩ(n) for n0 >0 and c= c1.(b)The upper bound is O(n3) for n0 >c3 and c= c2 +1. The lowerbound is Ω(n3) for n0 >c3 and c= c2.(c)The upper bound is O(nlog n) for n0 >c5 and c= c4 +1. The lowerbound is Ω(nlog n) for n0 >c5 and c= c4.(d)The upper bound is O(2n) for n0 >c7100 and c= c6 +lower bound is Ω(2n) for n0 >c7100 and c= c6. (100 is used forconvenience to insure that 2n>n6)(a) f(n)=Θ(g(n)) since log n2 = 2 log n.(b)f(n) is in Ω(g(n)) since ncgrows faster than log ncfor any c.(c)f(n) is in Ω(g(n)). Dividing both sides by log n, we see that log n grows faster than 1.(d)f(n) is in Ω(g(n)). If we take both f(n) and g(n) as exponents for 2,2we get 2non one side and 2log2 n=(2log n)2 = n2 on the other, and ngrows slower than 2n.(e)f(n) is in Ω(g(n)). Dividing both sides by log nand throwing awaythe low order terms, we see that ngrows faster than 1.(f)f(n)=Θ(g(n)) since log 10 and 10 are both constants.2(g)f(n) is in Ω(g(n)) since 2ngrows faster than 10n.(h)f(n) is in O(g(n)). 3n=, and if we divide both sides by 2n,we see thatgrows faster than 1.(a) This fragment is Θ(1).(b)This fragment is Θ(n) since the outer loop is executed a constant number。
数据结构c语言版课后习题答案完整版

数据结构c语言版课后习题答案完整版Document serial number【KKGB-LBS98YT-BS8CB-BSUT-BST108】第1章绪论5.选择题:CCBDCA6.试分析下面各程序段的时间复杂度。
(1)O(1)(2)O(m*n)(3)O(n2)n)(4)O(log3(5)因为x++共执行了n-1+n-2+……+1= n(n-1)/2,所以执行时间为O(n2)(6)O(n)第2章线性表1.选择题babadbcabdcddac2.算法设计题(6)设计一个算法,通过一趟遍历在单链表中确定值最大的结点。
ElemType Max (LinkList L ){if(L->next==NULL) return NULL;pmax=L->next; 法设计题(2)回文是指正读反读均相同的字符序列,如“abba”和“abdba”均是回文,但“good”不是回文。
试写一个算法判定给定的字符向量是否为回文。
(提示:将一半字符入栈)根据提示,算法可设计为:合应用题(1)已知模式串t=‘abcaabbabcab’写出用KMP法求得的每个字符对应的next和nextval函数值。
-1到9,列下标从1到11,从首地址S开始连续存放主存储器中,主存储器字长为16位。
求:①存放该数组所需多少单元②存放数组第4列所有元素至少需多少单元③数组按行存放时,元素A[7,4]的起始地址是多少④ 数组按列存放时,元素A[4,7]的起始地址是多少每个元素32个二进制位,主存字长16位,故每个元素占2个字长,行下标可平移至1到11。
(1)242 (2)22 (3)s+182 (4)s+142(4)请将香蕉banana 用工具 H( )—Head( ),T( )—Tail( )从L 中取出。
L=(apple,(orange,(strawberry,(banana)),peach),pear)H (H (T (H (T (H (T (L )))))))(5)写一个算法统计在输入字符串中各个不同字符出现的频度并将结果存入文件(字符串中的合法字符为A-Z 这26个字母和0-9这10个数字)。
数据结构c语言版课后习题答案完整版

数据结构c语⾔版课后习题答案完整版第1章绪论5.选择题:CCBDCA6.试分析下⾯各程序段的时间复杂度。
(1)O(1)(2)O(m*n)(3)O(n2)(4)O(log3n)(5)因为x++共执⾏了n-1+n-2+……+1= n(n-1)/2,所以执⾏时间为O(n2)(6)O(n)第2章线性表1.选择题babadbcabdcddac2.算法设计题(6)设计⼀个算法,通过⼀趟遍历在单链表中确定值最⼤的结点。
ElemType Max (LinkList L ){if(L->next==NULL) return NULL;pmax=L->next; //假定第⼀个结点中数据具有最⼤值p=L->next->next;while(p != NULL ){//如果下⼀个结点存在if(p->data > pmax->data) pmax=p;p=p->next;}return pmax->data;(7)设计⼀个算法,通过遍历⼀趟,将链表中所有结点的链接⽅向逆转,仍利⽤原表的存储空间。
void inverse(LinkList &L) {// 逆置带头结点的单链表 Lp=L->next; L->next=NULL;while ( p) {q=p->next; // q指向*p的后继p->next=L->next;L->next=p; // *p插⼊在头结点之后p = q;(10)已知长度为n的线性表A采⽤顺序存储结构,请写⼀时间复杂度为O(n)、空间复杂度为O(1)的算法,该算法删除线性表中所有值为item的数据元素。
[题⽬分析] 在顺序存储的线性表上删除元素,通常要涉及到⼀系列元素的移动(删第i个元素,第i+1⾄第n个元素要依次前移)。
本题要求删除线性表中所有值为item的数据元素,并未要求元素间的相对位置不变。
数据结构c语言版第三版习题解答

数据结构c语言版第三版习题解答数据结构是计算机科学中非常重要的一门学科,它研究如何在计算机中存储和组织数据,以便有效地进行检索和操作。
数据结构的知识对于编写高效的程序和解决复杂的问题至关重要。
在学习和理解数据结构的过程中,解决习题是一种非常有效的方法。
本文将为读者提供《数据结构C语言版(第三版)》习题的解答。
1. 第一章:绪论第一章主要介绍了数据结构的基本概念和内容,包括算法和数据结构的概念、抽象数据类型(ADT)以及算法的评价等。
习题解答中,我们可以通过分析和讨论的方式对这些概念进行加深理解。
2. 第二章:算法分析第二章主要介绍了算法的基本概念和分析方法,包括时间复杂度和空间复杂度的计算方法。
习题解答中,我们可以通过具体的算法实例来计算其时间和空间复杂度,加深对算法分析的理解。
3. 第三章:线性表第三章主要介绍了线性表的概念和实现,包括顺序表和链表两种实现方式。
习题解答中,我们可以通过编写代码实现线性表的基本操作,并分析其时间和空间复杂度。
4. 第四章:栈和队列第四章主要介绍了栈和队列的概念和实现,包括顺序栈、链栈、顺序队列和链队列四种实现方式。
习题解答中,我们可以通过编写代码实现栈和队列的基本操作,并分析其时间和空间复杂度。
5. 第五章:串第五章主要介绍了串的概念和实现,包括顺序串和链串两种实现方式。
习题解答中,我们可以通过编写代码实现串的基本操作,并分析其时间和空间复杂度。
6. 第六章:树第六章主要介绍了树的概念和实现,包括二叉树、哈夫曼树和赫夫曼编码等内容。
习题解答中,我们可以通过编写代码实现树的基本操作,并分析其时间和空间复杂度。
7. 第七章:图第七章主要介绍了图的基本概念和实现,包括图的表示方法和图的遍历算法等。
习题解答中,我们可以通过编写代码实现图的基本操作,并分析其时间和空间复杂度。
8. 第八章:查找第八章主要介绍了查找算法的基本概念和实现,包括顺序查找、二分查找、哈希查找等内容。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1.名词解释
数据:信息的载体,在计算科学中指所有能被输入到计算机中并能被计算机程序识别和处理的符号集合。
数据元素:数据的基本单位,有时也称为元素,节点,顶点,记录。
数据结构:是指数据元素之间的相互关系,即数据的组织形式。
逻辑结构:是从逻辑关系上描述数据,是面向问题的,不涉及数据在计算机的存储,独立于计算机的。
包括线性结构和非线性结构两大类。
存储结构:又称为物理结构,是指数据在计算机内的表示方法,是逻辑结构的具体实现。
包括顺序存储,链接存储,索引存储,散列存储四大类。
线性结构:有且仅有一个开始结点和一个终端结点,并且所有的节点都最多只有一个直接前驱和一个直接后继。
非线性结构:是一个结点可能有多个直接前驱和直接后继。
2.填空题
1.线性结构非线性结构
2.顺序存储链接存储索引存储散列存储
3.一对一一对多多对多
4.对数据施加的操作(检索.插入.删除.更新.排序)
5.算法
6.算法的时间复杂度算法的空间复杂度
7. O(n*n) O(1)
8. O(nlog2(n))
9. O(n)
10.O(n*n)
三.选择题
1-5 D C A C A 6-10 B D D D B
四.简答题
1.逻辑结构:是从逻辑关系上描述数据,是面向问题的,不涉及数据在计算机的存储,独立于计算机的。
包括线性结构和非线性结构两大类。
存储结构:又称为物理结构,是指数据在计算机内的表示方法,是逻辑结构的具体实现。
包括顺序存储,链接存储,索引存储,散列存储四大类。
运算:是对数据的施加的操作。
包括检索,插入,删除,更新,排序等。
2.线性结构有且仅有一个开始结点和一个终端结点,并且所有的节点都最多只有一个直接前驱和一个直接后继。
非线性结构是一个结点可能有多个直接前驱和直接后继。
3.逻辑结构:是从逻辑关系上描述数据,是面向问题的,不涉及数据在计算机的存储,独立于计算机的。
包括线性结构和非线性结构两大类。
存储结构:又称为物理结构,是指数据在计算机内的表示方法,是逻辑结构的具体实现。
包括顺序存储,链接存储,索引存储,散列存储四大类。
4.顺序存储:将逻辑上相邻的数据元素存储在物理位置上相邻的存储单元中,元素间的逻辑关系由存储单元的位置直接体现。
链接存储:将数据元素存储在一组任意的存储单元当中,用附加的指针域表示元素之间的逻辑关系。
5.1.有穷性2.确定性3.可行性4.输入5.输出
一.名词解释。