c的链表实现

c的链表实现

复习了 单向链表双向链表 ,代码中注释不多,但基本从函数名字就可以知道函数的作用。

双向链表中的前后节点中的思路是按照linux内核中思路写的。

环境

GCC 7.4.0

单向链表

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
    int pos;
    int data;
    struct node *next;
}*lnode,*llist,node;

void insert(llist list, int data, int pos){
    if(!list){
        list->pos = pos;
        list->data = data;
        list->next = NULL;
    }else{
        lnode tmp = (lnode)malloc(sizeof(node));
        tmp->pos = pos;
        tmp->data = data;
        tmp->next = NULL;

        lnode tmps = list;
        while(tmps->next != NULL){
            tmps = tmps->next;
        }
        tmps->next = tmp;
    }
}

void delete(llist list, int pos){
    if(list->pos == pos){
        llist tmp = list;
        list = list->next;
        free(tmp);
    }else{
        lnode tmp = list;
        lnode tmps = tmp->next;
        while(tmp->next != NULL){
            if(tmps->pos == pos){
                tmp->next = tmps->next;
                free(tmps);
                break;
            }else{
                tmp = tmps;
                tmps = tmps->next;
            }
        }
    }
}

void changedata(llist list, int data, int pos){
    if(list->pos == pos){
        list->data = data;
    }else{
        lnode tmp = list->next;
        while(tmp->pos != pos && tmp->next != NULL){
            tmp = tmp->next;
        }
        if(tmp->next != NULL){
            tmp->data = data;
        }else if(tmp->pos == pos){
            tmp->data = data;
        }else{
            printf("Can't find the element!
");
        }
    }
}

void deletelist(llist list){
    lnode p = list;
    while(list->next){
        p = list;
        list = p->next;
        free(p);
    }
    free(list);
}

void show(llist list){
    lnode tmp = list;
    while(tmp->next != NULL){
        printf("pos:%d,data:%d
",tmp->pos,tmp->data);
        tmp = tmp->next;
    }
    printf("pos:%d,data:%d
",tmp->pos,tmp->data);
}

int main(){
    llist list = (llist)malloc(sizeof(node));
    list->pos = 0;
    list->data = 7;
    list->next = NULL;
    printf("Origin List!
");
    show(list);
    insert(list,6,1);
    printf("After insert
");
    show(list);
    changedata(list,4,1);
    printf("After change value
");
    show(list);
    insert(list,5,2);
    printf("After insert
");
    show(list);
    delete(list,1);
    printf("After delete
");
    show(list);
    deletelist(list);
    return 0;
}

双向链表

#include <stdio.h>
#include <stdlib.h>

typedef struct pn{
    struct node *prev;
    struct node *next;
}pn;

typedef struct node{
    int pos;
    int data;
    struct pn *pn;
}*lnode,*llist,node;

void insert(llist list, int data, int pos){
    if(!list){
        list->pos = pos;
        list->data = data;
        list->pn = NULL;
    }else{
        lnode tmp = (lnode)malloc(sizeof(node));
        pn *pntmp = (pn*)malloc(sizeof(pn));
        
        lnode tmps = list;
        while(tmps->pn->next != NULL){
            tmps = tmps->pn->next;
        }

        pntmp->prev = tmps;
        pntmp->next = NULL;
        tmps->pn->next = tmp;

        tmp->pos = pos;
        tmp->data = data;
        tmp->pn = pntmp;
    }
}

void delete(llist list, int pos){
    if(list->pos == pos){
        llist tmp = list;
        list = list->pn->next;
        list->pn->prev = NULL;
        free(tmp);
    }else{
        lnode tmp = list;
        lnode tmps = tmp->pn->next;
        while(tmp->pn->next != NULL){
            if(tmps->pos == pos){
                tmp->pn->next = tmps->pn->next;
                tmps->pn->next->pn->prev = tmp;
                free(tmps);
                break;
            }else{
                tmp = tmps;
                tmps = tmps->pn->next;
            }
        }
    }
}

void changedata(llist list, int data, int pos){
    if(list->pos == pos){
        list->data = data;
    }else{
        lnode tmp = list->pn->next;
        while(tmp->pos != pos && tmp->pn->next != NULL){
            tmp = tmp->pn->next;
        }
        if(tmp->pn->next != NULL){
            tmp->data = data;
        }else if(tmp->pos == pos){
            tmp->data = data;
        }else{
            printf("Can't find the element!
");
        }
    }
}

void deletelist(llist list){
    lnode p = list;
    while(list->pn->next){
        p = list;
        list = p->pn->next;
        free(p);
    }
    free(list);
}

void show(llist list){
    lnode tmp = list;
    while(tmp->pn->next){
        printf("pos:%d,data:%d
",tmp->pos,tmp->data);
        tmp = tmp->pn->next;
    }
    printf("pos:%d,data:%d
",tmp->pos,tmp->data);
}

int main(){
    llist list = (llist)malloc(sizeof(node));

    pn *pnx = (pn*)malloc(sizeof(pn));
    pnx->prev = NULL;
    pnx->next = NULL;

    list->pos = 0;
    list->data = 7;
    list->pn = pnx;

    insert(list,6,1);
    show(list);

    insert(list,6,2);
    changedata(list,5,2);
    show(list);

    delete(list,1);
    show(list);

    deletelist(list);
    return 0;
}
原文地址:https://www.cnblogs.com/wangha/p/11098869.html