双链表的增删改查(C实现)

#include 
#include 

typedef int data_t;

typedef struct dnode_t{
    data_t data;
    struct dnode_t *prev, *next;
}dlinknode_t, *dlinklist_t;

dlinklist_t CreateEmptyDLinklist()
{
    dlinklist_t list;
    list = (dlinklist_t)malloc(sizeof(dlinknode_t));
    if(list != NULL)
    {
        list->prev = NULL;
        list->next = NULL;
    }
    return list;
}

int InsertDLinklist(dlinklist_t list, int at , data_t x)
{
    dlinklist_t node_prev, node_at, node_new;
    int post_at=0, found;

    if(NULL == list) return -1;
    if(at < 0) return -1;

    node_new = (dlinklist_t)malloc(sizeof(dlinknode_t));
    if(node_new == NULL) return -1;

    node_new->data = x;
    node_new->prev = NULL;
    node_new->next = NULL;

    node_prev = list;
    node_at = list->next;

    while(node_at != NULL)
    {
        if(post_at == at)
        {
            found = 1;
            break;
        }
        node_prev = node_at;
        node_at = node_at->next;
        post_at++;
    }

    if(found == 1)
    {
        node_new->prev = node_prev;
        node_new->next = node_prev->next;

        node_prev->next->prev = node_new;
        node_prev->next = node_new;

    }else
    {
        node_prev->next = node_new;
        node_new->prev = node_prev;
    }

    return 0;

}

int DeleteDLinklist(dlinklist_t list, int at)
{
    int post_at = 0;
    int found;
    if(NULL == list) return -1;
    if(at < 0) return -1;

    dlinklist_t node;
    node=list->next;

    while(node!=NULL)
    {
        if(post_at == at)
        {
            found = 1;
            break;
        }
        node = node->next;
        post_at++;
    }

    if(found)
    {
        node->prev->next = node->next;
        node->next->prev = node->prev;
        free(node);
        return 0;
    }else
    {
        printf("delete no found\n");
        return -1;
    }
}


int SetDLinklist(dlinklist_t list, int at, data_t x)
{
    int post_at = 0; 
    int found = 0;
    if(NULL == list) return -1;
    if(at < 0) return -1;

    dlinklist_t node;
    node=list->next;

    while(node != NULL)
    {
        if(post_at == at)
        {
            found = 1;
            node->data = x;
            break;
        }

        node=node->next;
        post_at++;
    }
    if(found)
    {
        return 0;
    }else{
        return -1;
    }
}


int GetDLinklist(dlinklist_t list, int at, data_t *x)
{
    if(NULL == list) return -1;
    if(at < 0) return -1;

    dlinklist_t node;
    node = list->next;
    int post_at=0 , found;
    while(node != NULL)
    {
        if(post_at == at)
        {
            if(x!=NULL)
                {
                    *x=node->data;
                }
            return 0;
        }

        node = node->next;
        post_at++;
    }
    printf("no found\n");
    return -1;

}



int main(int argc, char *argv[])
{
    int a[5] = {1, 3, 5, 7, 9};
    int i=0;
    dlinklist_t list;
    int x;
    list = CreateEmptyDLinklist();
    for(i=0; i<5; i++)
    {
        InsertDLinklist(list,i,a[i]);
    }

    GetDLinklist(list,2,&x);
    printf("x=%d\n", x);

    DeleteDLinklist(list,3);

    SetDLinklist(list,0,9);

    GetDLinklist(list,0,&x);
    printf("x=%d\n", x);
    return 0;
}

你可能感兴趣的:(数据结构与算法)