[英]In C, is node.next->prev the same as node->next->prev?
[英]C(Linked List): Unable to do node->next->next(error: dereferencing pointer to incomplete type 'struct node') and being limited to only node->next
我正在学习如何创建我的第一个链表并尝试通过弹出/添加列表中的第一个/最后一个元素来使用不同的功能。
当我尝试从列表中弹出最后一项时,我无法转到列表的倒数第二个地址并弹出删除最后一个元素的列表倒数第二个地址的下一个地址。
节点结构:
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 %p\n", 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 %p\n", 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->addressOfNextNode 时,出现错误(错误:取消引用指向不完整类型“结构节点”的指针)
这是为什么?
由于我无法以这种格式执行此操作,因此我改为执行此操作:
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 问题可能是一个有用的链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.