如何制作链表数组,其列表指向C中的矩阵行?
我有一种类型:如何制作链表数组,其列表指向C中的矩阵行?,c,linked-list,C,Linked List,我有一种类型: typedef struct node { double data; struct node * next; }NODE; 它表示一个链表节点 我有一个功能: void add(NODE ** head, double data) { NODE * new = NULL; new = malloc(sizeof(NODE)); new->next = NULL; new->data = data; if(* h
typedef struct node
{
double data;
struct node * next;
}NODE;
它表示一个链表节点
我有一个功能:
void add(NODE ** head, double data)
{
NODE * new = NULL;
new = malloc(sizeof(NODE));
new->next = NULL;
new->data = data;
if(* head == NULL) * head = new;
else
{
NODE * temp = NULL;
for(temp = * head; temp->next; temp = temp->next);
temp->next = new;
}
}
在列表的末尾添加一个新的数字
我需要编写一个函数(使用函数“add”)来形成并返回一个动态分配的“n”链表数组(数组的每个元素指向列表的第一个节点),该数组基于一个维度为“nxm”的矩阵,其中第k个列表的元素应与矩阵的第k阶元素相对应
功能原型:
NODE ** mat2arr(double ** matrix, int n, int m)
其中“矩阵”表示指针数组、n行数和m列数
我试着这样做,但它给我的分割错误。如何修复代码或用新的想法重写代码,以及如何访问返回的链表数组以便打印值
NODE ** mat2arr(double ** matrix, int n, int m)
{
NODE ** array_list = NULL;
array_list = malloc(n * sizeof(NODE *));
for(int i = 0; i < n; i++)
{
NODE * list = NULL;
for(int j = 0; j < m; j++)
add(&list, matrix[i][j]);
array_list[i] = list;
}
return array_list;
}
这似乎有效
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
double data;
struct node * next;
}NODE;
NODE * add(NODE * head, double data)
{
NODE * new = NULL;
new = malloc(sizeof(NODE));
new->next = NULL;
new->data = data;
if( head == NULL) {
head = new;
}
else
{
NODE * temp = head;
while( temp->next) {
temp = temp->next;
}
temp->next = new;
}
return head;
}
NODE ** mat2arr ( int n, int m, double **matrix)
{
NODE ** array_list = NULL;
array_list = calloc(n, sizeof(NODE *));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++) {
array_list[i] = add(array_list[i], matrix[i][j]);
}
}
return array_list;
}
int main ( void) {
double **myArray = NULL;
NODE ** array_list = NULL;
myArray = calloc ( sizeof *myArray, 5);
for ( int i = 0; i < 5; ++i) {
myArray[i] = calloc ( sizeof **myArray, 3);
for ( int j = 0; j < 3; ++j) {
myArray[i][j] = ( i + 1) * ( j + 1);
}
}
array_list = mat2arr( 5, 3, myArray);
for(int i = 0; i < 5; i++) {
NODE * each = array_list[i];
while ( each) {
printf ( "data %f\n", each->data);
each = each->next;
}
printf ( "\n");
}
for(int i = 0; i < 5; i++) {
NODE * each = array_list[i];
while ( each) {
NODE *temp = each;
each = each->next;
free ( temp);
}
free ( myArray[i]);
}
free ( array_list);
free ( myArray);
return 0;
}
#包括
#包括
类型定义结构节点
{
双重数据;
结构节点*下一步;
}节点;
节点*添加(节点*头,双数据)
{
NODE*new=NULL;
新=malloc(sizeof(NODE));
新建->下一步=空;
新建->数据=数据;
if(head==NULL){
头=新的;
}
其他的
{
节点*温度=头部;
while(临时->下一步){
温度=温度->下一步;
}
临时->下一步=新建;
}
回流头;
}
节点**mat2arr(整数n、整数m、双**矩阵)
{
节点**数组\列表=NULL;
array_list=calloc(n,sizeof(NODE*);
对于(int i=0;i数据);
每个=每个->下一个;
}
printf(“\n”);
}
对于(int i=0;i<5;i++){
节点*each=array_list[i];
而(每个){
节点*温度=每个节点;
每个=每个->下一个;
免费(临时);
}
free(myArray[i]);
}
自由(数组_列表);
免费(myArray);
返回0;
}
#包括
#包括
类型定义结构节点
{
双重数据;
结构节点*下一步;
}节点;
空添加(节点**头,双数据)
{
NODE*new=NULL;
新=malloc(sizeof(NODE));
新建->下一步=空;
新建->数据=数据;
如果(*head==NULL)*head=new;
其他的
{
节点*temp=NULL;
用于(温度=*磁头;温度->下一步;温度=温度->下一步);
临时->下一步=新建;
}
}
节点**mat2arr(双**矩阵,整数n,整数m)
{
节点**数组\列表=NULL;
array_list=malloc(n*sizeof(NODE*));
对于(int i=0;inext;
自由(*头);
*压头=温度;
}
}
void main()
{
printf(“输入矩阵2x3:\n”);
双**矩阵=空;
矩阵=malloc(2*sizeof(双*);
对于(int i=0;i<2;i++)
{
double*array=NULL;
数组=malloc(3*sizeof(double));
对于(int j=0;j<3;j++)
{
printf(“矩阵[%d][%d]:”,i,j);
scanf(“%lf”,&array[j]);
}
矩阵[i]=数组;
}
节点**数组\列表=NULL;
数组_list=mat2arr(矩阵,2,3);
对于(int i=0;i<2;i++)
{
节点*temp=NULL;
temp=数组_列表[i];
while(临时)
{
printf(“%lf”,临时->数据);
温度=温度->下一步;
}
printf(“\n”);
}
对于(int i=0;i<2;i++)
{
删除_列表(&数组_列表[i]);
自由(矩阵[i]);
}
自由(矩阵);
自由(数组_列表);
}
为了明确您的目标,您似乎需要一个稀疏/可变维度的二维数组,数组的大小为双
???如果是这样,链表[如您所定义的]为每个数据单元都有一个next
指针——这是非常浪费的。所以,我假设你想要我第一次描述的2D数组[你的第二个代码块],但是代码被破坏了?也就是说,您[实际上]不需要节点
结构,而是更像:typedef struct{int-width;int-height;double*data}数组代码>用于创建数组时固定的可变维度?或者,是否需要使每行的长度/计数可变且独立,例如:typedef struct{int-width;double*data;}row;typedef结构{int height;ROW*rows;}数组代码>您可以随时添加/删除行,并且可以独立于任何其他行的长度来增长/收缩任何[给定]行???@CraigEstey这是我的考试任务,据我所知,我需要为矩阵中的每一行创建一个单独的链接列表。然后我需要使用列表数组指向每个链表。我无法更改类型定义,我需要按原样使用它。好吧,有点。。。考虑一个正常的2D数组:<代码>双MyLa[ 5 ] [ 3 ] < /代码>。获取一行的地址是:double*rowptr=myarray[i]
。但是,对于一个行的链表,查找一行是LL遍历[这很慢]。但是,好吧。LL不是行的链接列表,而是数据单元的LL。您可能需要:struct rowptr{double*data;struct rowptr*next;}
注意,data
是double*
而不仅仅是double
。因此,我对所示的节点定义表示怀疑。确切的数字是多少
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
double data;
struct node * next;
}NODE;
NODE * add(NODE * head, double data)
{
NODE * new = NULL;
new = malloc(sizeof(NODE));
new->next = NULL;
new->data = data;
if( head == NULL) {
head = new;
}
else
{
NODE * temp = head;
while( temp->next) {
temp = temp->next;
}
temp->next = new;
}
return head;
}
NODE ** mat2arr ( int n, int m, double **matrix)
{
NODE ** array_list = NULL;
array_list = calloc(n, sizeof(NODE *));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++) {
array_list[i] = add(array_list[i], matrix[i][j]);
}
}
return array_list;
}
int main ( void) {
double **myArray = NULL;
NODE ** array_list = NULL;
myArray = calloc ( sizeof *myArray, 5);
for ( int i = 0; i < 5; ++i) {
myArray[i] = calloc ( sizeof **myArray, 3);
for ( int j = 0; j < 3; ++j) {
myArray[i][j] = ( i + 1) * ( j + 1);
}
}
array_list = mat2arr( 5, 3, myArray);
for(int i = 0; i < 5; i++) {
NODE * each = array_list[i];
while ( each) {
printf ( "data %f\n", each->data);
each = each->next;
}
printf ( "\n");
}
for(int i = 0; i < 5; i++) {
NODE * each = array_list[i];
while ( each) {
NODE *temp = each;
each = each->next;
free ( temp);
}
free ( myArray[i]);
}
free ( array_list);
free ( myArray);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
double data;
struct node * next;
}NODE;
void add(NODE ** head, double data)
{
NODE * new = NULL;
new = malloc(sizeof(NODE));
new->next = NULL;
new->data = data;
if(* head == NULL) * head = new;
else
{
NODE * temp = NULL;
for(temp = * head; temp->next; temp = temp->next);
temp->next = new;
}
}
NODE ** mat2arr(double ** matrix, int n, int m)
{
NODE ** array_list = NULL;
array_list = malloc(n * sizeof(NODE *));
for(int i = 0; i < n; i++)
{
NODE * list = NULL;
for(int j = 0; j < m; j++)
{
add(&list, matrix[i][j]);
}
array_list[i] = list;
}
return array_list;
}
void delete_list(NODE ** head)
{
while(* head)
{
NODE * temp = (* head)->next;
free(* head);
* head = temp;
}
}
void main()
{
printf("Input matrix 2x3: \n");
double ** matrix = NULL;
matrix = malloc(2 * sizeof(double *));
for(int i = 0; i < 2; i++)
{
double * array = NULL;
array = malloc(3 * sizeof(double));
for(int j = 0; j < 3; j++)
{
printf("Matrix[%d][%d]: ", i, j);
scanf("%lf", &array[j]);
}
matrix[i] = array;
}
NODE ** array_list = NULL;
array_list = mat2arr(matrix, 2, 3);
for(int i = 0; i < 2; i++)
{
NODE * temp = NULL;
temp = array_list[i];
while(temp)
{
printf("%lf ", temp->data);
temp = temp->next;
}
printf("\n");
}
for(int i = 0; i < 2; i++)
{
delete_list(&array_list[i]);
free(matrix[i]);
}
free(matrix);
free(array_list);
}