I'm trying to create a singly linked list with nodes containing two parameters. Whenever I enqueue another node using the tail pointer, the head pointer takes the same value as the new node.
I'm sure the pointers are pointing to the same memory location or something similar, but I'm not sure how to fix this.
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;
}
I'd like to use this function to be able to create a singly linked list with the head node pointing to the first element in the list and the tail node pointing to the last element in the list. The current code results in both variables representing the last element added.
tail = tail->nextand where do you update thetail->nextpointer value? - KamilCuk