C Language Realization __c language of list (linked list)

Source: Internet
Author: User
Tags prev sin

The objects in the

List are sorted in a linear order, and the order is determined by the pointers in each object. The
list has several forms, which can be either single or double linked, either sorted or unordered, and can be either circular or non circular. The structure body and functions defined by
are as follows:

typedef struct SINGLE_LINKED_LIST {Node * head;
Node * TAIL;
} SLL;
    typedef struct CIRCULAR_SINGLE_LINKED_LIST {Node * head;
Node * TAIL;
} csll;
    typedef struct DOUBLE_LINKED_LIST {Node * head;
Node * TAIL;
} DLL; typedef struct CIRCULAR_DOUBLE_LINKED_LIST_WITH_SENTINEL {Node * NIL;}

Cdlls;
void Linked_list_traverse (Node * L);

void Linked_list_free (node* L);
node* Single_linked_list_insert (SLL * L, item_t item);
node* Single_linked_list_search (SLL * L, item_t item);

int Single_linked_list_delete (SLL * L, node * node);
node* Circular_single_linked_list_insert (CSLL * L, item_t item);
node* Circular_single_linked_list_search (CSLL * L, item_t item);

int Circular_single_linked_list_delete (CSLL * L, node * node);
node* Double_linked_list_insert (DLL * L, item_t item);
node* Double_linked_list_search (DLL * L, item_t item);

int Double_linked_list_delete (DLL * L, node * node);
cdlls* Circular_double_linked_list_with_sentinel_init (); node* Circular_double_liNked_list_with_sentinel_insert (Cdlls * L, item_t item);
node* Circular_double_linked_list_with_sentinel_search (Cdlls * L, item_t item); int Circular_double_linked_list_with_sentinel_delete (Cdlls * L, node * node);

Several common functions are as follows:

Functions for all kind of linked list
void Linked_list_traverse (node* l) {
    if (l = = NULL) {
        printf ("Empty Li nked list.\n ");
    }
    Node * pos = L;
    while (POS!= NULL) {
        printf ("%2d be in location:%9p.") Prev is%9p. Next is%9p.\n ", Pos->item.key,
            pos, Pos->prev, pos->next);
        pos = pos->next;
        if (pos = L) return;
    }

void Linked_list_free (node* l) {
    Node * pos = l;
    Node * f = L;
    while (POS!= NULL && pos!= L) {
        f = pos;
        pos = pos->next;
        Free (f);
    }
}
//-----------------------------------------------------------------------

A single linked unordered list of non-circular links can be implemented as follows:

