数据结构第二次单元测试

合集下载

数据结构练习题第二章答案

数据结构练习题第二章答案

数据结构练习题第二章答案一、选择题1. 在数据结构中,线性结构的特点是什么?A. 元素之间存在一对一的关系B. 元素之间存在一对多的关系C. 元素之间存在多对多的关系D. 元素之间存在一对一或一对多的关系答案:D2. 栈(Stack)是一种特殊的线性表,其特点是:A. 允许在表的一端进行插入和删除操作B. 允许在表的两端进行插入和删除操作C. 只能在表的两端进行插入和删除操作D. 只能在表的中间进行插入和删除操作答案:A3. 队列(Queue)与栈的主要区别在于:A. 队列是先进先出(FIFO),栈是先进后出(LIFO)B. 栈是先进先出(FIFO),队列是先进后出(LIFO)C. 队列和栈都是先进先出(FIFO)D. 队列和栈都是先进后出(LIFO)答案:A二、简答题1. 什么是链表?链表有哪些基本操作?答案:链表是一种由一系列节点组成的线性数据结构,每个节点包含数据部分和指向下一个节点的指针。

链表的基本操作包括插入节点、删除节点、查找节点和遍历链表。

2. 线性表的顺序存储结构和链式存储结构有何区别?答案:顺序存储结构使用连续的存储单元来存储数据元素,如数组。

链式存储结构不要求数据元素在存储空间中连续,每个元素包含指向下一个元素的指针,如链表。

三、编程题1. 编写一个函数,实现在单链表中插入一个新节点到指定位置。

```c#include <stdio.h>#include <stdlib.h>typedef struct Node {int data;struct Node *next;} Node;Node* createNode(int data) {Node *newNode = (Node*)malloc(sizeof(Node));newNode->data = data;newNode->next = NULL;return newNode;}void insertNode(Node head, int position, int data) {Node *newNode = createNode(data);if (position == 0) {newNode->next = *head;*head = newNode;} else {Node *current = *head;for (int i = 0; current != NULL && i < position - 1; i++) {current = current->next;}if (current == NULL) return; // Position is greater than the number of nodesnewNode->next = current->next;current->next = newNode;}}int main() {Node *head = NULL;insertNode(&head, 0, 10);insertNode(&head, 1, 20);// Print the list to verify the insertionNode *current = head;while (current != NULL) {printf("%d ", current->data);current = current->next;}return 0;}```四、分析题1. 分析栈的后进先出(LIFO)特性在实际应用中的优势和局限性。

数据结构--第二章考试题库(含答案)

数据结构--第二章考试题库(含答案)

第2章线性表一选择题1.下述哪一条是顺序存储结构的优点?()【北方交通大学 2001 一、4(2分)】A.存储密度大 B.插入运算方便 C.删除运算方便 D.可方便地用于各种逻辑结构的存储表示2.下面关于线性表的叙述中,错误的是哪一个?()【北方交通大学 2001 一、14(2分)】A.线性表采用顺序存储,必须占用一片连续的存储单元。

B.线性表采用顺序存储,便于进行插入和删除操作。

C.线性表采用链接存储,不必占用一片连续的存储单元。

D.线性表采用链接存储,便于插入和删除操作。

3.线性表是具有n个()的有限序列(n>0)。

【清华大学 1998 一、4(2分)】A.表元素 B.字符 C.数据元素 D.数据项 E.信息项4.若某线性表最常用的操作是存取任一指定序号的元素和在最后进行插入和删除运算,则利用()存储方式最节省时间。

【哈尔滨工业大学 2001二、1(2分)】A.顺序表 B.双链表 C.带头结点的双循环链表 D.单循环链表5.某线性表中最常用的操作是在最后一个元素之后插入一个元素和删除第一个元素,则采用()存储方式最节省运算时间。

【南开大学 2000 一、3】A.单链表 B.仅有头指针的单循环链表 C.双链表 D.仅有尾指针的单循环链表6.设一个链表最常用的操作是在末尾插入结点和删除尾结点,则选用( )最节省时间。

A. 单链表B.单循环链表C. 带尾指针的单循环链表D.带头结点的双循环链表【合肥工业大学 2000 一、1(2分)】7.若某表最常用的操作是在最后一个结点之后插入一个结点或删除最后一个结点。

则采用()存储方式最节省运算时间。

