首先,变量的单词列表不是一个好的选择。继续。
从我所看到的情况看来,您正在制作一个自引用结构,尽管头元素是一个单独的结构,但与结点具有相同的结构。
像一些评论,我也没有得到任何错误与您发布的代码,所以为了帮助你,这里是一些代码,使用你必须创建一个列表,然后添加值并链接列表和节点在一起。我还使用 - >运算符更改了最后一个节点。
我希望这可以帮助你。
在h文件中。
/*
* 35352860_list.h
*/
#ifndef _35352860_LIST_H__
#define _35352860_LIST_H__
#include <stdio.h>
#include <string.h>
typedef struct node
{
int coef;
int exp;
struct node* next;
} NODE;
typedef struct list
{
NODE head;
} LIST;
void initList(LIST*);
#endif /* _35352860_LIST_H__ */
和c文件。
/*
* 35352860_main.c
*/
#include "35352860_list.h"
void initList(LIST* list)
{
list->head.coef = 0x0000;
list->head.exp = 0x0000;
list->head.next = NULL;
}
int main
(
unsigned int argc,
unsigned char *arg[]
)
{
LIST list;
NODE node_1;
NODE node_2;
printf("There is 1 variable of type LIST and 2 variable of type NODE.\n");
printf("Address of list................................0x%.8X\n", &list);
printf("Value of list.head.coef........................0x%.8X\n", list.head.coef);
printf("Value of list.head.exp.........................0x%.8X\n", list.head.exp);
printf("Value of list.head.next........................0x%.8X\n", list.head.next);
printf("The nodes Node_1 Node_2\n");
printf("Address of nodes.........................................0x%.8X 0x%.8X\n", &node_1, &node_2);
printf("Value of nodes.coef......................................0x%.8X 0x%.8X\n", node_1.coef, node_2.coef);
printf("Value of nodes.exp.......................................0x%.8X 0x%.8X\n", node_1.exp, node_2.exp);
printf("Value of nodes.next......................................0x%.8X 0x%.8X\n", node_1.next, node_2.next);
printf("\n\nCall initList.\n");
initList(&list);
printf("Address of list.head...........................0x%.8X\n", &list);
printf("Value of list.head.coef........................0x%.8X\n", list.head.coef);
printf("Value of list.head.exp.........................0x%.8X\n", list.head.exp);
printf("Value of list.head.next........................0x%.8X\n", list.head.next);
printf("The nodes Node_1 Node_2\n");
printf("Address of nodes.........................................0x%.8X 0x%.8X\n", &node_1, &node_2);
printf("Value of nodes.coef......................................0x%.8X 0x%.8X\n", node_1.coef, node_2.coef);
printf("Value of nodes.exp.......................................0x%.8X 0x%.8X\n", node_1.exp, node_2.exp);
printf("Value of nodes.next......................................0x%.8X 0x%.8X\n", node_1.next, node_2.next);
printf("\n\nLinking node_1 to list, and add some values to node_1.\n");
list.head.next = &node_1;
node_1.coef = 0x1111;
node_1.exp = 0x1111;
node_1.next = NULL;
printf("Address of list.head...........................0x%.8X\n", &list);
printf("Value of list.head.coef........................0x%.8X\n", list.head.coef);
printf("Value of list.head.exp.........................0x%.8X\n", list.head.exp);
printf("Value of list.head.next........................0x%.8X\n", list.head.next);
printf("The nodes Node_1 Node_2\n");
printf("Address of nodes.........................................0x%.8X 0x%.8X\n", &node_1, &node_2);
printf("Value of nodes.coef......................................0x%.8X 0x%.8X\n", node_1.coef, node_2.coef);
printf("Value of nodes.exp.......................................0x%.8X 0x%.8X\n", node_1.exp, node_2.exp);
printf("Value of nodes.next......................................0x%.8X 0x%.8X\n", node_1.next, node_2.next);
printf("\n\nLinking node_2 to node_1, and add some values to node_2.\n");
node_1.next = &node_2;
node_2.coef = 0x2222;
node_2.exp = 0x2222;
node_2.next = NULL;
printf("Address of list.head...........................0x%.8X\n", &list);
printf("Value of list.head.coef........................0x%.8X\n", list.head.coef);
printf("Value of list.head.exp.........................0x%.8X\n", list.head.exp);
printf("Value of list.head.next........................0x%.8X\n", list.head.next);
printf("The nodes Node_1 Node_2\n");
printf("Address of nodes.........................................0x%.8X 0x%.8X\n", &node_1, &node_2);
printf("Value of nodes.coef......................................0x%.8X 0x%.8X\n", node_1.coef, node_2.coef);
printf("Value of nodes.exp.......................................0x%.8X 0x%.8X\n", node_1.exp, node_2.exp);
printf("Value of nodes.next......................................0x%.8X 0x%.8X\n", node_1.next, node_2.next);
printf("\n\nChange the value node_2.coef using the -> operator.\n");
list.head.next -> next -> coef = 0x3333;
printf("Address of list.head...........................0x%.8X\n", &list);
printf("Value of list.head.coef........................0x%.8X\n", list.head.coef);
printf("Value of list.head.exp.........................0x%.8X\n", list.head.exp);
printf("Value of list.head.next........................0x%.8X\n", list.head.next);
printf("The nodes Node_1 Node_2\n");
printf("Address of nodes.........................................0x%.8X 0x%.8X\n", &node_1, &node_2);
printf("Value of nodes.coef......................................0x%.8X 0x%.8X\n", node_1.coef, node_2.coef);
printf("Value of nodes.exp.......................................0x%.8X 0x%.8X\n", node_1.exp, node_2.exp);
printf("Value of nodes.next......................................0x%.8X 0x%.8X\n", node_1.next, node_2.next);
return(0x0000);
}
您是使用C编译器编译还是使用C++编译器? – paddy
我在Windows上使用MinGW的gcc。 – Ryan
如果将参数名称(列表)添加到声明中,会发生什么情况? –