Functions for single linked list node* Single_linked_list_insert (SLL * L, item_t Item) {Node * node = (node*) malloc
    (sizeof (Node));
    Node->item = Item;
    Node->prev = NULL;
    Node->next = l->head;
    if (L->tail = = NULL) l->tail = node;
    l->head = node;
return node; } node* Single_linked_list_search (SLL * L, item_t Item) {if (L->head = NULL) {fprintf (stderr, "the Singl
        E linked list is empty.\n ");
    return NULL;
    Node * pos = l->head;
        while (POS!= NULL) {if (Item.key = = Pos->item.key) return to POS;
    pos = pos->next;
    } fprintf (stderr, "The item cannot be found.\n");
return NULL; int Single_linked_list_delete (SLL * L, Node * node) {if (L->head = NULL) {fprintf (stderr, "the"
        Linked list is empty.\n ");
    return 0;
        } if (node = NULL) {fprintf (stderr, "The node is null.\n");
    return 0; } if (L->head = node) {L->head = node->next;
        if (L->head = null) L->tail = NULL;
    return 1;
    Node * pos = l->head;
    while (POS!= NULL && pos->next!= node) {pos = pos->next;
        } if (Pos->next = node) {if (node = l->tail) L->tail = pos;
        Pos->next = pos->next->next;
    return 1;
    } fprintf (stderr, "No such node in linked list.\n");
return 0; int Single_linked_list_reverse (SLL * L) {if (L->head = NULL) {fprintf (stderr, "the single linked list
        is empty.\n ");
    return 0;
    } L->tail = l->head;
    Node *pre, *current, *next;
    Pre = l->head;
    Current = l->head->next;
    L->head->next = NULL;
    Next = NULL;
        While (the current!= NULL) {next = current->next;
        Current->next = pre;
        Pre = current;
    current = next;
    } l->head = Pre; RetuRN 1; }
//------------------------------------------------------------------------

The function name and return value of each function are visible. Dynamically allocating memory when using SLL, such as:

void Test_for_single_linked_list () {SLL * L = (sll*) malloc (sizeof (SLL));
    printf ("%p\n", L->head);
    item_t item = {ten, NULL};
    node * node;
        for (int i = 0; i < i++) {Item.key = i + 10;
    Single_linked_list_insert (L, item);
    printf ("SLL head are in location:%p\n", l->head);
    Linked_list_traverse (L->head);
    Single_linked_list_reverse (L);
    Linked_list_traverse (L->head);
        for (int i =-1; I <= i++) {Item.key = i + 10;
        node = Single_linked_list_search (L, item); if (node!= NULL) printf ("Node%d in location%p Delete:%d\n", Node->item.key, node, \ Sin
        Gle_linked_list_delete (L, node));
    printf ("SLL tail is in location:%p\n", l->tail);
    printf ("---------------------------------------------\ n");
        for (int i = 0; i < i++) {Item.key = i + 10;
    Single_linked_list_insert (L, item); printf ("SLL head are in LocatiOn:%p\n ", l->head);
    Linked_list_traverse (L->head);
        for (int i = one; I >=-1; i--) {Item.key = i + 10;
        node = Single_linked_list_search (L, item); if (node!= NULL) printf ("Node%d in location%p Delete:%d\n", Node->item.key, node, \ Sin
        Gle_linked_list_delete (L, node));
    printf ("SLL tail is in location:%p\n", l->tail);
    } linked_list_free (L->head);
Free (L); }

A doubly-linked unordered, non-circular list can be implemented as follows:

Functions for double linked list node* Double_linked_list_insert (DLL * L, item_t Item) {Node * node = (node*) malloc
    (sizeof (Node));
    Node->prev = NULL;
    Node->item = Item;
    Node->next = l->head;
    if (l->head!= NULL) L->head->prev = node;
    else L->tail = node;
    l->head = node;
return node; } node* Double_linked_list_search (DLL * L, item_t Item) {if (L->head = = NULL) {fprintf (stderr, "the Doubl
        E linked list is empty.\n ");
    return NULL;
    Node * pos = l->head;
        while (POS!= NULL) {if (Pos->item.key = = Item.key) return to POS;
    pos = pos->next;
    } fprintf (stderr, "The item cannot be found.\n");
return NULL; int Double_linked_list_delete (DLL * L, Node * node) {if (L->head = = NULL) {fprintf (stderr, "the double
        Linked list is empty.\n ");
    return 0; } if (node = NULL) {fprintf stderr, "The node is NULl.\n ");
    return 0;
    } if (node = = l->tail) L->tail = node->prev;
    if (Node->prev!= NULL) {node->prev->next = node->next;
    else {l->head = node->next;
    } if (Node->next!= NULL) {Node->next->prev = node->prev;
return 1; }
//----------------------------------------------------------------------------

Note that the key points are the same as the unordered, unordered list of linked lists. The test can be as follows:

void Test_for_double_linked_list () {DLL * L = (dll*) malloc (sizeof (DLL));
    printf ("%p\n", L->head);
    item_t item = {ten, NULL};
    node * node;
        for (int i = 0; i < i++) {Item.key = i + 10;
    Double_linked_list_insert (L, item);
    printf ("DLL head are in location:%p\n", l->head);
    Linked_list_traverse (L->head);
        for (int i =-1; I <= i++) {Item.key = i + 10;
        node = Double_linked_list_search (L, item); if (node!= NULL) printf ("Node%d in location%p Delete:%d\n", Node->item.key, node, \ Dou
        Ble_linked_list_delete (L, node));
    printf ("DLL tail is in location:%p\n", l->tail);
    printf ("---------------------------------------------\ n");
        for (int i = 0; i < i++) {Item.key = i + 10;
    Double_linked_list_insert (L, item);
    printf ("DLL head are in location:%p\n", l->head);
    Linked_list_traverse (L->head); for (int i = 11; I >=-1;
        i--) {Item.key = i + 10;
        node = Double_linked_list_search (L, item); if (node!= NULL) printf ("Node%d in location%p Delete:%d\n", Node->item.key, node, \ Dou
        Ble_linked_list_delete (L, node));
    printf ("DLL tail is in location:%p\n", l->tail);
    } linked_list_free (L->head);
Free (L); }

The

Single link unordered circular list can be implemented as follows:

Functinos for circular single linked list node* Circular_single_linked_list_insert (CSLL * L, item_t Item) {Node * n
    Ode = (node*) malloc (sizeof (Node));
    Node->item = Item;
    Node->prev = NULL;
        if (L->head = = NULL) {l->head = node;
        l->tail = node;
    Node->next = node;
        else {Node->next = l->head;
        l->head = node;
    L->tail->next = node;
} return node; } node* Circular_single_linked_list_search (CSLL * L, item_t Item) {if (L->head = NULL) {fprintf (stderr,
        "The circular single linked list is empty.\n");
    return NULL;
    Node * pos = l->head;
    if (Pos->item.key = = Item.key) return to POS;
    else pos = pos->next;
    while (Pos->item.key!= item.key && pos!= l->head) {pos = pos->next;
    } if (pos!= l->head) return POS;
    fprintf (stderr, "The item cannot be found.\n"); return NULL;
int Circular_single_linked_list_delete (CSLL * L, Node * node) {if (L->head = NULL) {fprintf (stderr, "
        The circular single linked list is empty.\n ");
    return 0;
        } if (node = NULL) {fprintf (stderr, "The node is null.\n");
    return 0;
        } if (node = = L->head && node = = l->tail) {l->head = NULL;
        L->tail = NULL;
    return 1;
        else if (node = = l->head) {L->head = node->next;
        L->tail->next = l->head;
    return 1;
    Node * pos = l->head;
    while (Pos->next!= node && pos!= l->tail) {pos = pos->next;
        } if (Pos->next = node) {if (node = l->tail) {l->tail = pos;
        } Pos->next = node->next;
    return 1;
    } fprintf (stderr, "No such node in linked list.\n");
return 0; }
//------------------------------------------------------------------------ 

The

test can be as follows:

void Test_for_circular_single_linked_list () {CSLL * L = (csll*) malloc (sizeof (CSLL));
    printf ("%p\n", L->head);
    item_t item = {ten, NULL};
    node * node;
        for (int i = 0; i < i++) {Item.key = i + 10;
    Circular_single_linked_list_insert (L, item);
    printf ("Csll head are in location:%p\n", l->head);
    Linked_list_traverse (L->head);
        for (int i =-1; I <= i++) {Item.key = i + 10;
        node = Circular_single_linked_list_search (L, item); if (node!= NULL) printf ("Node%d in location%p Delete:%d\n", Node->item.key, node, \ Cir
        Cular_single_linked_list_delete (L, node));
    Linked_list_traverse (L->head);
    printf ("---------------------------------------------\ n");
        for (int i = 0; i < i++) {Item.key = i + 10;
    Circular_single_linked_list_insert (L, item);
    printf ("Csll head are in location:%p\n", l->head); Linked_list_trAverse (l->head);
        for (int i = one; I >=-1; i--) {Item.key = i + 10;
        node = Circular_single_linked_list_search (L, item); if (node!= NULL) printf ("Node%d in location%p Delete:%d\n", Node->item.key, node, \ Cir
    Cular_single_linked_list_delete (L, node));
    } linked_list_free (L->head);
Free (L); }

A double link with a sentry. Unordered circular lists can be implemented as follows:

Functions for cicular double linked list with Sentinel
cdlls* Circular_double_linked_list_with_sentinel_init () C1/>cdlls * L = (cdlls*) malloc (sizeof (cdlls));
    L->nil = (node*) malloc (sizeof (Node));
    L->nil->
Related Article

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.