中北数据结构2023真题

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

#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.mfbz.cn/a/331956.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

一文教你V3+TS(保姆级教程)

TS 与 JS 的区别 基础类型 ts 的常用类型 ts 的常用基础类型分为两种&#xff1a; js 已有类型 原始类型&#xff1a;number/string/boolean/null/undefined/symbol 对象类型&#xff1a;object&#xff08;包括&#xff0c;数组、对象、函数等对象&#xff09; 每次编写前…

oracle11g的闪回技术-闪回表-时间戳

--数据库闪回表 --1创建表&#xff08;登录模式system&#xff09; CREATE table dept2 as select * from dept;--此语句如果加上where条件可用于工作中数据的临时备份 select * from dept2;--查询新建表信息 --进入sql>set time on 通过时间点闪回 记录弹出的时间点&#…

软件测试|使用Python生成PDF文件

简介 PDF&#xff08;Portable Document Format&#xff09;是一种常用的文档格式&#xff0c;具有跨平台兼容性、保真性、安全性和交互性等特点。我们日常生活工作中的合同、报告、论文等通常都采用PDF格式&#xff0c;以确保文档在不同的操作系统&#xff08;例如 Windows、…

腾讯云轻量化应用服务器_轻量化应用服务器_轻量化私有云

腾讯云轻量应用服务器开箱即用、运维简单的轻量级云服务器&#xff0c;CPU内存带宽配置高并且价格特别便宜&#xff0c;大带宽&#xff0c;但是限制月流量&#xff0c;轻量2核2G3M带宽62元一年、2核2G4M优惠价118元一年&#xff0c;540元三年、2核4G5M带宽218元一年&#xff0c…

【不用找素材】ECS 游戏Demo制作教程(1) 1.15

一、项目设置 版本&#xff1a;2022.2.0f1 &#xff08;版本太低的话会安装不了ECS插件&#xff09; 模板选择3D URP 进来后移除URP&#xff08;因为并不是真的需要&#xff0c;但也不是完全不需要&#xff09; Name: com.unity.entities.graphics Version: 1.0.0-exp.8 点击…

推荐 10 个基于 Stable Diffusion 的 AI 绘画网站

在当今快速发展的人工智能领域&#xff0c;AI 绘画已经成为一个不可忽视的趋势。特别是基于 Stable Diffusion 技术的 AI 绘画工具&#xff0c;以其强大的图像生成能力和丰富的创意潜力吸引了众多艺术家和设计师的目光。对于那些热爱艺术创作&#xff0c;但又缺乏专业绘画技巧的…

三、MySQL实例初始化、设置、服务启动关闭、环境变量配置、客户端登入(一篇足以从白走到黑)

目录 1、选择安装的电脑类型、设置端口号 2、选择mysql账号密码加密规则 3、设置root账户密码 4、设置mysql服务名和服务启动策略 5、执行设置&#xff08;初始化mysql实例&#xff09; 6、完成设置 7、MySQL数据库服务的启动和停止 方式一&#xff1a;图形化方式 方式…

《Python数据分析技术栈》第01章 03 Python基础(Python Basics)

03 Python基础&#xff08;Python Basics&#xff09; 《Python数据分析技术栈》第01章 03 Python基础&#xff08;Python Basics&#xff09; In this section, we get familiar with the syntax of Python, commenting, conditional statements, loops, and functions. 在…

LRU Cache

文章目录 1. 什么是LRU Cache2. LRU Cache的实现3. LRU Cache的OJ题目分析AC代码 1. 什么是LRU Cache LRU是Least Recently Used的缩写&#xff0c;意思是最近最少使用&#xff0c;它是一种Cache替换算法。 什么是Cache&#xff1f; 狭义的Cache指的是位于CPU和主存间的快速RAM…

linux 更新镜像源

打开终端&#xff0c;备份一下旧的 源 文件&#xff0c;以防万一 cd /etc/apt/ ls sudo cp sources.list sources.list.bak ls然后打开清华大学开源软件镜像站 搜索一下你的linux发行版本&#xff0c;我这里是ubuntu发行版本 点击这个上面图中的问号 查看一下自己的版本号&a…