【北京理工大学 2000一、1(2分)】A.单链表 B.双链表 C.单循环链表 D.带头结点的双循环链表8. 静态链表中指针表示的是(). 【北京理工大学 2001 六、2(2分)】A.内存地址 B.数组下标 C.下一元素地址 D.左、右孩子地址9. 链表不具有的特点是()【福州大学 1998 一、8 (2分)】A.插入、删除不需要移动元素 B.可随机访问任一元素C.不必事先估计存储空间 D.所需空间与线性长度成正比10. 下面的叙述不正确的是()【南京理工大学 1996 一、10(2分)】A.线性表在链式存储时,查找第i个元素的时间同i的值成正比B. 线性表在链式存储时,查找第i个元素的时间同i的值无关C. 线性表在顺序存储时,查找第i个元素的时间同i 的值成正比D. 线性表在顺序存储时,查找第i个元素的时间同i的值无关11. 线性表的表元存储方式有((1))和链接两种。

华文慕课数据结构与算法(上)(北京大学)章节测验答案

华文慕课数据结构与算法(上)(北京大学)章节测验答案

解忧书店 JieYouBookshop第二章单元测试1、(1分)以下哪种结构是逻辑结构,而与存储和运算无关:Which of the following structure is a logical structure regardless of the storage or algorithm:(There is only one correct answer)A、队列(queue)B、双链表(doubly linked list)C、数组(array)D、顺序表(Sequential list)答案: A2、(1分)计算运行下列程序段后m的值:Calculate the value of m after running the following program segmentn = 9; m = 0;for (i=1;i<=n;i++)for (j = 2*i; j<=n; j++)m=m+1;求m的值答案: 203、(1分)下列说法正确的是:Which options may be correct?(there are more than one correct answers)A、如果函数f(n)是O(g(n)),g(n)是O(h(n)),那么f(n)是O(h(n))【 if f(n) is O(g(n)),g(n) is O(h(n)),then f(n) is O(h(n))】B、如果函数f(n)是O(g(n)),g(n)是O(h(n)),那么f(n)+g(n)是O(h(n))【if f(n) is O(g(n)),g(n) is O(h(n)),so f(n)+g(n) is O(h(n))】C、如果a>b>1,logan是O(logbn),但logbn不一定是O(logan)【if a>b>1,logan is O(logbn),logbn may not be O(logan)】D、函数f(n)是O(g(n)),当常数a足够大时,一定有函数g(n)是O(af(n))【if f(n)是O(g(n)),When constant a is big enough ,there must be g(n) is O(af(n))】答案: A,B4、(1分)由大到小写出以下时间复杂度的序列:答案直接写标号,如:(1)(2)(3)(4)(5) (提示:系统基于字符匹配来判定答案,所以您的答案中不要出现空格)Write the following time complexity in descending sequence:Write down the answer labels such as (1)(2)(3)(4)(5). (Hint:This problem is judged by string matching, Please make sure your answer don't contain any blanks. )RUX4%GXZNDD{IAQWTCSEEJG.png答案: (5)(1)(2)(4)(3)5、(1分)已知一个数组a的长度为n,求问下面这段代码的时间复杂度:An array of a, its length is known as n. Please answer the time complexity of the following code.(There are more than one answers.)for (i=0,length=1;i<n-1;i++){for (j = i+1;j<n && a[j-1]<=a[j];j++)if(length<j-i+1)length=j-i+1;}Screen Shot 2017-09-05 at 23.31.19.pngA、如图,A选项B、如图,B选项C、如图,C选项D、如图,D选项答案: A,B第三章单元测试1、(1分)下面关于线性表的叙述中,正确的是哪些?Which of the followings about linear list are correct?(There are more than one answers.)Select the answer that matchesA、线性表采用顺序存储,必须占用一片连续的存储单元。

数据结构5-6章测试题(带答案)

数据结构5-6章测试题(带答案)

《数据结构》第2教学单元测试练习题一.选择1.将一棵有100个结点的完全二叉树从根结点这一层开始,每一层上从左到右依次对结点编号,根结点的编号为1,则编号为49的结点的左孩子编号为()根? 右孩子?A.98B.99C.50D.482.以下说法错误的是( )A.一般在赫夫曼树中,权值越大的叶子离根结点越近B.赫夫曼树中没有度数为1的分支结点C.若初始森林中共有n棵二叉树,最终求得的赫夫曼树共有2n-1个结点D.若初始森林中共有n棵二叉树,进行2n-1次合并后才能剩下一棵最终的赫夫曼树3.深度为6的二叉树最多有( )个结点A.64 B.63 C.32 D.314.以下说法正确的是( )A.任何一棵二叉树中至少有一个结点的度为2B.任何一棵二叉树中每个结点的度都为2C.任何一棵二叉树的度肯定等于2D.任何一棵二叉树的度可以小于25.设森林T中有4棵树,第一、二、三、四棵树的结点个数分别是n1,n2,n3,n4,那么当把森林T转换成一棵二叉树后,且根结点的右子树上有( d )个结点。

根结点的左孩子上有( a )个结点。

A.n1-1B.n1C.n1+n2+n3D.n2+n3+n46.对含有( )个结点的非空二叉树,采用任何一种遍历方式,其结点访问序列均相同。

A.0B.1C.2D.不存在这样的二叉树7.讨论树、森林和二叉树的关系,目的是为了( )A.借助二叉树上的运算方法去实现对树的一些运算B.将树、森林按二叉树的存储方式进行存储C.将树、森林转换成二叉树D.体现一种技巧,没有什么实际意义8.已知某二叉树的后续遍历序列是dabec,中序遍历序列是deabc,它的前序遍历序列是( )A.acbedB.deabcC.decabD.cedba9.如果T2是由有序树T转化而来的二叉树,那么T中结点的前序就是T2中结点的( a ), 后序就是T2中结点的( b ) A.前序 B.中序 C.后序 D.层次序10.深度为5的二叉树至多有( )个结点。

数据结构试题及答案!!!

数据结构试题及答案!!!

一、单选题(每题 2 分,共20分)1. 1.对一个算法的评价,不包括如下(B )方面的内容。

A.健壮性和可读性B.并行性C.正确性D.时空复杂度2. 2.在带有头结点的单链表HL中,要向表头插入一个由指针p指向的结点,则执行(A )。

A. p->next=HL->next; HL->next=p;B. p->next=HL; HL=p;C. p->next=HL; p=HL;D. HL=p; p->next=HL;3. 3.对线性表,在下列哪种情况下应当采用链表表示?( B )A.经常需要随机地存取元素B.经常需要进行插入和删除操作C.表中元素需要占据一片连续的存储空间D.表中元素的个数不变4. 4.一个栈的输入序列为1 2 3,则下列序列中不可能是栈的输出序列的是( C )A. 2 3 1B. 3 2 1C. 3 1 2D. 1 2 35. 5.AOV网是一种( D )。

A.有向图B.无向图C.无向无环图D.有向无环图6. 6.采用开放定址法处理散列表的冲突时,其平均查找长度(B )。

A.低于链接法处理冲突 B. 高于链接法处理冲突C.与链接法处理冲突相同D.高于二分查找7.7.若需要利用形参直接访问实参时,应将形参变量说明为(D )参数。

A.值B.函数C.指针D.引用8.8.在稀疏矩阵的带行指针向量的链接存储中,每个单链表中的结点都具有相同的(A )。

A.行号B.列号C.元素值D.非零元素个数9.9.快速排序在最坏情况下的时间复杂度为(D )。

A.O(log2n) B.O(nlog2n) C.0(n) D.0(n2)10.10.从二叉搜索树中查找一个元素时,其时间复杂度大致为( C )。

A. O(n)B. O(1)C. O(log2n)D. O(n2)二、二、运算题(每题 6 分,共24分)1. 1.数据结构是指数据及其相互之间的______联系______。

数据结构第2章习题参考答案

数据结构第2章习题参考答案

数据结构第2章习题参考答案1. 简答题1.1 什么是数据结构?数据结构是指相互之间存在一种或多种特定关系的数据元素的集合,它包括数据的逻辑结构和物理结构。

1.2 数据结构的分类有哪些?数据结构可以分为线性结构和非线性结构。

线性结构包括线性表、栈、队列和串;非线性结构包括树和图。

1.3 数据结构的逻辑结构有哪些?数据结构的逻辑结构包括线性结构、树形结构和图形结构。

1.4 数据结构的物理结构有哪些?数据结构的物理结构包括顺序存储结构和链式存储结构。

1.5 什么是算法?算法是指求解问题的具体步骤和方法。

1.6 算法的特性有哪些?算法应具有有穷性、确定性、可行性和输入输出性。

2. 选择题2.1 在栈的顺序存储结构中,栈的存储位置是:A. 自顶向下递增B. 自底向上递增C. 自底向上递减D. 自顶向下递减答案:D2.2 下列哪个数据结构不适合表示有父子关系的数据?A. 二叉树B. 图C. 链表D. 堆答案:D2.3 对于一棵完全二叉树,叶子节点的个数为n,则树中节点的总数为:A. 2nB. 2n + 1C. nD. n + 1答案:A2.4 假设有一个长度为10的栈,初始时栈为空,若对该栈连续执行5次入栈操作,然后执行4次出栈操作,最后执行1次入栈操作,则栈中剩余的元素个数为:A. 0B. 1C. 4D. 6答案:D3. 编程题3.1 实现一个栈数据结构的基本操作,包括入栈、出栈、获取栈顶元素和判断栈是否为空。

```Pythonclass Stack:def __init__(self):self.items = []def is_empty(self):return len(self.items) == 0def push(self, item):self.items.append(item)def pop(self):if self.is_empty():return Nonereturn self.items.pop()def peek(self):if self.is_empty():return Nonereturn self.items[-1]```3.2 实现一个队列数据结构的基本操作,包括入队、出队、获取队首元素和判断队列是否为空。

2020智慧树知道网课《数据结构(中国海洋大学)》课后章节测试满分答案

2020智慧树知道网课《数据结构(中国海洋大学)》课后章节测试满分答案

第一章测试1【单选题】(2分)图书馆的数目检索系统采用关系的数据结构。

A.树形B.图状C.集合D.线性2【单选题】(2分)是相互之间存在一种或多种特定关系的数据元素的集合。

A.数据项B.数据结构C.数据元素D.数据3【单选题】(2分)()是一个值的集合和定义在这个值集上的一组操作的总称。

A.数据项B.数据类型C.数据元素D.数据结构4【单选题】(2分)算法的确定性是指()A.算法中没有逻辑B.在任何情况下,算法不会出现死循环C.算法中的每一条指令必须有确切的含义D.当输入数据非法时,算法也能作出反应或进行处理第二章测试1【单选题】(2分)线性表中的数据元素有一个前驱多个后继。

A.错B.对2【单选题】(2分)用顺序结构存储,删除最后一个结点时,()A.其它B.会移动其它结点位置C.可能会移动其它结点位置D.一定不会移动其它结点位置3【单选题】(2分)链表中逻辑上相邻的元素的物理地址__________相邻。

A.一定不B.必定C.其它D.不一定4【单选题】(2分)1.假设有两个按元素值递增有序排列的线性表A和B,均以单链表作存储结构,请编写算法将A表和B表归并成一个按元素值递减有序(即非递增有序,允许表中含有值相同的元素)排列的线性表C,并要求利用原表(即A表和B表)的结点空间构造C表。

//将合并逆置后的结果放在C表中,并删除B表StatusListMergeOppose_L(LinkList&A,LinkList&B,LinkList&C){LinkListpa,pb,qa,qb;pa=A;pb=B;qa=pa;//保存pa的前驱指针qb=pb;//保存pb的前驱指针pa=pa->next;pb=pb->next;A->next=NULL;C=A;while(pa&&pb){if(pa->data<pb->data){qa=pa;pa=pa->next;qa->next=A->next;//将当前最小结点插入A表表头A->next=qa;}else{qb=pb;pb=pb->next;()//将当前最小结点插入B表表头A->next=qb;}}while(pa){qa=pa;pa=pa->next;qa->next=A->next;A->next=qa;}while(pb){qb=pb;pb=pb->next;qb->next=A->next;A->next=qb;}pb=B;free(pb);returnOK;}A.qa->next=A->nextB.qa->next=A;C.qb->next=A->nextD.qb->next=A;5【单选题】(2分)假设某个单向循环链表的长度大于1,且表中既无头结点也无头指针。

数据结构-试卷二及答案

数据结构-试卷二及答案

数据结构-试卷二及答案一、判断(每小题 1 分,共 10 分) 1.数据的存储结构是数据的逻辑结构的存储映象,不仅要存储数据元素的值,还要存储元素之间的相互关系。

2.用顺序表来存储线性表时,不需要另外开辟空间来保存数据元素之间的相互关系。

3.完全二叉树的叶子结点只能出现在最后一层上。

4.折半查找方法要求待查表必须是有序的顺序表。

5.在有向图 G 中, V 2 , V 1 和 V 1 , V 2 是两条不同的边。

6.图的最小生成树是唯一的。

7.从循环单链表的某一结点出发,只能找到它的后继结点,不能找到它的前趋结点。

8.在单链表中,头结点是必不可少的。

9.对快速排序来说,初始序列为正序和反序,都是最坏情况。

10.广义表是特殊的线性表。

二、选择(每题 1 分,共 15 分) 1.设栈 S 和队列 Q 的初始状态均为空,元素 abcdefg 依次进入栈 S 。

若每个元素出栈后立即进入队列 Q ,且 7 个元素出队的顺序是bdcfeag ,则栈 S 的容量至少是()。

A.1 B.2 C.3 D.4 2.下列线索二叉树1/ 8中(用虚线表示线索),符合后序线索树定义的是( )。

3.已知广义表 A= (( a,b ) ,(c,d) ) , 则 head(A) 等于 ( )。

A.(a,b) B.((a,b)) C.a,b D.a 4.设字符串s1=‘ABCDEFG’,s2=‘PQRST’, 则运算s=strcat(strsub(s1,2,strlen(s2)),strsub (s1,strlen(s2),2))后结果为()。

A.BCQR B.BCDEF C.BCDEFG D.BCDEFEF 5.具有 8 个顶点的连通图的深度优先生成树,其边数为()。

A.8 B.9 C.7 D.6 6.算法分析的两个主要方面是()。

A.空间复杂性和时间复杂性 B.正确性和简明性 C.可读性和文档性 D.数据复杂性和程序复杂性 7.下列四种排序中()的空间复杂度最大。

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

0980 输出利用先序遍历创建的二叉树的层次遍历序列(中)#include <iostream>#include <malloc.h>using namespace std;typedef struct node{char data;node *leftchild;node *rightchild;}Node;void Init(Node *&L){L = (Node *)malloc(sizeof(Node));}void PreCreate(Node *&L){char ch;cin>>ch;if(ch != '#'){Init(L);L->data = ch;L->leftchild = NULL;L->rightchild = NULL;PreCreate(L->leftchild);PreCreate(L->rightchild);}else{L = NULL;}}void levelT(Node *L){Node *p;Node *q[100];int fornt, rear;fornt = -1;rear = -1;if(L != NULL){rear = (rear+1)%100;q[rear] = L;while(fornt != rear){fornt = (fornt+1)%100;p = q[fornt];cout<<p->data;if(p->leftchild != NULL){rear = (rear+1)%100;q[rear] = p->leftchild;}if(p->rightchild != NULL){rear = (rear+1)%100;q[rear] = p->rightchild;}}}}int main(){Node *p;PreCreate(p);levelT(p);return 0;}0981统计利用二叉树存储的森林中树的棵树(易)#include <iostream>#include <malloc.h>using namespace std;typedef struct node{char data;node *leftchild;node *rightchild;}Node;void InitTree(Node *&L){L = (Node *)malloc(sizeof(Node));}void preCreate(Node *&L){InitTree(L);char ch;cin>>ch;if(ch != '#'){L->data = ch;L->leftchild = NULL;L->rightchild = NULL;preCreate(L->leftchild);preCreate(L->rightchild);}elseL = NULL;}int Ftree(Node *&L){if(L == NULL){return 0;}else if(L->rightchild == NULL){return 1;}else{return Ftree(L->rightchild)+1;}}int main(){Node *p;preCreate(p);cout<<Ftree(p);return 0;}0982 输出利用二叉树存储的普通树的度(中)#include <iostream>#include <malloc.h>using namespace std;typedef struct node{char data;node *leftchild;node *rightchild;}Node;void Init(Node *&L){L = (Node *)malloc(sizeof(Node));}void preCreate(Node *&L){char ch;cin>>ch;if(ch != '#'){Init(L);L->data = ch;L->leftchild = NULL;L->rightchild = NULL;preCreate(L->leftchild);preCreate(L->rightchild);}else{L = NULL;}}int findG(Node *&L){int max = 0, a, b;if(L == NULL){return max;}else{a = (findG(L->rightchild)+1);b = findG(L->leftchild);(a >= b)? max = a : max = b;}return max;}int main(){Node *p;preCreate(p);if(p->rightchild != NULL){cout<<"ERROR";}else{cout<<findG(p);}return 0;}0983利用二叉树中序及后序遍历确定该二叉树的先序序列(难)#include <iostream>#include <malloc.h>#include <string.h>using namespace std;typedef struct node{char data;node *leftchild;node *rightchild;}Node;void InitTree(Node *&L){L = (Node *)malloc(sizeof(Node));L->leftchild = NULL;L->rightchild = NULL;}void preCreate(Node *&L, char *pos, char *in, int n) {char *p;int k;if(n <= 0){InitTree(L);L = NULL;return;}InitTree(L);L->data = *(pos+n-1);for(p = in; p < in +n; p++){if(*p == *(pos+n-1))break;}k = p -in;preCreate(L->leftchild, pos, in, k);preCreate(L->rightchild, pos+k, p+1, n-k-1);}void post(Node *&L){if(L != NULL){cout<<L->data;post(L->leftchild);post(L->rightchild);}}int main(){Node *p;int n;char pos[100], in[100];scanf("%s",in);scanf("%s",pos);n = strlen(in);preCreate(p, pos, in, n);post(p);return 0;}0984利用二叉树中序及先序遍历确定该二叉树的后序序列(难)#include <iostream>#include <malloc.h>#include <string.h>using namespace std;typedef struct node{char data;node *leftchild;node *rightchild;}Node;void InitTree(Node *&L){L = (Node *)malloc(sizeof(Node));L->leftchild = NULL;L->rightchild = NULL;}void preCreate(Node *&L, char *pre, char *in, int n){char *p;int k;if(n <= 0){InitTree(L);L = NULL;return;}InitTree(L);L->data = *pre;for(p = in; p < in +n; p++){if(*p == *pre)break;}k = p -in;preCreate(L->leftchild, pre+1, in, k);preCreate(L->rightchild, pre+k+1, p+1, n-k-1);}void post(Node *&L){if(L != NULL){post(L->leftchild);post(L->rightchild);cout<<L->data;}}int main(){Node *p;int n;char pre[100], in[100];scanf("%s",in);scanf("%s",pre);n = strlen(in);preCreate(p, pre, in, n);post(p);return 0;}0986 哈夫曼译码0987输出用先序遍历创建的二叉树是否为完全二叉树的判定结果(难)1053输出利用先序遍历创建的二叉树中的指定结点的度(易)1052 输出利用先序遍历创建的二叉树中的指定结点的双亲结点(中)#include<stdlib.h>#include<malloc.h>#include<iostream>using namespace std;typedef struct Node{char data;Node*lc,*rc;}node;char x;void precreat(node*&root){char c;cin>>c;if(c=='#'){root=NULL;}else{root=(node*)malloc(sizeof(node));root->data=c;root->lc=NULL;root->rc=NULL;precreat(root->lc);precreat(root->rc);}}void PreOrder(node*root){if(root!=NULL){if(root->lc!=NULL&&root->lc->data==x){cout<<root->data;exit(0);}if(root->rc!=NULL&&root->rc->data==x){cout<<root->data;exit(0);}PreOrder(root->lc);PreOrder(root->rc);}else{cout<<"#";exit(0);}}int main(){node*root;precreat(root);cin>>x;PreOrder(root);return 0;}1051 输出利用先序遍历创建的二叉树中的指定结点的孩子结点(易)#include<iostream>#include<malloc.h>using namespace std;typedef struct node{char data;node *left,*right;}treenode;void pre(treenode *&root){char c;cin>>c;if(c=='#') root=NULL;else{root=(treenode *)malloc(sizeof(treenode));root->data=c;root->left=NULL;root->right=NULL;pre(root->left);pre(root->right);}}void inorder(treenode *&root,char c){if(root==NULL)return;else{if(root->data==c){if(root->left==NULL)cout<<"L:#,";elsecout<<"L:"<<root->left->data<<",";if(root->right==NULL)cout<<"R:#";elsecout<<"R:"<<root->right->data;return;}inorder(root->left,c);inorder(root->right,c);}}int main(){char c;treenode *root;pre(root);cin>>c;inorder(root,c);return 0;}1105交叉二叉树的孩子结点(易)1055邻接矩阵到邻接表(易)1056邻接表到邻接矩阵(易)1057有向图的出度计算(易)1058无向图的出度计算(易)1059有向图的最大出度计算(易1060无向图的最大出度计算(易)1061有向图的k出度计算(中)1062有向图的边存在判断(易)#include <iostream>#include <stdio.h>using namespace std;int main(){int a[50][50], i, j, n, row, col;cin>>n;cin>>row;cin>>col;for(i = 0; i < n; i++){for(j = 0; j < n; j++){cin>>a[i][j];}}if(a[row][col]==1)cout<<"yes";elsecout<<"no";return 0;}1063带权有向图计算(易) 1064带权无向图存储判定(中) #include <iostream>#include <stdio.h>using namespace std;void Jug(int a[][50], int n){int i, j;for(i = 0; i < n; i++){if(a[i][i] != 0){cout<<"no";return;}for(j = 0; j < n; j++){if(a[i][j] != a[j][i]){cout<<"no";return;}}}cout<<"yes";}int main(){int a[50][50], i, j, n;cin>>n;for(i = 0; i < n; i++){for(j = 0; j < n; j++){cin>>a[i][j];}}Jug(a,n);return 0;}1065无向图的连通分量计算(中)1067 有向图的邻接表存储强连通判断(中)#include <iostream>#include <string.h>using namespace std;int str[1000][1000],n;int main (void){int i,a,b,n,m;cin>>m>>n;for (i=0;i<n;i++){cin>>a>>b;str[a][b]=1;}int mark=1;for (i=0;i<m-1;i++){if (str[i][i+1]!=1)mark=0;}if (mark)cout<<"yes";elsecout<<"no";return 0;}1068图的按录入顺序深度优先搜索(难)#include<iostream>using namespace std;int visited[100]={0};typedef struct{int n;char data[500];int edge[500][500];}Mgraph;void create(Mgraph &G,int n){int i,j;for(i=0;i<n;i++)cin>>G.data[i];for(i=0;i<n;i++)for(j=0;j<n;j++)cin>>G.edge[i][j];G.n=n;}void dfs(Mgraph &g,char c){cout<<c;int i,j;for(i=0;i<g.n;i++)if(g.data[i]==c){j=i;visited[j]=1;break;}for(i=0;i<(g.n);i++){if(visited[i]==0&&g.edge[j][i]!=0)dfs(g,g.data[i]);}}int main(){int n;char e;Mgraph my;cin>>n;create(my,n);cin>>e;dfs(my,e);return 0;}1069图的按录入顺序广度优先搜索(难)#include<stdio.h>#include<iostream>#include<stdlib.h>using namespace std;typedef struct{char vexs[20];int edges[20][20];int n,e;}MGraph;int CreatMGraph(MGraph *G){int i,j;char a,b[50];int k;cin>>G->n;cin>>b;for(i=0;i<G->n;i++){G->vexs[i]=b[i];}for(i=0;i<G->n;i++){for(j=0;j<G->n;j++){cin>>G->edges[i][j];}}cin>>a;for(i=0;i<G->n;i++){if(G->vexs[i]==a){k=i;}}return k;}void BFS(MGraph *G,int k){int i,j,f=-1,r=-1;int visited[20];int queue[20];for(i=0;i<G->n;i++)visited[i]=0;cout<<G->vexs[k];visited[k]=1;r=(r+1)%20;queue[r]=k;while(f!=r){f=(f+1)%20;i=queue[f];for(j=0;j<G->n;j++)if(G->edges[i][j]!=0 && visited[j]==0){cout<<G->vexs[j];visited[j]=1;r=(r+1)%20;queue[r]=j;}}}int main(){int q;MGraph * G;G=(MGraph *)malloc(sizeof(MGraph));q=CreatMGraph(G);BFS(G,q);return 0;}1070邻接矩阵存储简单路径(难)#include <iostream>#include <stdio.h>using namespace std;int path[100], visited[100] = {0}, top = -1;void DFS(int a[][100], int v1, int v2, int n) {visited[v1] = 1;path[++top] = v1;int i , j;for(j = 0; j < n; j++){if(a[v1][j] != 0 && visited[j] == 0) {if(j == v2){for(i = 0; i <= top; i++)cout<<path[i];cout<<j;cout<<endl;}elseDFS(a,j,v2,n);}}top--;visited[v1] = 0;}int main(){int i, j, n, v1, v2, a[100][100];cin>>n;cin>>v1>>v2;for(i = 0; i < n; i++){for(j = 0; j < n; j++){cin>>a[i][j];}}DFS(a,v1,v2,n);return 0;}1071有向图的邻接矩阵存储顶点删除(难)#include <iostream>#include <malloc.h>#include <stdio.h>using namespace std;int visited[100];typedef struct{int no;int n;int edges[100][100];}MGraph;void Delet(MGraph *&G){G = (MGraph *)malloc(sizeof(MGraph)); int e, i, j;cin>>G->n;cin>>e;for(i = 0; i < G->n; i++){G->no = i;for(j = 0; j < G->n; j++){cin>>G->edges[i][j];}}cout<<G->n-1;cout<<endl;for(i = 0; i < G->n; i++){if(i != e)cout<<i;}cout<<endl;for(i = 0; i < G->n; i++){if(i == e)continue;for(j = 0; j < G->n; j++){if(j == e)continue;cout<<G->edges[i][j];}cout<<endl;}}int main(){MGraph *G;Delet(G);return 0;}1072 有向图的邻接矩阵存储根计算(难)#include <iostream>#include <queue>#define MAXSIZE 100using namespace std;bool bfs(const bool (*arr)[MAXSIZE],const int &n,const int &beg=0); int main(){bool arr[MAXSIZE][MAXSIZE]={0};int n;cin >> n;for(int i=0;i!=n;++i)for(int j=0;j!=n;++j)cin >> arr[i][j];for(int i=0;i!=n;++i)if(bfs(arr,n,i))cout << i;return 0;}bool bfs(const bool (*arr)[MAXSIZE],const int &n,const int &beg) {bool visited[MAXSIZE]={false};int cur,ct(0);queue<int> q;q.push(beg);visited[beg]=true;while(!q.empty()){++ct;cur=q.front();q.pop();for(int i=0;i!=n;++i)if(!visited[i]&&arr[cur][i]){q.push(i);visited[i]=true;}}return n==ct?true:false;}1075求最小生成树(Prim算法)(难)#include<cstdio>#include<cstdlib>#include<memory.h>#include<limits.h>#include<iostream>#define INF 30000using namespace std;int n,m,map[100][100];char a[100];int get(char e){for(int i=0;i<n;i++)if(a[i]==e)return i;}void read(){int i,w;char x,y;memset(map,INF,sizeof(map));cin>>n>>m;for(i=0;i<n;i++)cin>>a[i];for(i=0;i<m;i++){cin>>x>>y>>w;map[get(x)][get(y)]=w;map[get(y)][get(x)]=w;}}void prim(){int lowcost[100],adjvex[100],i,j,k; for(i=0;i<n;i++){lowcost[i]=map[0][i];adjvex[i]=0;}for(i=1;i<n;i++){int min=INF;for(j=1;j<n;j++){if(lowcost[j] && lowcost[j]<min){min=lowcost[j];k=j;}}cout<<"("<<a[adjvex[k]]<<","<<a[k]<<")"; lowcost[k]=0;for(j=0;j<n;j++){if(map[k][j] && map[k][j]<lowcost[j]) {lowcost[j]=map[k][j];adjvex[j]=k;}}}}void print(){for(int i=0;i<n;i++){for(int j=0;j<n;j++)cout<<map[i][j]<<' ';cout<<endl;}}int main(){read();//print();prim();//cout<<endl;return 0;}1076判断给定有向图是否存在回路(难)1010折半查找的实现(易)1012哈希表(链地址处理冲突)(中)1013哈希表(开放定址法处理冲突)(中)#include<iostream>using namespace std;typedef struct Data{int num;int n;}data;int n;data hash[100];void build(){int m;int i;cin>>n;for(i=0;i<n;i++)hash[i].n=0;cin>>m;for(i=0;i<m;i++){int e,t,e_;int mid=1;cin>>e;t=e%n;e_=e;while(hash[t].n!=0){mid++;t=(e_+1)%n;e_++;}hash[t].num=e;hash[t].n=mid;}}int deal(){int e;cin>>e;int e_;e_=e;while (e_%n!=e%n-1){if(hash[e_%n].num==e){cout<<e_%n<<","<<hash[e_%n].n;return 0;}elsee_++;}return -1;}int main(void){build();if(deal()==-1)cout<<-1;return 0;}1014交换排序算法的设计与实现(中) 1015堆排序算法(难)1016插入排序算法实现(易)1098堆的判断(易)1099希尔排序算法实现(中)1077平衡二叉树的判定(中)#include <iostream>#include <string.h>#include <malloc.h>using namespace std;typedef struct node{char data;node *leftchild;node *rightchild;}Node;void Init(Node *N){N = (Node *)malloc(sizeof(Node));}void CreateT(Node *&N){char ch;cin>>ch;if(ch == '#'){N = (Node *)malloc(sizeof(Node));N = NULL;return;}N = (Node *)malloc(sizeof(Node));N->data = ch;CreateT(N->leftchild);CreateT(N->rightchild);}char pre[100], in[100];int i=0, j=0;void preOrder(Node *&N){if(N == NULL)return;pre[i++] = N->data;preOrder(N->leftchild);preOrder(N->rightchild);}void inOrder(Node *&N){if(N == NULL)return;inOrder(N->leftchild);in[j++] = N->data;inOrder(N->rightchild);}int juge(char *pre, char *in, int n) {char *p;int k;if(n <= 1)return 1;for(p = in; p <= in+n; p++){if(*p == *pre)break;}k = p - in;if(n-2*k<=2 && n-2*k>=0){juge(pre,in,k);juge(pre+k,p,n-k-1);return 1;}elsereturn 0;}int main(){Node *N;CreateT(N);preOrder(N);inOrder(N);int n, flag;n = strlen(in);flag = juge(pre, in, n);if(flag == 0){cout<<"no!";return 0;}else{cout<<"yes!";return 0;}}1011二叉排序树的实现和查找(中)#include<stdio.h>#include<malloc.h>typedef struct node{int key;struct node *lchild,*rchild;}BSTNode;int insertBST(BSTNode *&p,int k){if(p==NULL){p=(BSTNode*)malloc(sizeof(BSTNode));p->key=k;p->lchild=p->rchild=NULL;return 1;}else if(k==p->key)return 0;else if(k<p->key)return insertBST(p->lchild,k);elsereturn insertBST(p->rchild,k);}BSTNode *CreatBST(int A[],int n){BSTNode *bt=NULL;int i=0;while (i<n){insertBST(bt,A[i]);i++;}return bt;}int sum=0;BSTNode *SearchBST(BSTNode *bt,int k){ sum++;if(bt==NULL){sum=-1;return bt;}if (bt->key==k)return bt;if (k<bt->key){return SearchBST(bt->lchild,k);}else{return SearchBST(bt->rchild,k);}}int main(){BSTNode *bt;int A[100],n,i,m,j;scanf("%d",&n);for(i=0;i<n;i++){scanf("%d",&A[i]);}scanf("%d",&m);bt=CreatBST(A,n);SearchBST(bt,m);printf("%d",sum);return 0;}。

相关文档
最新文档