C algorithm introduction-linked list (1)

Source: Internet
Author: User
Document directory
  • Example 2 Implementation of abstract data types in linked lists
C algorithm-linked list is the most basic data structure. A linked list is composed of a group of elements in a specific order or linked together. It is useful when maintaining data sets. During normal data processing, links are often used for temporary data storage. But how can I optimize the data processed by the linked list? The following describes how to process a single-chain table! 1. Single-chain table Introduction A Single-chain table is composed of various elements connected by a pointer. An element can be divided into two parts: a data member and a pointer called nest. In this two-member structure, the next pointer of each element points to the pointer behind the element. To better describe this structure, you can see:

To access the elements in the linked list, start from the head of the linked list and use the next pointer from one element to another. However, each element must dynamically apply for and release space. The connection between elements ensures that each element can be accessed. Because you must be especially careful when processing data. If an element is lost in the middle, the latter cannot be accessed. 2 implementation and analysis of a single-chain table example 1 header file of the abstract data type of the linked list
#ifndef LIST_H#define LIST_H#include <stdlib.h>/*define a structure for linked List elements */typedef struct ListElmt_{    void *data;    struct ListElmt_ *next;}ListElmt;/*define a structure for linked lists */typedef struct List_ {    int size;    int (*match)(const void * key1, const void * key2);    void (*destroy)(void *data);    ListElmt *head;    ListElmt *tail;}List;/*Public Intefaces */void list_init(List *list, void (*destroy)(void *data));void list_destory(List *list);int list_ins_next(List *list, ListElmt *element, const void *data);int list_rem_next(List *list, ListElmt *element, void *data);#define list_size(list)            ((list) -> size)#define list_head(list)           ((list) -> head)#define list_tail(list)              ((list) -> tail)#define list_is_head(list, element)    ((element) == (list) -> head ? 1:0)#define list_is_tail(list, element)  ((element) == (list) -> tail ? 1:0)#define list_data(element)          ((element) -> data)#define list_next(element)          ((element) -> next)#endif
Example 2 Implementation of abstract data types in linked lists
#include <studio.h>#include <string.h>#include "../include/List.h"/*list_init */void list_init(List * list, void(* destroy)(void * data)) {    /* initialize the list */    list -> size = 0;    list -> destroy = destroy;    list -> head = NULL;    list -> tail = NULL;    return ;}/*list_destroy */void list_destory(List * list) {    void *data;    /*Remove eah element */    while (list_size(list) > 0){        if (list_rem_next(list, NULL, (void **)  &data) == 0 && list -> destroy         != NULL){        /* Call a user-defined function to free dynamically allocated data*/            list -> destroy(data);        }    }    memset(list, 0, sizeof(List));    return;}/*list_ins_nest */int list_ins_next (List * list, ListElmt * element, const void * data){    ListElmt *new_element;    /* Allocate storage for the element */    if ((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL){        return -1;    }    /*insert the element into the list */    new_element ->data =(void *)data;    if (element == NULL){        /* handle insertion at the head of the list */        if (list_size(list) == 0){            list -> tail = new_element;        }        new_element ->next = list ->head;        list -> head = new_element;    } else {        /*Handle insertion somewhere other than at the head */        if (element ->next == NULL ){            list ->tail = new_element;        }        new_element ->next = element ->next;        element->next = new_element;                   }    list->size ++;    return 0;    }/* list_rem_next */int list_rem_next(List * list, ListElmt * element, void * data) {     ListElmt *old_element;     /* Do not allow removal from an emptry list */     if (list_size(list) == 0){        return -1;     }     /* romove the element from the list  */     if (element == NULL){        /* handle removel from the head of the list */        *data = list->head->data;        old_element = list ->head;        list->head = list -> head->next;        if (list_size(list) == 1){            list->tail = NULL;        }         } else {         /* handle removal from somewhere other than the head */          if ( element->next == NULL){            return -1;          }          *data = element->next->data;          old_element = element->next;          element->next = element->next->next;          if (element ->next == NULL){              list->tail = NULL;          }               }     /* free the storage allocated by the abstract datatype*/     free(old_element);     list->size --;     return 0;}
3. The following describes several important function interfaces. list_init is used to initialize a linked list meter to perform other operations. The main member description is size: number of elements in the linked list; initialized to 0; destroy: defined a destructor (http://baike.baidu.com/view/1277985.htm); initially passed in with function parameters; head and tail: two pointers to the primary header and table tail. The initialization is null. list_destroy is used to destroy the linked list, that is, to retrieve all functions in the linked list. The function judges the size cyclically and exits when the size is 0. The function is implemented by calling the list_rem_next function. If the second parameter is null, the function is deleted from the first element. If the Destructor is not null, you should call the destructor. List_ins_next insert an element into the element specified by the element. The data of the new element is only the data imported by the user. If you want to insert data to a linked list, consider the following: insert header or other locations. For details, refer to the implementation of the function. When the input parameter element is null, it indicates to remove the node after the specified element from the linked list from the insert to the header; list_rem_next, and save the stored data in the parameter data; you also need to consider the case 2: delete the header or other locations. If the element parameter is null, it indicates that it is deleted from the header! 4. Two-way linked list: As the name implies, linked list elements are linked by two pointers. Each element in the two-way linked list consists of three parts: in addition to the data member and pointer next, a new pointer Prev is added to point to its precursor element. Clearly draw the structure of a two-way linked list:

Implementation and Analysis of Two-way linked list
#ifndef DLIST_H#define DLIST_H#include <stdlib.h>/*define a structure for doubly-linked list elements */typedef struct DlistElmt_ {    void *data;    struct DlistElmt_ *prev;    struct DlistElmt_ *next;}DlistElmt;/*define a structure for doubly-linked lists */typedef struct Dlist_ {    int size;    int (*match)(const void *key1, const void * key2);    void (*destroy)(void *data);    DlistElmt *head;    DlistElmt *tail; }Dlist;/*Public Intefaces */void dlist_init(Dlist *dlist, void (*destroy)(void *data));void dlist_destory(Dlist *dlist);int dlist_ins_next(Dlist *dlist, DlistElmt *element, const void *data);int dlist_ins_prev(Dlist *dlist, DlistElmt *element, const void *data);int dlist_remove(Dlist *dlist, DlistElmt *element, void *data);#define dlist_size(dlist)            ((dlist) ->size)#define dlist_head(dlist)           ((dlist) ->head)#define dlist_tail(dlist)              ((dlist) ->tail)#define dlist_is_head(element)    ((element) ->prev == NULL? 1:0)#define dlist_is_tail(element)  ((element) ->next == NULL ? 1:0)#define dlist_data(element)          ((element) ->data)#define dlist_next(element)          ((element) ->next)#define dlist_prev(element)      ((element)->prev)#endif

 

/* dlist.c */#include <stdlib.h>#include <string.h>#include "../include/Dlist.h"/*dlist_init */void dlist_init(Dlist * dlist, void(* destroy)(void * data)){    /* initialize the list */    list -> size = 0;    list -> destroy = destroy;    list -> head = NULL;    list -> tail = NULL;    return ;}/*list_destroy */void dlist_destory(Dlist * dlist){    void *data;    /*Remove eah element */    while (dlist_size(dlist) > 0){        if (dlist_remove(dlist, dlist_tail(dlist), (void **)  &data) == 0 && dlist -> destroy         != NULL){        /* Call a user-defined function to free dynamically allocated data*/            dlist ->destroy(data);        }    }    memset(dlist, 0, sizeof(Dlist));    return;}/*dlist_destroy */void dlist_destory(Dlist * dlist) {    void *data;    /* remove each element */    while (dlist_size(dlist) > 0){        if (dlist_remove(Dlist * dlist, dlist_tail(dlist), void * data) == 0 &&         dlist->destroy != NULL){            /* call a user-defined function to free dynamically allocated data */            dlist->destroy(data);            }        }    memset(list,0,sizeof(Dlist));    return;    }/* dlist_ins_next */int dlist_ins_next(Dlist * dlist, DlistElmt * element, const void * data){    DlistElmt *new_element;    /* do not allow a NULL element unless dlist is empty*/    if (element == NULL && dlist_size(dlist) == 0){        return -1;    }    /* allocate storage for the element */    if ((new_element = (DlistElmt *)malloc(sizeof(DlistElmt))) == NULL){        return -1;    }    new_element->data = (void *) data;    if (dlist_size(dlist) == 0){        dlist->head = new_element;        new_element->prev = NULL;        new_element->next = NULL;        dlist->tail = new_element;    } else {        new_element->next = element->next;        new_element->prev = element;        if (element->next == NULL){            dlist->tail = new_element;        } else {            element->next->prev = new_element;        }        element->next = new_element;    }    dlist->size++;    return 0;}int dlist_ins_prev(Dlist * dlist, DlistElmt * element, const void * data){     DlistElmt *new_element;    /* do not allow a NULL element unless dlist is empty*/    if (element == NULL && dlist_size(dlist) == 0){        return -1;    }    /* allocate storage for the element */    if ((new_element = (DlistElmt *)malloc(sizeof(DlistElmt))) == NULL){        return -1;    }    new_element->data = (void *) data;    if (dlist_size(dlist) == 0){        dlist->head = new_element;        new_element->prev = NULL;        new_element->next = NULL;        dlist->tail = new_element;    } else {        new_element->next = element;        new_element->prev = element->prev;        if (element== NULL){            dlist->tail = new_element;        } else {            element->prev->next= new_element;        }        element->prev = new_element;    }    dlist->size++;    return 0;}int dlist_remove(Dlist * dlist, DlistElmt * element, void * data) {    /*do not allow a NULL element or removal from an empty list */    if (element == NULL || dlist_size(dlist) == 0){        return -1;    }    /* Remove the element from the list */    *data = element->data;    if (element == dlist->head){        /*handle removal from the head of the list */        dlist->head = element->next;        if (dlist->head ==NULL){            dlist->tail = NULL;        } else {            element->next->prev = NULL;        }    } else {        element->prev->next = element ->next;        if (element->next == NULL){            dlist->tail = element->prev;        } else {            element ->next->prev = element->prev;        }    }    free(element);    dlist->size--    return 0;}

The above is the function implementation process of the two-way linked list, which is similar to the one-way linked list!

 

 

 

 

 

 

 

 

 

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.