【控制篇 / 分流】(7.4) ❀ 03. 对国内和国际IP网段访问进行分流 ❀ FortiGate 防火墙

【简介】公司有两条宽带用来上网&#xff0c;一条电信&#xff0c;一条IPLS国际专线&#xff0c;由于IPLS仅有2M&#xff0c;且价格昂贵&#xff0c;领导要求&#xff0c;访问国内IP走电信&#xff0c;国际IP走IPLS&#xff0c;那么应该怎么做&#xff1f; 国内IP地址组 我们已…

KubeSphere 开源社区 2023 年度回顾与致谢

2023 年结束了&#xff0c;让我们再一次一起回顾一下 KubeSphere 开源社区在过去一年的变化。更重要的是&#xff0c;本篇文章将会对 2023 年所有参与过 KubeSphere 社区贡献的成员致以最诚挚的感谢&#xff0c;快来看看有没有你&#xff01; 开源项目发展情况 2023 年&#…

黑马 Javaweb - MySQL 精华篇

我是南城余&#xff01;阿里云开发者平台专家博士证书获得者&#xff01; 欢迎关注我的博客&#xff01;一同成长&#xff01; 一名从事运维开发的worker&#xff0c;记录分享学习。 专注于AI&#xff0c;运维开发&#xff0c;windows Linux 系统领域的分享&#xff01; 知…

查询数据库表字段具有某些特征的表

目录 引言举例总结 引言 当我们把一个项目做完以后&#xff0c;客户要求我们把系统中所有的电话&#xff0c;证件号等进行加密处理时&#xff0c;我们难道要一个表一表去查看那些字段是电话和证件号码吗&#xff1f; 这种办法有点费劲&#xff0c;下面我们来探索如何找到想要的…

mybatis分页、延迟加载、立即加载、一级缓存、二级缓存

mybatis分页、延迟加载、立即加载、一级缓存、二级缓存 分页延迟加载和立即加载缓存一级缓存二级缓存 分页 分类&#xff1a; 使用Limit&#xff0c;来进行分页&#xff1b;物理分页使用RowBounds集合来保存分页需要数据&#xff0c;来进行分页;逻辑分页&#xff1b;本质是全…

Air780E开发板开发环境搭建

开发板原理图 开发软件 下载网站 https://luatos.com/luatools/download/last 使用教程 烧录教程 - LuatOS 文档 开发流程 首先下载最新版本的Luatools 然后新建一个Luatools文件夹&#xff0c;将下载的exe文件放入其中后&#xff0c;再打开exe文件&#xff08;会生成目…

《WebKit 技术内幕》之四(2): 资源加载和网络栈

2.Chromium 多进程资源加载 2,1 多进程 资源的实际加载在各个WebKit移植中有不同的实现。Chromium采用的多进程的资源加载机制。 ResourceHandle 类之下的部分是不同移植对获取资源的不同实现&#xff0c;Chromium 中是 多进程资源加载 。主要是多个Renderer进程和Browser进程…

SystemVerilog验证测试平台

2.2定宽数组 相比于 Verilog1995中的一维定宽数组, System verilog提供了更加多样的数组类型,功能上也大大增强。 2.2.1定宽数组的声明和初始化 Verilog要求在声明中必须给出数组的上下界。因为几乎所有数组都使用0作为索引下界,所以 System verilog允许只给出数组宽度的便捷声…

华为DHCP配置

1. 全局地址池和接口地址池的应用场景有什么不同呢&#xff1f; 答&#xff1a;接口地址池适用于当前接口只给DHCP client分配与接口同一网段的IP地址的场景。 全局地址池可以给DHCP Client分配与接口同网段的IP地址&#xff0c;也可以分配不同网段的IP地址&#xff08;DHCP中…

Python爬虫 - 网易云音乐下载

爬取网易云音乐实战&#xff0c;仅供学习&#xff0c;不可商用&#xff0c;出现问题&#xff0c;概不负责&#xff01; 分为爬取网易云歌单和排行榜单两部分。 因为网页中&#xff0c;只能显示出歌单的前20首歌曲&#xff0c;所以仅支持下载前20首歌曲&#xff08;非VIP音乐&…
最新文章