简体   繁体   中英

Double linked list implementation in c

I am trying to improve my c programming skills and so started with trying to program a double linked list.

Here is what i have come up with so far.

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
//forward definition

typedef struct Node node_t;


//Define the structures needed for double linked list

//Each node
typedef struct Node 
{
        int data;
        node_t *next;
        node_t *prev;
}node_t;




void initList(node_t** first , node_t** last)
{
    //Allocate memory for the first and the last node

    *first = (node_t*) malloc(sizeof(node_t));
    *last =  (node_t*) malloc(sizeof(node_t));
    (*first)->data = 1;
    (*last)->data = 2;

    (*first)->prev = NULL;
    (*last)->next = NULL;

    (*first)->next = (*last)->prev;

    return;

}

void destroyList(node_t** first)
{
    node_t* temp;

    temp = *first;

    free((*first)->next);
    free((*first));
    temp = NULL;



    return;
}



int main()
{

    node_t *first =NULL, *last = NULL;

    printf("Initalizing the List\n");
    initList(&first,&last);

    printf(" 1st element is %d\n",first->data);
    printf(" 2nd element is %d\n",last->data);

    printf("Destroying the List\n");




    destroyList(&first) ;


    return 0;
}

I actually looked up for some code online and i see that most implementations have

1) 1 structure for Node and 1 structure for List itself (with head and tail). My question is, is this mandatory? Can i not implement it with just 1 structure?

2) My idea is to make this file as a library and call it from an application. Like
InitList(), DestroyList(), AddNode, DeleteNode etc.

And that is why i am using double pointers for the INit and destroy. I am having some trouble destroying the list. I know i am doing it wrong, i will continue to correct it.

3) I found that node pointer

 temp = first

was pointing to some address. If i do temp++. Why doesn't it point to next node?

4)We can either pass the first or the last node pointer to delete the entire list correct?. ( ie traverse and dleete sequentialluy?)

Thanks!

1) It's not necessary to have a list structure but it makes certain operations quicker to perform if the structure kept track of certain meta-data (length of list for example, otherwise to get length you'd have to iterate through your list every time which can be expensive for large lists).

3) I'm assuming you mean

temp = *first

and temp++ doesn't point to the next node because it's not guaranteed that your nodes are in contiguous memory addresses (arrays on the other hand do ).

4) You can use first or last to delete the list but you have to ensure a certain property that previous of head also points to NULL otherwise you can get stuck in infinite loop

1) 1 structure for Node and 1 structure for List itself is certainly not mandatory. It is often done with 1 structure.

2) Nice ideas InitList(), DestroyList(), AddNode, DeleteNode etc.

Your init may need

(*first)->next = *last;
(*last)->prev = *first;
//  rather than
(*first)->next = (*last)->prev;

3) As @Jamil Seaidoun, do not do temp++ , rather temp = temp->next .

4) You can pass either end. The classic problem is not getting the next pointer before the free()

// bad code
free(temp);
temp = temp->next;

// good code
next = temp->next;
free(temp);
temp = next;

Ramblings

Paradigm shift. Consider a double-link-list with no NULL pointers. Rather make the full circle. Last->next = First . First->prev = Last . Then instead of of while loop that goes until p->next == NULL , loop until p->next == first . The list simply points to the first node (or NULL if empty). I find this style more flexible and less change of *NULL.

2nd Paradigm shift. Sometimes the only reason for a double-link-list is to allow adding nodes to the beginning or the end. This can be accomplished with a single next field that goes around in a circle. The list pointer in this case does not point to the first node, but to the last. (note: first is last->next) Inserting at the beginning or end is the same, add a node after last and before first. The difference is do we leave the list pointer as is, or advance it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM