中北数据结构2023真题

news2025/2/26 13:46:04

雪雾:
设计一个算法,将一个节点值为自然数的单列表拆分成两个单列表,原表中值为偶数的节点保留,而值为奇数的节点,按他们在原表的相对次序组成一个新的单列表

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
} Node;

void splitList(Node* head, Node** evenList, Node** oddList) {
    Node* evenTail = NULL;
    Node* oddTail = NULL;

    while (head != NULL) {
        if (head->data % 2 == 0) {
            if (*evenList == NULL) {
                *evenList = head;
                evenTail = head;
            } else {
                evenTail->next = head;
                evenTail = evenTail->next;
            }
        } else {
            if (*oddList == NULL) {
                *oddList = head;
                oddTail = head;
            } else {
                oddTail->next = head;
                oddTail = oddTail->next;
            }
        }
        head = head->next;
    }

    if (evenTail != NULL) evenTail->next = NULL;
    if (oddTail != NULL) oddTail->next = NULL;
}

// Example usage:
// Call splitList with your original list and two empty lists for even and odd nodes.
// Node* originalList;
// Node* evenList = NULL;
// Node* oddList = NULL;
// splitList(originalList, &evenList, &oddList);

雪雾:
顺序表a和b其元素值均被从小到大的顺序排列,编写一个算法,将它们合并成一个顺序表c,要求c的元素也按从小到大的升序排列

#include <stdio.h>
#include <stdlib.h>

void mergeSortedArrays(int a[], int m, int b[], int n, int c[]) {
    int i = 0, j = 0, k = 0;

    while (i < m && j < n) {
        if (a[i] < b[j]) {
            c[k++] = a[i++];
        } else {
            c[k++] = b[j++];
        }
    }

    while (i < m) {
        c[k++] = a[i++];
    }

    while (j < n) {
        c[k++] = b[j++];
    }
}

// Example usage:
// int a[] = {1, 3, 5, 7};
// int b[] = {2, 4, 6, 8};
// int c[8]; // Assuming enough space in the array
// mergeSortedArrays(a, 4, b, 4, c);

雪雾:
设计快速排序的非递归算法

#include <stdio.h>
#include <stdlib.h>

