是否可以将结构存储到链表中?
Is it possible to store a struct into a linked list?
我正在编写一个使用 DFS 算法和堆栈解决迷宫问题的程序。我正在考虑将用于到达终点的路径的坐标存储到包含坐标的整数 x
、y
的结构中,然后将该结构推入堆栈以执行其他指令(打印,流行音乐等)。
我找遍了,但还没有找到任何有用的东西。所以我继续进行设置,但是我收到了一个关于类型兼容性的错误,因为我的节点数据是一个 int,但我试图放入一个结构。作为链表的新手,我只将数据视为 int 或 char。最后,我什至可以做我想做的事吗?如果不能,您能否建议一种将 x
、y
坐标传递到堆栈的方法?先感谢您。
这是我的代码示例,保存位置 space a1
是 COORD
的一个实例,并且初始化列表以及迷宫等。
typedef struct node {
int data; /* Value or data stored in node*/
struct node *pNext; /* Reference to the next node address */
} NODE;
/*Structure declares pointers for front and back of the list*/
typedef struct LIST {
NODE *front;
NODE *back;
} LIST;
/* Structure to pass multiple values onto stack */
typedef struct COORD{
int x;
int y;
}COORD;
/*Example of one of the functions */
void lst_push_front(LIST *l, COORD *a1) {
NODE *p = malloc(sizeof(NODE));
p->data = a1;
p->pNext = l->front;
l->front = p;
if(l->back == NULL) // was empty, now one elem
l->back = p;
}
正如@barak manos 提到的,您应该将 COORD
结构放在 NODE
之前并将 int data
更改为 COORD data
并使用 p->data = *a1
检查下面的代码。
由于 COORD 是一个结构,您可以将其包含在另一个结构中,如下面的代码所示。
还要确保结构的顺序正确。
p->data.x
是访问结构成员的正确方法 COORD
#include <stdio.h>
/* Structure to pass multiple values onto stack */
typedef struct COORD{
int x;
int y;
}COORD;
typedef struct node {
COORD data; /* --> Changes done here */
struct node *pNext; /* Reference to the next node address */
} NODE;
/*Structure declares pointers for front and back of the list*/
typedef struct LIST {
NODE *front;
NODE *back;
} LIST;
void func(COORD *q)
{
NODE *p = malloc(sizeof(NODE));
p->data.x = q->x;
p->data.y = q->y;
printf("%d %d",p->data.x,p->data.y);
free(p);
}
int main(void) {
COORD *q = malloc(sizeof(COORD));
q->x = 20;
q->y = 30;
func(q);
free(q);
return 0;
}
我正在编写一个使用 DFS 算法和堆栈解决迷宫问题的程序。我正在考虑将用于到达终点的路径的坐标存储到包含坐标的整数 x
、y
的结构中,然后将该结构推入堆栈以执行其他指令(打印,流行音乐等)。
我找遍了,但还没有找到任何有用的东西。所以我继续进行设置,但是我收到了一个关于类型兼容性的错误,因为我的节点数据是一个 int,但我试图放入一个结构。作为链表的新手,我只将数据视为 int 或 char。最后,我什至可以做我想做的事吗?如果不能,您能否建议一种将 x
、y
坐标传递到堆栈的方法?先感谢您。
这是我的代码示例,保存位置 space a1
是 COORD
的一个实例,并且初始化列表以及迷宫等。
typedef struct node {
int data; /* Value or data stored in node*/
struct node *pNext; /* Reference to the next node address */
} NODE;
/*Structure declares pointers for front and back of the list*/
typedef struct LIST {
NODE *front;
NODE *back;
} LIST;
/* Structure to pass multiple values onto stack */
typedef struct COORD{
int x;
int y;
}COORD;
/*Example of one of the functions */
void lst_push_front(LIST *l, COORD *a1) {
NODE *p = malloc(sizeof(NODE));
p->data = a1;
p->pNext = l->front;
l->front = p;
if(l->back == NULL) // was empty, now one elem
l->back = p;
}
正如@barak manos 提到的,您应该将 COORD
结构放在 NODE
之前并将 int data
更改为 COORD data
并使用 p->data = *a1
检查下面的代码。
由于 COORD 是一个结构,您可以将其包含在另一个结构中,如下面的代码所示。
还要确保结构的顺序正确。
p->data.x
是访问结构成员的正确方法 COORD
#include <stdio.h>
/* Structure to pass multiple values onto stack */
typedef struct COORD{
int x;
int y;
}COORD;
typedef struct node {
COORD data; /* --> Changes done here */
struct node *pNext; /* Reference to the next node address */
} NODE;
/*Structure declares pointers for front and back of the list*/
typedef struct LIST {
NODE *front;
NODE *back;
} LIST;
void func(COORD *q)
{
NODE *p = malloc(sizeof(NODE));
p->data.x = q->x;
p->data.y = q->y;
printf("%d %d",p->data.x,p->data.y);
free(p);
}
int main(void) {
COORD *q = malloc(sizeof(COORD));
q->x = 20;
q->y = 30;
func(q);
free(q);
return 0;
}