C 使用链表创建矩阵
我的程序中的C 使用链表创建矩阵,c,memory-management,matrix,linked-list,C,Memory Management,Matrix,Linked List,我的程序中的display_matrix功能有问题,并且在运行程序时也遇到了分段错误,我不知道这是否与我还没有销毁内存功能有关。我的程序读取矩阵的维数,然后创建从1到100的随机数,生成矩阵,然后显示它,然后释放分配的内存。我相信对于我的显示函数,我必须使用嵌套的for循环来打印值。感谢您的帮助,并提前向您表示感谢。下面是我现在拥有的代码以及我的程序的示例输出 #include<stdio.h> #include<stdlib.h> #include<time.h&
display_matrix
功能有问题,并且在运行程序时也遇到了分段错误,我不知道这是否与我还没有销毁内存功能有关。我的程序读取矩阵的维数,然后创建从1到100的随机数,生成矩阵,然后显示它,然后释放分配的内存。我相信对于我的显示函数,我必须使用嵌套的for循环来打印值。感谢您的帮助,并提前向您表示感谢。下面是我现在拥有的代码以及我的程序的示例输出
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct MatrixNode_{
int value;
struct MatrixNode_ *next_row;
struct MatrixNode_ *next_col;
}MatrixNode;
void create_linked_list_matrix(MatrixNode** head, const int rows, const int cols);
MatrixNode* create_node(void);
void display_matrix(MatrixNode* head);
void destroy_linked_list_matrix(MatrixNode** head);
int main (int argc, char *argv[]) {
if (argc < 3) {
exit(1);
}
srand(time(NULL));
const int rows = atoi(argv[1]);
const int cols = atoi(argv[2]);
MatrixNode* head_a = NULL;
printf("Matrix A\n");
create_linked_list_matrix(&head_a,rows,cols);
display_matrix(head_a);
destroy_linked_list_matrix(&head_a);
}
MatrixNode* create_node(void){
MatrixNode *temp = (MatrixNode *)malloc(sizeof(MatrixNode));
if(temp == NULL){
printf("Memory alloc error");
exit(1);
}
temp->next_col = NULL;
temp->next_row = NULL;
temp->value = rand() % 100 + 1;
return temp;
}
/*The MatrixNode double pointer will be NULL when first coming in,
make sure to allocate space For it and adjust your linked list of linked
list to start from 1 not 0. Next allocate a linked list of
MatrixNodes using the next row as the next node in the linked list.
You will need to create the linked list length up to the passed in rows value.
After the allocation of the rows linked list, we need to allocate a separate
linked list for each of the next_col MatrixNode pointers in the rows linked list.
To create the linked list for the columns create the linked list of MatrixNodes
using the next_col as the next node in the linked list. You will need to create
the linked list length up to the passed in cols value.
Use the create_node function to create nodes for your linked list.
*/
void create_linked_list_matrix(MatrixNode **head, const int rows, const int cols){
MatrixNode *tmpabove = NULL, *tmpleft = NULL, *tmpaboveleft = NULL, *newnode = NULL;
int i, y;
for( i = 0; i < rows; i++){
tmpleft = NULL;
for( y = 0; y < cols; y++){
newnode = create_node();
if(tmpabove != NULL) tmpabove->next_row = newnode;
if(tmpleft != NULL) tmpleft->next_col = newnode;
else{
tmpaboveleft = newnode;
tmpleft = newnode;
}
tmpabove = tmpabove->next_col;
tmpleft = tmpleft->next_col;
}
tmpabove = tmpaboveleft;
}}
void display_matrix(MatrixNode* head){
MatrixNode *temp = head;
while(temp != NULL){
printf("%d", temp->val);
temp = temp->next_col;
}
temp = temp->next_row;
}
好的,让我们仔细看看你的函数 假设您使用create_node()功能创建了一个新节点
new = create_node() //You state that new points to that new node.
new = new->next_row; //You state that new points to NULL again
如您所见,一个节点和下一个节点之间没有连接。
要解决此问题,我将使用帮助指针:
MatrixNode *new,*tmp = head;
new = create_node();
if (prev != NULL) {
prev->next = new;
prev = prev->next;
}
else //Means that this is the first element
prev=new;
更改这两个循环,并询问是否需要更多帮助
MatrixNode *tmpabove = NULL, *tmpleft = NULL, *tmpaboveleft = NULL, *newnode = NULL;
for(i = 0; i < rows; i++){
tmpleft = NULL;
for(y = 0; y < cols; y++){
newnode = create_node();
if (tmpabove != NULL) tmpabove -> nextrow = newnode;
if (tmpleft != NULL) tmpleft -> nextcolumn = newnode;
else {
tmpaboveleft = newnode;
tmpleft = newnode;
}
tmpabove = tmpabove -> nextcolumn;
tmpleft = tmpleft -> nextcolumn;
}
tmpabove = tmpaboveleft;
}
MatrixNode*tmpabove=NULL,*tmpleft=NULL,*tmpaboveleft=NULL,*newnode=NULL;
对于(i=0;inextrow=newnode;
如果(tmpleft!=NULL)tmpleft->nextcolumn=newnode;
否则{
tmpaboveleft=newnode;
tmpleft=newnode;
}
tmpabove=tmpabove->nextcolumn;
tmpleft=tmpleft->nextcolumn;
}
tmpabove=tmpaboveleft;
}
void create_linked_list_matrix(MatrixNode **head, const int rows, const int cols){
MatrixNode *mat[rows][cols];//use malloc if the number of elements is large
int r, c;
for(r = 0; r < rows; ++r){
for(c = 0; c < cols; ++c){
mat[r][c] = create_node();
}
}
for(r = 0; r < rows; ++r){
for(c = 0; c < cols; ++c){
if(c < cols -1)
mat[r][c]->next_col = mat[r][c+1];
if(r < rows -1)
mat[r][c]->next_row = mat[r+1][c];
}
}
*head = mat[0][0];
}
void display_matrix(MatrixNode *head){
MatrixNode *row = head;
while(row){
MatrixNode *col = row;
while(col){
printf("%d\t", col->value);
col = col->next_col;
}
printf("\n");
row = row->next_row;
}
}
void创建链接列表矩阵(矩阵节点**头,常量整数行,常量整数列){
MatrixNode*mat[rows][cols];//如果元素数量较大,则使用malloc
int r,c;
对于(r=0;rnext_col=mat[r][c+1];
如果(r下一行=mat[r+1][c];
}
}
*头=垫[0][0];
}
无效显示矩阵(矩阵节点*头){
MatrixNode*行=头;
while(世界其他地区){
MatrixNode*col=行;
while(col){
printf(“%d\t”,列->值);
col=col->next\u col;
}
printf(“\n”);
行=行->下一行;
}
}
简化版
void create_linked_list_matrix(MatrixNode **head, const int rows, const int cols){
MatrixNode *node, *mat[cols];
int r, c;
for(r = rows-1; r >= 0; --r){
for(c = cols-1; c >=0; --c){
node = create_node();
if(r < rows -1)
node->next_row = mat[c];
mat[c] = node;
if(c < cols -1)
mat[c]->next_col = mat[c+1];
}
}
*head = mat[0];
}
void创建链接列表矩阵(矩阵节点**头,常量整数行,常量整数列){
矩阵node*node,*mat[cols];
int r,c;
对于(r=rows-1;r>=0;--r){
对于(c=cols-1;c>=0;--c){
node=创建_节点();
如果(r下一行=材料[c];
mat[c]=节点;
if(cnext_col=mat[c+1];
}
}
*头=垫[0];
}
1)MatrixNode*new=头部代码>:类型不同。2) new=create_node();新建=新建->下一行代码>:未形成链接。您也没有创建足够的节点。在示例中,有三个矩阵节点指向下一行,还有三个(不同的)矩阵节点指向下一列。你们应该有九个节点,每个节点都有两个链接,一个用于下一行,一个用于下一列。是的,高尔基是有意义的,我该怎么做呢?我会在我的for循环中添加一个new=new->next\u col和new=new->next\u行吗?如果有人可以帮助我,还需要一些关于显示这一行的帮助。你需要使用不同的变量行和列。prev和tmp应该是相同的吗?那么我该如何显示它呢?我会使用嵌套的for循环还是什么样的循环,但有人能解释一下吗?就像我们使用嵌套for循环一样,idk我在编码方面很差劲,谢谢你的帮助though@user3699735我认为这样做更容易,因为这样可以在垂直和水平方向上放置链接。我认为它是通过改变处理顺序来总结循环的,但很容易理解。
void create_linked_list_matrix(MatrixNode **head, const int rows, const int cols){
MatrixNode *node, *mat[cols];
int r, c;
for(r = rows-1; r >= 0; --r){
for(c = cols-1; c >=0; --c){
node = create_node();
if(r < rows -1)
node->next_row = mat[c];
mat[c] = node;
if(c < cols -1)
mat[c]->next_col = mat[c+1];
}
}
*head = mat[0];
}