将char*类型转换为C中的struct*



char *constant是指向我的链表头的指针。我应该使用全局字符指针";常数";在我的功能和我正在做的是-

typedef struct Node
{
int attribute;
struct Node next;
} Node;

void init(int data)
{
Node *constant = (Node *)malloc(sizeof(Node));
constant->attribute = data;

Node *next = (Node *)malloc(sizeof(Node));
next->attribute = data*2;
constant->next = next;
next->next = NULL;
printList();
}

void printList()
{
Node *temp = (Node *)constant;
while(temp != NULL)
{
printf("%dn", temp->attribute);
temp = temp->next;
}
}

当我在init函数中调用printList时,它不会打印任何内容,但当我将printList代码复制到init时,它会做它应该做的事情;常数";类似地,在其他函数中,它给了我一个分段错误。我该如何解决这个问题?

如果您的应用程序中已经有一个char*常量,那么您肯定不想声明具有相同名称的Node*。

从init((的第一行中删除Node*,得到constant = malloc(sizeof(Node));,这样您就可以实际使用全局变量,而不是本地变量。你还需要在任何你使用((Node*)constant)->next = next;的地方投射它,例如

init()函数中,您有点困惑如何在列表中添加起始节点。您有全局指针constant *head;添加节点时,声明并分配新的临时节点,并初始化成员设置attribute = data;next = NULL;分配第一个节点后,将节点地址分配给head

不需要init()函数,您真正需要的是add()push()函数,它只是将数据作为参数传递,然后为节点分配,如上所述初始化,然后分配给列表中最后一个节点的next指针。(您也可以执行所谓的前向链接,每次添加一个新的第一个节点,但列表中的节点最终以与输入顺序相反的顺序结束。

您可以使用两个全局指针headtail,其中tail将始终指向列表中的最后一个节点。这样,您就不必迭代插入列表的末尾,只需在tail->next = node;添加新节点,然后更新尾部,使其指向新的末端节点,例如tail = node;

假设您的struct constant类似于:

typedef struct constant {
int attribute;
struct constant *next;
} constant;

您可以编写add()函数来处理将所有节点按顺序添加到您的列表中:

constant *head, *tail;
...
constant *add (int data)
{
constant *node = malloc (sizeof *node);     /* allocate */

if (!node) {                                /* validate EVERY allocation */
perror ("malloc-node");
return NULL;
}
node->attribute = data;                     /* assign data to attribute */
node->next = NULL;                          /* initialize next NULL */

if (!head)                                  /* if 1st node */
head = tail = node;                     /* assign node to both head, tail */
else {  /* otherwise */
tail->next = node;                      /* set tail->next = node */
tail = node;                            /* update tail to point to last node */
}

return node;                /* return pointer to added node to indicate success */
}

print()free_list()函数都可以简单地在每个节点上循环,输出节点属性或释放节点。free_list()中唯一需要注意的是,在删除当前节点之前,必须递增到下一个节点。两种功能如下所示:

总之,你可以做到:

#include <stdio.h>
#include <stdlib.h>
typedef struct constant {
int attribute;
struct constant *next;
} constant;
constant *head, *tail;
constant *add (int data)
{
constant *node = malloc (sizeof *node);     /* allocate */

if (!node) {                                /* validate EVERY allocation */
perror ("malloc-node");
return NULL;
}
node->attribute = data;                     /* assign data to attribute */
node->next = NULL;                          /* initialize next NULL */

if (!head)                                  /* if 1st node */
head = tail = node;                     /* assign node to both head, tail */
else {  /* otherwise */
tail->next = node;                      /* set tail->next = node */
tail = node;                            /* update tail to point to last node */
}

return node;                /* return pointer to added node to indicate success */
}
void prn_list (void)
{
/* loop over each node using iter and output attribute for each node */
for (constant *iter = head; iter; iter = iter->next)
printf (" %d", iter->attribute);

putchar ('n');
}
void free_list (void)
{
/* loop over each node, but increment to next in list before deleting current */
for (constant *iter = head; iter;) {
constant *victim = iter;
iter = iter->next;
free (victim);
}
}
int main (void) {

for (int i = 0; i < 10; i++)
add (i + 1);

prn_list();
free_list();
}

示例使用/输出

$ ./bin/ll_global_head
1 2 3 4 5 6 7 8 9 10

仔细看看,如果你还有问题,请告诉我。

最新更新