We are trying to make the linklist work with a tail and a head pointer. I think there is an error @ line 26.
" tail->next = new;"
We are trying to change whatever the tail pointer is pointing next to the new node. It's probably a syntax error.
Thanks for your help.
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node {
int id;
struct node* next;
};
/* if it's the first item, pass in HEAD
* else pass in the TAIL
*/
void addNode(struct node** head, struct node** tail, int id)
{
struct node* new = malloc(sizeof(struct node* ));
if(head == NULL)
{
new->id = id;
new->next = NULL;
*head = new;
*tail = new;
}
else
{
new->id = id;
new->next = NULL;
tail->next = new;
*tail = new;
}
}
void printList(struct node* head)
{
while(head != NULL)
{
printf("I M INHERE!!! \n");
printf("ID %d", head->id);
head = head->next;
}
}
int main()
{
struct node* head;
struct node* tail;
head = malloc(sizeof(struct node*));
tail = malloc(sizeof(struct node*));
head = NULL;
tail = head;
int i;
for(i = 0; i < 5; i++)
{
addNode(&head,&tail, i);
}
printList(head);
return 0;
}