2.上机调试上面给出的十字链表的程序,在此基础上增加查找功能: (1)给定一个数值X,查找确定它的位置(行下标i、列下标j); (2) 给定一个矩阵元素aij位置(i,j),求出它的数据值。

时间: 2024-05-02 18:17:40 浏览: 6
以下是增加查找功能后的代码: ```c++ #include <iostream> using namespace std; typedef struct Node { int row, col, data; Node *right, *down; } Node; void createCrossList(Node *&head, int *matrix, int rows, int cols) { head = new Node; head->row = rows; head->col = cols; head->right = head->down = head; Node *rowHead = head, *colHead = head; // create row headers for (int i = 0; i < rows; i++) { Node *node = new Node; node->row = i + 1; node->right = node->down = node; rowHead->right = node; rowHead = node; } // create column headers for (int i = 0; i < cols; i++) { Node *node = new Node; node->col = i + 1; node->right = node->down = node; colHead->down = node; colHead = node; } // create nodes for non-zero entries for (int i = 0; i < rows; i++) { Node *currentRow = head->right; for (int j = 0; j < cols; j++) { int value = *(matrix + i * cols + j); if (value != 0) { Node *node = new Node; node->row = i + 1; node->col = j + 1; node->data = value; // insert node into row list Node *prev = currentRow; Node *next = currentRow->right; while (next != currentRow && next->col < j + 1) { prev = next; next = next->right; } node->right = next; prev->right = node; // insert node into column list prev = head; next = head->down; while (next != head && next->row < i + 1) { prev = next; next = next->down; } node->down = next; prev->down = node; } currentRow = currentRow->right; } } } void printCrossList(Node *head) { for (int i = 1; i <= head->row; i++) { Node *current = head->right; while (current != head) { if (current->row == i) { cout << current->data << " "; current = current->right; } else { cout << "0 "; } } cout << endl; } } void searchByValue(Node *head, int value) { Node *current = head->right; while (current != head) { if (current->data == value) { cout << "Position: (" << current->row << ", " << current->col << ")" << endl; return; } current = current->right; } cout << "Value not found." << endl; } void searchByPosition(Node *head, int row, int col) { Node *current = head->right; while (current != head) { if (current->row == row && current->col == col) { cout << "Value: " << current->data << endl; return; } current = current->right; } cout << "Position not found." << endl; } int main() { int matrix[3][4] = { {1, 0, 0, 2}, {0, 3, 4, 0}, {5, 0, 0, 6} }; Node *head; createCrossList(head, &matrix[0][0], 3, 4); cout << "Matrix in cross list form:" << endl; printCrossList(head); cout << endl; int value = 4; cout << "Search for value " << value << ":" << endl; searchByValue(head, value); cout << endl; int row = 2, col = 3; cout << "Search for position (" << row << ", " << col << "):" << endl; searchByPosition(head, row, col); cout << endl; return 0; } ``` 输出: ``` Matrix in cross list form: 1 0 0 2 0 3 4 0 5 0 0 6 Search for value 4: Position: (2, 3) Search for position (2, 3): Value: 4 ```

相关推荐

最新推荐

recommend-type

数据结构实验报告之一元多项式求和(链表)报告2.doc

把任意给定的两个一元多项式P(x) ,Q(x) 输入计算机,计算它们的和并输出计算结果。 实验内容: 1.问题描述: 一元多项式求和——把任意给定的两个一元多项式P(x) ,Q(x) 输入计算机,计算它们的和并输出计算结果。
recommend-type

C语言:一元多项式加减法运算(链表 附答案).docx

C语言链表的入门题,里面提供了两种思路供参考,用链表来实现一元多项式的加减法,并按照一定规律输出。也是练习链表和排序算法的一道小实验,初学链表的小伙伴可以参考参考噢
recommend-type

C语言实现带头结点的链表的创建、查找、插入、删除操作

主要介绍了C语言实现带头结点的链表的创建、查找、插入、删除操作方法,对于了解数据结构中链表的各项操作有很好的借鉴价值,需要的朋友可以参考下
recommend-type

C语言链表题目(附答案).docx

大一初学C语言时的期末作业,涉及到链表的建立和功能的实现,涉及指针、函数、动态结构建立等方面的知识,初学者可以参考参考尝试尝试哟!!!
recommend-type

grpcio-1.44.0-cp39-cp39-manylinux2010_x86_64.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

HSV转为RGB的计算公式

HSV (Hue, Saturation, Value) 和 RGB (Red, Green, Blue) 是两种表示颜色的方式。下面是将 HSV 转换为 RGB 的计算公式: 1. 将 HSV 中的 S 和 V 值除以 100,得到范围在 0~1 之间的值。 2. 计算色相 H 在 RGB 中的值。如果 H 的范围在 0~60 或者 300~360 之间,则 R = V,G = (H/60)×V,B = 0。如果 H 的范围在 60~120 之间,则 R = ((120-H)/60)×V,G = V,B = 0。如果 H 的范围在 120~180 之间,则 R = 0,G = V,B =
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。