1

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?

Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
Sss
  • 1,519
  • 8
  • 37
  • 67

3 Answers3

4

You are not adding new node at the end.
To add at the end you should first reach to the last node by iterating over all other nodes in between.

Or,

you can keep track of the pointer to last node and then add new node after that.

Also, specify the return type of your insert function:
"void" insert_at_end(int data, node ** head)

C treats unspecified return types as int and hence your compiler might complain: "Control reached at the end of a non-void function"

0xF1
  • 6,046
  • 2
  • 27
  • 50
1

there is mistake in you insert at end function.
you should travese a non-empty list first to reach the end of the list and then insert the new node there. then the node will be added at the last position. now what you wrote makes the new node to be added to the 2nd position. and its next pointer is NULL so the resulting list will always contain only 2 nodes.
1st_node followed by the node containing last node(node with last entered value)

LearningC
  • 3,182
  • 1
  • 12
  • 19
0
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; 
        while((temp2)->next!=NULL) //for moving  to the end point
              temp2=(temp2)->next; 
        temp -> prev = temp2;

        temp2->next=temp;
    }
}
vinod
  • 132
  • 5
  • 1
    You shouldn't solve the homework for the user. At least use a spoiler markup: http://meta.stackexchange.com/questions/72877/whats-the-exact-syntax-for-spoiler-markup – hivert Mar 05 '14 at 10:38
  • 2
    Always add explanation with the code. Posting just the code is like doing someone's homework. – 0xF1 Mar 05 '14 at 10:38
  • @vinod thanks for the answer, but why you have used asterisk "*" in while loop for "temp2" also for temp1 ? I mean here (while((*temp2)->next!=NULL) //for moving the to the end point *temp=(*temp)->next; ) – Sss Mar 05 '14 at 10:41
  • 1
    You can add the explanation now itself in this answer, don't have to wait for the next time. – 0xF1 Mar 05 '14 at 10:44
  • 1
    -1 for no explanation and plain code answering a homework question. – hivert Mar 05 '14 at 11:12