linked list - Inserting node at the end of LinkList in C -
i don't have problems insert nodes
in middle or @ beginning of linkedlist
. however, @ end different. when try print values, gives me address:
now, here how build linkedlist
:
employeedata ed[4]; node *head = null, *list = null; (int = 0; < 4; i++) fscanf(file, "%d %s %d %d %lf", &ed[i].emp_id, ed[i].name, &ed[i].dept, &ed[i].rank, &ed[i].salary); head = (node *)malloc(sizeof(node)); head->employee = ed[0]; head->next = null; list = head; (int = 1; < 5; i++){ node *ptr = (node *)malloc(sizeof(node)); ptr->employee = ed[i]; ptr->next = null; head->next = ptr; head = head->next; }
here how add nodes it:
node *newnode = (node *)malloc(sizeof(node)), *temptr = list, *endnode = list; newnode->employee.emp_id = id; strcpy(newnode->employee.name, name); newnode->employee.dept = dept; newnode->employee.rank = rank; newnode->employee.salary = salary; newnode->next = null; while (endnode->next != null) endnode = endnode->next; if (id < temptr->employee.emp_id){ newnode->next = list; list = newnode; } else if (id > endnode->employee.emp_id){ endnode->next = newnode; } else{ while ((temptr->next != null) && (temptr->next->employee.emp_id < newnode->employee.emp_id)) temptr = temptr->next; newnode->next = temptr->next; temptr->next = newnode; }
when try remove value, remove it. however, not display value.
what doing wrong?
thanks
as bluepixy stated, problem during building.
the index needed in 4 for (int = 1; < 4; i++)
and also, changed how printed list.
from
while (temp->next != null){ printf(""); temp = temp->next; }
to
while (temp != null){ printf(""); temp = temp->next; }
Comments
Post a Comment