void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int partition(int arr[], int low, int high) {
    int pivot = arr[high];
    int i = (low - 1);

    for (int j = low; j <= high - 1; j++) {
        if (arr[j] < pivot) {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return (i + 1);
}

void quickSort(int arr[], int low, int high) {
    int stack[high - low + 1];
    int top = -1;

    stack[++top] = low;
    stack[++top] = high;

    while (top >= 0) {
        high = stack[top--];
        low = stack[top--];

        int pi = partition(arr, low, high);

        if (pi - 1 > low) {
            stack[++top] = low;
            stack[++top] = pi - 1;
        }

        if (pi + 1 < high) {
            stack[++top] = pi + 1;
            stack[++top] = high;
        }
    }
}

// Example usage:
// int arr[] = {64, 25, 12, 22, 11};
// int n = sizeof(arr) / sizeof(arr[0]);
// quickSort(arr, 0, n - 1);

雪雾:
二叉树前序遍历为ABCDRF,中序遍历为CBAEDF,画出此二叉数,并求该树的后序遍历

#include <stdio.h>
#include <stdlib.h>

typedef struct TreeNode {
    char data;
    struct TreeNode* left;
    struct TreeNode* right;
} TreeNode;

TreeNode* buildTree(char preorder[], char inorder[], int preStart, int preEnd, int inStart, int inEnd) {
    if (preStart > preEnd || inStart > inEnd) {
        return NULL;
    }

    TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
    root->data = preorder[preStart];

    int inIndex = inStart;
    while (inorder[inIndex] != root->data) {
        inIndex++;
    }

    int leftSubtreeSize = inIndex - inStart;

    root->left = buildTree(preorder, inorder, preStart + 1, preStart + leftSubtreeSize, inStart, inIndex - 1);
    root->right = buildTree(preorder, inorder, preStart + leftSubtreeSize + 1, preEnd, inIndex + 1, inEnd);

    return root;
}

void postorderTraversal(TreeNode* root) {
    if (root == NULL) {
        return;
    }

    postorderTraversal(root->left);
    postorderTraversal(root->right);
    printf("%c ", root->data);
}

// Example usage:
// char preorder[] = "ABCDRF";
// char inorder[] = "CBAEDF";
// TreeNode* root = buildTree(preorder, inorder, 0, 5, 0, 5);
// postorderTraversal(root);

雪雾:
对于几个顶点的无向图和有向图均为不带权图,采用邻接矩阵和邻接表表示时如何求解以下问题

1.图中有多少条边
2.任意两个顶点i和j是否有边相连
3.任意一个顶点的度是多少

#define MAX_VERTICES 100

// 邻接矩阵表示
typedef struct {
    int matrix[MAX_VERTICES][MAX_VERTICES];
    int numVertices;
} GraphMatrix;

// 邻接表表示
typedef struct Node {
    int vertex;
    struct Node* next;
} Node;

typedef struct {
    Node* head;
} LinkedList;

typedef struct {
    LinkedList array[MAX_VERTICES];
    int numVertices;
} GraphList;

// 问题解答
int countEdges(GraphMatrix* graph) {
    int count = 0;
    for (int i = 0; i < graph->numVertices; i++) {
        for (int j = 0; j < graph->numVertices; j++) {
            if (graph->matrix[i][j] != 0) {
                count++;
            }
        }
    }
    return count / 2; // For undirected graph
}

bool areVerticesConnected(GraphMatrix* graph, int i, int j) {
    return (graph->matrix[i][j] != 0);
}

int calculateVertexDegree(GraphMatrix* graph, int vertex) {
    int degree = 0;
    for (int i = 0; i < graph->numVertices; i++) {
        if (graph->matrix[vertex][i] != 0) {
            degree++;
        }
    }
    return degree;
}

// 类似的函数也可以定义在GraphList中

// Example usage:
// GraphMatrix graph;
// int edgeCount = countEdges(&graph);
// bool connected = areVerticesConnected(&graph, 0, 1);
// int degree = calculateVertexDegree(&graph, 0);

雪雾:
假设稀疏矩阵采用3元组表示,编写一个函数计算其转置矩阵b,要求b也采用3元组表示

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int row;
    int col;
    int value;
} Triplet;

void transposeSparseMatrix(Triplet a[], Triplet b[]) {
    int numTerms = a[0].value;
    b[0].row = a[0].col;
    b[0].col = a[0].row;
    b[0].value = numTerms;

    if (numTerms > 0) {
        int currentB = 1;

        for (int currentA = 0; currentA < numTerms; currentA++) {
            int currentCol = a[currentA].row;

            b[currentB].row = currentCol;
            b[currentB].col = a[currentA].col;
            b[currentB].value = a[currentA

雪雾:
含有60个叶子节点的二叉树的最小高度是多少
最小高度的二叉树是一棵平衡二叉树,其中每个叶子节点都位于最底层或倒数第二层。平衡二叉树的高度可以通过以下方式计算:

对于含有 n 个叶子节点的平衡二叉树,其高度 h 可以通过以下不等式确定:

[2^{h-1} < n \leq 2^h - 1]

在这个问题中,n = 60,我们需要找到最小的 h 满足上述不等式。可以通过迭代计算找到最小的 h:

[2^{h-1} < 60 \leq 2^h - 1]

[2^{h-1} < 60 \leq 2^h - 1]

[2^{h-1} + 1 \leq 60 \leq 2^h - 1]

[2^{h-1} + 1 \leq 60 \leq 2^h - 1]

[2^{h-1} \leq 59 \leq 2^h - 2]

因此,[h-1 \leq \log_2(59) < h]

最小的 h 是 (\lceil \log_2(59) \rceil),其中 (\lceil x \rceil) 表示不小于 x 的最小整数。计算得到:

[h = \lceil \log_2(59) \rceil = 6]

所以,含有 60 个叶子节点的二叉树的最小高度为 6。
在这里插入图片描述

雪雾:
在分块检索中,对256个元素的线性表分成多少块最好?每块的最佳长度是多少?若每块的长度为8,其平均检索的长度是多少

在分块检索(或块搜素)中,要确定最佳的块数和每块的最佳长度,可以考虑使用平方根法则。该法则认为,如果有 (n) 个元素,最佳的块数 (m) 和每块的最佳长度 (k) 分别为:

[m = \sqrt{n}]
[k = \frac{n}{m} = \sqrt{n}]

对于 256 个元素的线性表,应该分成 (m = \sqrt{256} = 16) 块,每块的最佳长度 (k = \frac{256}{16} = 16)。

如果每块的长度为 8,而不是最佳长度 16,那么平均检索长度(average search length)可以通过以下公式计算:

[L = \frac{k}{2} + \frac{n}{k}]

其中,(k) 是每块的长度,(n) 是元素的总数。

在这个情况下:

[L = \frac{8}{2} + \frac{256}{8} = 4 + 32 = 36]

所以,如果每块的长度为 8,平均检索长度将为 36。但是,为了达到最佳性能,建议使用平方根法则计算最佳的块数和每块的最佳长度。
在这里插入图片描述

雪雾:
设有一组关键字(19 1 23 14 55 20 84 27 68 11 10 77)其哈希函数为h(key)=key%13,采用拉链法解决冲突是在0~18的哈希表中对该关键字序列构造哈希表,并求在查找成功的情况下的平均查找长度

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>

#define HASH_TABLE_SIZE 13

typedef struct Node {
    int key;
    struct Node* next;
} Node;

typedef struct {
    Node* head;
} Bucket;

void insert(int key, Bucket* hashTable) {
    int hashValue = key % HASH_TABLE_SIZE;
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->key = key;
    newNode->next = hashTable[hashValue].head;
    hashTable[hashValue].head = newNode;
}

Node* search(int key, Bucket* hashTable) {
    int hashValue = key % HASH_TABLE_SIZE;
    Node* current = hashTable[hashValue].head;

    while (current != NULL) {
        if (current->key == key) {
            return current; // 找到了
        }
        current = current->next;
    }

    return NULL; // 没找到
}

double calculateASL(Bucket* hashTable) {
    int totalLength = 0;
    int totalCount = 0;

    for (int i = 0; i < HASH_TABLE_SIZE; i++) {
        Node* current = hashTable[i].head;
        int length = 0;

        while (current != NULL) {
            length++;
            current = current->next;
        }

        totalLength += length;
        totalCount += (length > 0) ? 1 : 0;
    }

    return (double)totalLength / totalCount;
}

int main() {
    int keys[] = {19, 1, 23, 14, 55, 20, 84, 27, 68, 11, 10, 77};
    int keyCount = sizeof(keys) / sizeof(keys[0]);

    // 初始化哈希表
    Bucket hashTable[HASH_TABLE_SIZE];
    for (int i = 0; i < HASH_TABLE_SIZE; i++) {
        hashTable[i].head = NULL;
    }

    // 插入关键字
    for (int i = 0; i < keyCount; i++) {
        insert(keys[i], hashTable);
    }

    // 计算平均查找长度
    double averageSearchLength = calculateASL(hashTable);

    printf("Average Search Length: %.2lf\n", averageSearchLength);

    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1394622.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Python算法例36 丑数Ⅱ

1. 问题描述 设计一个算法&#xff0c;找出只含素因子2、3、5的第n小的数&#xff0c;符合条件的数如&#xff1a;1、2、3、4、5、6、8、9、10、12… 2. 问题示例 如果n9&#xff0c;返回10。 3. 代码实现 def find_nth_number(n):if n < 0:return Nonenumbers [1]idx…

Python-基础篇-类与对象/面向对象程序设计

文章目录 思维导图是何物类定义类&#x1f4da; class类的成员&#x1f4da;类的继承性&#x1f4da;封装性&#x1f4da;多态性 对象面向对象&#x1f4da;创建对象&#x1f4da;销毁对象&#x1f4da; 类和对象关系必背必记专业英语学习角 思维导图 是何物 类 “类”是物以…

DotNET 8 新特性 - AoT 编译、 MinimalAPI、Json源生成器

AoT编译方式特性 裁剪减小体积&#xff0c;取消JIT编译&#xff0c;不使用反射技术。直接产生目标机器二进制代码&#xff0c;目前支持x86&#xff0c;解决被反编译问题。 使用本机 AOT 发布的应用&#xff1a;最大程度减少了磁盘占用空间缩短了启动时间减少了内存需求 | 功能…

springcloud +Vue 前后端分离的onlinejudge在线评测系统

功能描述&#xff1a; 本系统的研究内容主要是设计并实现一个一个在线测评系统&#xff08;OJ&#xff09;&#xff0c;该系统集成了博客、竞赛、刷题、教学&#xff0c;公告&#xff0c;个人管理六大功能&#xff0c;用户注册后登录系统&#xff0c;可以浏览本站的全部文章、发…

Nginx 简介

1、概念介绍 Nginx ("engine x") 是一个轻量级、高性能的 WEB 服务器软件和反向代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的&#xff0c;第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。其将源代码以类 BSD 许可证的形式发…

67.基于控制流编程,优化Go代码可读性

文章目录 一&#xff1a;简介二、代码整洁小技巧&#xff1a;基于控制流编程三、总结 代码地址&#xff1a;https://gitee.com/lymgoforIT/golang-trick/tree/master/40-clean-code-controll-flow 一&#xff1a;简介 如何写好代码&#xff0c;一千个人眼中有一千个哈姆雷特&…

Docker之nacos集群部署

前言 Nacos 是一个开源的注册中心和配置中心&#xff0c;用于实现微服务架构中的服务发现、服务治理和动态配置管理。在 Docker 中使用 Nacos&#xff0c;你可以通过拉取官方提供的 Docker 镜像并运行容器的方式来快速部署 一.Nacos镜像拉取及独立模式 镜像拉取命令详解&…

用3D渲染效果图时,需要注意什么?

要用3D渲染效果图&#xff0c;您需要使用3D建模软件来创建三维模型、材质、灯光和相机等元素&#xff0c;并使用渲染引擎将它们渲染成逼真的图像。那么用3D渲染效果图需要注意什么? 在用3D渲染效果图时&#xff0c;您需要注意以下几点&#xff1a; 1、模型精度&#xff1a;模型…

qnx 上screen + egl + opengles 最简实例

文章目录 前言一、qnx 上的窗口系统——screen二、screen + egl + opengles 最简实例1.使用 addvariant 命令创建工程目录2. 添加源码文件3. common.mk 文件4. 编译与执行总结参考资料前言 本文主要介绍如何在QNX 系统上使用egl和opengles 控制GPU渲染一个三角形并显示到屏幕上…

在自定义数据集上训练 YOLOv8 进行目标检测

这是目标检测中令人惊叹的 AI 模型之一。在这种情况下&#xff0c;您无需克隆存储库、设置要求并配置模型&#xff0c;就像在 YOLOv5 及其之前的版本中所做的那样。 在 YOLOv8 中&#xff0c;不需要执行这些手动任务。您只需安装 Ultralytics 即可&#xff0c;我将向您展示如何…

C++:类(Class)

内容整理自&#xff1a;The Cherno:C系列&#xff0c;2024年1月18日 --------------------------------------------------------------------------------------------------------------------------------- 简单地说&#xff0c; 类&#xff0c;只是 数据功能 的整合体。…

SPI传感器接口设计与优化:基于STM32的实践

SPI&#xff08;串行外设接口&#xff09;是一种常用的串行通信协议&#xff0c;用于在微控制器和外部设备之间进行全双工的高速数据传输。在本文中&#xff0c;我们将探讨如何基于STM32微控制器设计和优化SPI传感器接口&#xff0c;并提供相应的代码示例。 1. SPI传感器接口设…

RXJS中Subject, BehaviorSubject, ReplaySubject, AsyncSubject的区别?

在RxJS&#xff08;Reactive Extensions for JavaScript&#xff09;中&#xff0c;Subject、BehaviorSubject、ReplaySubject和AsyncSubject都是Observable的变体&#xff0c;它们用于处理观察者模式中的不同场景。以下是它们之间的主要区别&#xff1a; 1、Subject: 是一种特…

【VMware】安装和卸载VMware的Ubuntu

安装视频&#xff1a;&#xff08;全程无废话&#xff09; https://www.bilibili.com/video/BV1W34y1k7ge/?spm_id_from333.337.search-card.all.click&vd_sourcefb8dcae0aee3f1aab700c21099045395

SegVol: Universal and Interactive Volumetric Medical Image Segmentation

Abstract 精确的图像分割为临床研究提供了有意义且结构良好的信息。尽管在医学图像分割方面取得了显著的进展&#xff0c;但仍然缺乏一种能够分割广泛解剖类别且易于用户交互的基础分割模型。 本文提出了一种通用的交互式体医学图像分割模型——SegVol。通过对90k个未标记的C…

Midjourney 1 月 17 日官方版本补丁更新公告,可能于二月底或三月向公众开放

Midjourney专区&#xff1a;Midjourney-喜好儿aigc 更多消息&#xff1a;AI人工智能行业动态&#xff0c;aigc应用领域资讯 功能更新和开发 修复/改变区域和缩小功能预计在本周或下周推出 测试一致的样式&#xff0c;强调样式而不是内容 常量字符功能正在开发中&#xff0c;但…

如何使用批量图片转换工具快速转换图片格式

你好!在我们的日常生活中&#xff0c;我们常常需要处理大量的图片。而有时候&#xff0c;我们可能会遇到一种情况&#xff0c;就是需要将多张图片批量转换为特定的格式&#xff0c;比如将图片批量转换为PNG格式。那么&#xff0c;如何实现这一操作呢?在本文中&#xff0c;我将…

详解IP安全:IPSec协议簇 | AH协议 | ESP协议 | IKE协议_ipsec esp

目录 IP安全概述 IPSec协议簇 IPSec的实现方式 AH&#xff08;Authentication Header&#xff0c;认证头&#xff09; ESP&#xff08;Encapsulating Security Payload&#xff0c;封装安全载荷&#xff09; IKE&#xff08;Internet Key Exchange&#xff0c;因特网密钥…

漏洞检测和评估【网站子域扫描工具02】

上一篇&#xff1a;爬取目标网站的域名和子域名【网站子域扫描工具01】 在Python中&#xff0c;有一些流行的漏洞扫描库可以对子域进行漏洞扫描和评估&#xff0c;比如Nmap、Sublist3r等。 1.端口扫描 以下是一个简单的示例代码&#xff0c;展示了如何使用Nmap进行基本的端口扫…

基于springboot+vue的高校心理教育辅导系统(前后端分离)

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战 主要内容&#xff1a;毕业设计(Javaweb项目|小程序等)、简历模板、学习资料、面试题库、技术咨询 文末联系获取 项目背景…