Hello. I am having a tough time understanding the concept of double linked list. For now, i understand double linked list has 2 links, one forward and another one previous. data is in the middle. when inserting must check where to insert. if previous is equal to null, then can insert it to previous. Set the previous to be equal to the newly allocated node. I am looking to do a bubble sort using double linked list. I want to understand the concept first then I can do it. Can someone give some usefule links or explain me the concept? Thank you.

Recommended Answers

All 4 Replies

firsly read Click Here. after reading this, Click Here, then if there is any doubt left, feel free to ask here only. thanks. ;)

Thank you for the links....if a newnode is added using a malloc() and the insertion is done in the beginning, so the previous should be null and head->next would be null, right?...How about the next insertion? I did a bit but not working....not sure if is correct.

typedef struct nodes
{
    int value;
    struct nodes *prev;
    struct nodes *next;
}node;

node *newnode, *current=head,*previous;
    printf("\nHow many integers to insert? ");
    scanf ("%d", &num);
    {
        for (i=0;i<num;i++)
        {
            printf("\nEnter integer %d: ",i+1);
            scanf("%d", &number);
            newnode=malloc(sizeof(node));
            if (newnode==NULL)
            {
                printf("\n\nUnable to allocate memory");
                exit(EXIT_FAILURE);
            }
            newnode->value=number;
            newnode->next=current;
            if (head==NULL)
            {
                head=newnode;
                head->prev=NULL;
                previous->next=newnode;
            }
            else
            {
                newnode->next=head;
                head->prev=newnode;
            }
            newnode=newnode->next;
        }

see, I want you to do the work. here goes the hint! Take the head insertion as the special case and do it the main() only. okay ? now, then make a function and pass the info part which you want to store in the node. okay ? now, then with each node update the pointer where you need to insert the node, like after head, update a temp pointer with temp==temp->next where temp is equal to the head initially. okay ? So do it n number of times. :-) got it ?

P.S this is one way linked list explaination. for double linked list, nothing difficiult! just have one more pointer which will point to the previous nothing else. but i wana advice you that first try one way linked list, then update it to double linked list. it will be better for you. thanks

I have updated my code as the one I post was wrong. Its working. All I had to do is to complete the insertion into the tail and checking the value of the first node with the second while i iterate the list until it is lesser than the list size.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.