zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    发送邮件程序
    T-SQL存储过程、游标
    GPS经纬度换算成XY坐标
    开博了
    你应该知道的 50 个 Python 单行代码
    想提升java知识的同学请进
    adb工具包使用方法
    红米note3刷安卓原生
    hadoop 使用和javaAPI
    django学习——url的name
  • 原文地址:https://www.cnblogs.com/wangha/p/11098869.html
Copyright © 2011-2022 走看看