【发布时间】:2019-08-14 16:05:28
【问题描述】:
我正在尝试创建一个包含两个参数的节点的单链表。每当我使用尾指针将另一个节点排入队列时,头指针的值与新节点相同。
我确定指针指向相同的内存位置或类似的东西,但我不知道如何解决这个问题。
struct node
{
struct process *p;
struct node *next;
}
struct node* head;
struct node* tail;
void enqueue(struct process* newProcess)
{
struct node *newNode = malloc(sizeof(struct node));
newNode->p = malloc(sizeof(struct process));
newNode->p = newProcess);
if(tail==NULL)
{
head = tail = newNode;
return;
}
tail = tail->next;
tail = newNode;
}
我想使用这个函数来创建一个单链表,头节点指向列表中的第一个元素,尾节点指向列表中的最后一个元素。当前代码生成的两个变量都代表最后添加的元素。
【问题讨论】:
-
`newNode->p = malloc(sizeof(struct process)); newNode->p = newProcess` 泄漏内存。
tail = tail->next你在哪里更新tail->next指针值? -
建议在 stackoverflow.com 中搜索“单链表”
标签: c linked-list singly-linked-list