One-way linked list C language implementation

Source: Internet
Author: User

  • Definition (quote Baidu Encyclopedia)
    A single linked list is a chain-access data structure that stores data elements in a linear table with an arbitrary set of storage units of addresses. The data in the list is represented by nodes, each node's composition: the element (the image of the data element) + pointer (indicating where the successor element is stored), the element is the storage unit where the data is stored, and the pointer is the address data that connects each node.
  • Scene
  • In the actual production, it is possible that after the software startup, some data for the multi-state expansion, for example, the network card transceiver packet, from the protocol stack to generate a demand package, need to queue, waiting for the network card to send the data out, in the queue processing, so this use the heap of scattered memory, The results of data based on nodes are of some significance.

      1. C language Implementation, chat for record
        Data structure of the linked list

        typedef struct node{    int data;                                           //数据域    struct node *next;                          //指针域}NODE_t;

        Creating a linked List

        NODE_t *CreatNodeList(){NODE_t *head = NULL;head = (NODE_t *)malloc(sizeof(NODE_t));if(!head)    exit(-1);head->next = NULL;return head;}

    Linked list insertion, head insertion, header node, easy traversal, processing

    int InsertNode(NODE_t *head,int data){    NODE_t *cur = NULL;    if(!head)        exit(-1);    cur = (NODE_t *)malloc(sizeof(NODE_t));    if(!cur)        exit(-1);    cur->data = data;    //cur 插入到 head 和 head->next 之间    cur->next = head->next;    head->next = cur;    return 0;}

    Finding a Node

    NODE_t *findNode(NODE_t *head,int data){    head = head->next;    while(head)    {        if(head->data == data)        {            break;        }        head = head->next;    }    if(head == NULL)    {        printf("sorry,%d is not in the list\n",data);    }    return head;}

    Deletion of nodes

        int DeleteNodeOfList(NODE_t *head,NODE_t *pfind){    // 首先找到这个需要删除指针的前一个节点的指针    // 因为pfind 的合法性在外面判断,此处不再判断    while(head->next != pfind)    {        head = head->next;    }    head->next = pfind->next;    free(pfind);    pfind = NULL;    return 0;}

    The deletion here, assuming a large number of nodes, it will cause a problem, the single-linked list can only one direction, you need to find the node to delete the precursor pointer, you need to start from the beginning to traverse, a waste of resources, so, this place has the optimization space, that is, once you have to delete the node, you can do so

      • Copy the data field data from the drive node to the current Delete node data field
      • Remove a post-drive pointer to a deleted node

    The optimized version is as follows:

    // 优化点: 不必每次都遍历所有的节点,找到前驱节点// 将这个需要删除的节点的后驱节点的数据域拷贝过来,然后删除这个后驱节点int DeleteNodeOfList_Better(NODE_t *head,NODE_t *pfind){    NODE_t *p = pfind->next;    //最后一个节点,它其后没有后驱节点,所以需要从头遍历,找到它的前置节点    if(pfind->next == NULL)    {        while(head->next != pfind)        {            head = head->next;        }        head->next = pfind->next;        free(pfind);        pfind = NULL;    }    else //对于除最后一个节点的外的其他位置节点,则使用覆盖后删除后置节点的方式实现删除    {        pfind->data = pfind->next->data;        pfind->next = pfind->next->next;        free(p);        p = NULL;    }    return 0;}

    Once you find the pointer operation of the node is only one operation for the data field, it is more convenient
    Changes to the node

    int UpdateNode(NODE_t *head,int olddata,int newdata){    NODE_t *p = findNode(head,olddata);    if(p)    {        p->data = newdata;    }    return 0;}

    Traverse Print Display

    void showList(NODE_t *head){    head = head->next;    while(head)    {        printf("%d ==> ",head->data);        head = head->next;    }    printf("end..\n");}

    Sorting of linked lists

    int sortList(NODE_t *head){    int i = 0,j = 0;    int listlen = 0;    int tmpData = 0;    NODE_t *p = NULL;    // 使用冒泡排序,不动指针域,比较数据域,使用临时变量,将有大小之别的节点的数据域交换    // 得到链表长度,方便冒泡    listlen = ListNodeLen(head);    // 指到首节点    p = head->next;    for(i = 0;i < listlen-1;i++)    {        // 每一轮从头开始        p = head->next;        for(j = 0;j<listlen - i-1;j++)        {            // 将小值排在前面            if(p->data > p->next->data)            {                tmpData = p->data;                p->data = p->next->data;                p->next->data = tmpData;            }            p = p->next;        }    }    return 0;}

    One-way linked list C language implementation

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.