• 链表(list)--c实现


        做c的开发有1年多了,期间写过c++,感觉基础不够好,补上去,不丢人。o(^▽^)o

        to better myself.

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node
    {
        int data;
        struct node* next;
    }Node;
    
    typedef Node* List;
    
    void pInitList(List*);
    List initList();
    void deleteList(List);
    int isNull(List);
    void insertNode(Node*, int);
    void deleteNode(List, Node*);
    Node* findLast(List);
    Node* findValue(List, int);
    void printList(List);
    
    void printList(List list)
    {
        if(list == NULL)
        {
            printf("empty list. printf nothing.
    ");
            return;
        }
        Node* curNode = list->next;
    
        while(curNode != NULL )
        {
            if(curNode->next != NULL)
            {
                printf("%d ->", curNode->data);
            }else{
                printf("%d 
    ", curNode->data);
    
            }
            curNode = curNode->next;
        }
    }
    
    Node* findValue(List list, int tarVal)
    {
        if(list == NULL)
        {
            printf("empty list.
    ");
            return NULL;
        }
        Node* curNode = list->next;
        while((curNode->data != tarVal )
              &&(curNode != NULL))
        {
            curNode = curNode->next;
        }
        if(curNode == NULL) {
            printf("not find the value %d
    ", tarVal);
            free(curNode);
            return NULL;
        }
        printf(" find the value %d
    ", tarVal);
        return curNode;
    }
    Node* findLast(List list)
    {
        if(list == NULL)
        {
            printf("empty list.
    ");
            return NULL;
        }
        Node* curNode = list->next;
        while(curNode->next != NULL)
        {
            curNode = curNode->next;
        }
        printf("find the last node value is %d.
    ", curNode->data);
        return curNode;
    
    }
    void deleteNode(List list, Node* delNode)
    {
        Node* curNode = (Node*)malloc(sizeof(Node));
        Node* freeNode = NULL;
    
        if(curNode == NULL)
        {
            printf("malloc error at line %d.", __LINE__);
            return;
        }
        curNode = list->next;
        if(curNode->data == delNode->data)
        {
            list->next = NULL;
            free(curNode);
            return ;
        }
        while((curNode != NULL)
              && (curNode->next->data != delNode->data)
              && (curNode->next != NULL))
        {
            curNode=curNode->next;
        }
    
        if(curNode->next == NULL || (curNode ==NULL))
        {
            printf("can not find the given node.
    ");
            return ;
        }else{
            freeNode = curNode->next;
            curNode->next = freeNode->next;
            free(freeNode);
        }
    
    
    }
    
    
    void insertNode(Node* curNode, int newValue)
    {
        Node *newNode = (Node*) malloc(sizeof(Node));
        if(newNode == NULL)
        {
            printf("malloc error at line %d.", __LINE__);
            return;
        }
        newNode->data = newValue;
        newNode->next = curNode->next;
        curNode->next = newNode;
    
    }
    
    /*return 0 means is not NULL*/
    int isNull(List list )
    {
        //printf("next value %d
    ", list->next->data);
        return (list->next == NULL);
    }
    #if 1
    void pInitList(List* list)
    {
        //*list = (Node*) malloc(sizeof(Node));
        (*list)->next = NULL;
        return;
    }
    #endif
    List initList()
    {
        List list;
        list = (Node*) malloc(sizeof(Node));
        list->next = NULL;
        return list;
    }
    
    void deleteList(List list)
    {
        Node* freeNode = NULL;
        if(list == NULL) return;
        while(list->next != NULL)
        {
            //get the free node
            freeNode = list->next;
            list->next = freeNode->next;
            free(freeNode);
        }
        return ;
    
    }
    
    int main()
    {
        printf("hello.
    ");
    #if 0
        List newList = NULL;
        newList = initList();
    #else
        List *list = NULL;
        //这里我第一次就没分配1级指针的内存,导致出错,感谢帮助我指出的人。@2016-07-26 00:54:22
        list = (List*)malloc(sizeof(List));
        *list = (Node*)malloc(sizeof(Node));
        pInitList(list);
        insertNode((*list), 1);
        insertNode((*list)->next, 2);
        insertNode((*list)->next->next, 3);
    
        findValue(*list, 2);
        findLast(*list);
    
        printList(*list);
    
        Node *delNode = (Node*)malloc(sizeof(Node));
        delNode->data = 2;
        deleteNode(*list, delNode);
        printList(*list);
    
        if(isNull(*list)){
            printf("list is  empty.
    ");
        }else{
            printf("list is not empty.
    ");
    
        }
    #endif
    
        return 0;
    }
    
  • 相关阅读:
    ollydbg设置断点经验总结
    proxifier 设置代理转发 对于IP地址的转发 也可以指定 这下C2裸IP的伪造可以做了
    畅捷通0day勒索攻击事件 几个IOC都只是漏洞的 勒索病毒本身没有看到ioc
    使用NSP机制解决windows7 dns溯源问题
    WMI后门技术的攻击与检测
    使用Windbg OllyDbg从头调试windows服务
    “漏洞利用之王”HolesWarm挖矿木马——非常典型的代理挖矿
    Proxychains.exe Proxychains for Windows ——通过给动态链接的程序注入一个 DLL,对 Ws2_32.dll 的 Winsock 函数挂钩子的方式来将应用程序的连接重定向到 SOCKS5 代理
    Lua 面向对象(实现类的创建和实例化、封装、继承、多态)
    lua ngx shared——Nginx本地缓存用法
  • 原文地址:https://www.cnblogs.com/ashen/p/5705954.html
Copyright © 2020-2023  润新知