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!\n");
        }
    }
}

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\n",tmp->pos,tmp->data);
        tmp = tmp->next;
    }
    printf("pos:%d,data:%d\n",tmp->pos,tmp->data);
}

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

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\n",tmp->pos,tmp->data);
        tmp = tmp->pn->next;
    }
    printf("pos:%d,data:%d\n",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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM