C(链表):无法执行节点>下一个>下一个(错误:取消引用指向不完整类型"结构节点"的指针)并且仅限于节点>下一个



我正在学习如何创建我的第一个链表,并尝试从列表中弹出/添加第一个/最后一个元素的不同功能。

当我尝试从列表中弹出最后一个项目时,我无法转到列表的倒数第二个地址,也无法弹出删除最后一个元素的列表倒数第二地址的下一个地址。

节点结构:

typedef struct Node
{
int data;
struct node* addressOfNextNode;
}node;

这是我的链接列表创建者:

node * createLinkedList(int n)
{
int i = 0;
node * headNode = NULL;
node * address_node_to_be_inserted = NULL;
node * currentNode = NULL;
for(i=0;i<n;i++)
{
//create individual isolated node
address_node_to_be_inserted = (node*)malloc(sizeof(node));
printf("n Enter the data for node number:");
scanf("%d", &(address_node_to_be_inserted->data));
address_node_to_be_inserted->addressOfNextNode = NULL;
if(headNode == NULL) //if list is currently empty, then make address_node_to_be_inserted as first node
{
headNode = address_node_to_be_inserted;
printf("current node's data is %d and address is %pn", headNode->data, headNode);
}
else
{
currentNode = headNode;
while(currentNode->addressOfNextNode != NULL)
{
currentNode = currentNode->addressOfNextNode;
}
currentNode->addressOfNextNode = address_node_to_be_inserted;
printf("current node's data is %d and address is %pn", address_node_to_be_inserted->data, currentNode);
}
}
return headNode;
}

我会输入数据为1、2、3、4、5的5个节点,并将它们分配给后底部的主功能中的HEAD

这是尝试删除最后一个元素的功能:

void remove_last(node * head)
{
//if there is only one item in the list, remove it
if (head->addressOfNextNode == NULL)
{
free(head);
}
node * currentNode = NULL;
currentNode = head;
while(currentNode->addressOfNextNode->addressOfNextNode != NULL)
{
currentNode = currentNode->addressOfNextNode;
}
currentNode->addressOfNextNode = NULL;
}

当我做currentNode->下一个节点的地址->addressOfNextNode,我得到一个错误(错误:取消引用指向不完整类型"struct-node"的指针)

为什么?

因为我不能用那种格式做,所以我改为:

void remove_last(node * head)
{
//if there is only one item in the list, remove it
if (head->addressOfNextNode == NULL)
{
free(head);
}
node * currentNode = NULL;
node * currentNode2 = NULL;
currentNode = head;
currentNode2 = head;
while(currentNode->addressOfNextNode != NULL) //reaches the last node
{
currentNode = currentNode->addressOfNextNode;
}
while(currentNode2->addressOfNextNode != currentNode) //reaches the 2nd last node by comparing it to != last node
{
currentNode2 = currentNode2->addressOfNextNode;
}
currentNode2->addressOfNextNode = NULL;
}

因此,这只是将列表迭代到NULL之前的最后一个节点,并获得另一个迭代以与NULL之前的上一个节点进行比较,从而获得倒数第二个节点。

我的主要功能:

int main()
{
int n = 0;
node *HEAD = NULL;
printf("n How many nodes?: "); //Asks user how many nodes to be created
scanf("%d", &n);
HEAD = createLinkedList(n); //input number of nodes in to Linked List creator
remove_last(HEAD);
return 0;
}

编辑:

编译器给出一个错误,因为在定义struct node之前存在对它的引用。

typedef struct Node
{
int data;
struct node* addressOfNextNode;  // <-- this is the error
} node;

在本语句中,您尚未完成对struct node的定义。编译器只知道一个struct Node(注意大写N)。如果您可以通过用Node:替换该行来纠正它

struct Node* addressOfNextNode;  // <-- renamed to Node

或者通过将整个结构重命名为node,如下所示:

typedef struct node // <-- renamed to node
{
int data;
struct node* addressOfNextNode;
} node;

如果您感兴趣,这个StackOverflow问题可能是一个有用的链接。

最新更新