I am trying to create a doubly linked list, I have successfully done insertion at begining but not at end.
When tried to add node at end , It just prints the first and last node only not the nodes between them.
My code to do so as follows:
insert_at_end(int data, node ** head) { //Where data is the data to be kept it in it's Info part.
node *temp, *temp2 = *head;
if (temp2 == NULL) {
temp2 = (node * ) malloc(sizeof(node));
temp2->freq = data;
temp2->next = NULL;
temp2->prev = *head;
*head = temp2;
} else {
temp = (node * ) malloc(sizeof(node));
temp->freq = data;
temp->next = NULL;
temp->prev = temp2;
temp2->next = temp;
}
}
code for printing is:
main() {
int size, data, i, pos, var, pos2;
node *head = NULL;
printf("enter the size of node\n");
scanf("%d", &size);
printf("start entering the number of elements until your size\n");
for (i = 1; i <= size; i++) {
scanf("%d", & data);
insert_at_end(data, & head);
}
node * temp1;
temp1 = head;
while (temp1 != NULL) {
printf("%d-> ", temp1->freq);
temp1 = temp1 -> next;
}
}
The output is as follows:
enter the size of node
5
start entering the number of elements until your size
1
2
3
4
5
1-> 5-> //It don't print "2->3->4->"
I think there is some logical problem, What that i couldn't find. Could any one correct me please in my logic?