Linked List {singly Linked list--doubly Linked list--Circular Linked list}

Source: Internet
Author: User


Linked List


unidirectional linked list singly linked list

/*********************************************************code Writer:eofcode File:single_linked_list.cCode Date:    2015.01.15e-mail: [Email protected]code Description:here is a implementation for singly linked list. If there is something wrong with my code please touch Meby e-mail, thank you.****************************************** /#include <stdio.h> #include <stdlib.h> #define Array_size 10//abstraction for our node    Which is in list.struct node{int num; struct node *next;};         struct node *list_search (struct node* p_head, int k) {if (!p_head) {printf ("Empty list!\n");    return NULL;    } struct node* p_tmp = p_head->next;        for (;p _tmp! = NULL; p_tmp = P_tmp->next) {if (P_tmp->num = = k) {break; }} return p_tmp;}         int List_insert (struct node *p_head, int num) {if (!p_head) {printf ("Empty list!\n");    return-1; } struct Node *p_node = malloc (sizeof (struct node));        if (!p_node) {printf ("malloc failed!\n");    return-1;    } p_node->num = num;    P_node->next = p_head->next;    P_head->next = P_node;   return 0;    }int list_delete (struct node *p_head, int num) {struct node *p_node = p_head;    struct node *p_tmp = NULL;        if (!p_head | |!p_node) {printf ("Empty list!\n");    return-1;            } for (;p _node->next! = NULL; p_node = P_node->next) {if (p_node->next->num = = num) {            P_tmp = p_node->next;            P_node->next = p_tmp->next;            Free (p_tmp);            P_tmp = NULL;        return 0;    }} printf ("There is node element%d in the list\n", num); return-1;}    struct node *init_list (void) {struct node *p_head = malloc (sizeof (struct node));        if (!p_head) {printf ("malloc failed!\n");    return NULL;    } p_head->next = NULL; /* * * * just garbageNumber, we don ' t use this * * Member in header node */p_head->num =-1; return p_head;}    void Release_list (struct node* p_head) {if (!p_head) {return;    } struct Node *p_node = NULL;    struct node *p_tmp = NULL;        for (P_node = P_head;         P_node->next! = NULL;        P_node = p_node->next) {p_tmp = p_node->next;        P_node->next = p_tmp->next;    Free (p_tmp); } free (P_node);}    void List_show (struct node *p_head) {if (!p_head) {return;    } struct Node *p_node = p_head;    for (; P_node->next! = NULL; p_node = p_node->next) {printf ("\t%d", p_node->next->num); } printf ("\ n");} /*-------------------------Test our API---------------------------------*/int main () {struct node *p_head = init_list (    );    int Array[array_size] = {1,2,3,4,5,6,7,8,9,10};    int i = 0;        for (i = 0; i < array_size; i++) {if (List_insert (P_head, Array[i]) < 0) {    Goto failed;    }} list_show (P_head); failed:release_list (P_head); return 0;}



doubly linked list doubly linked list

/*********************************************************code Writer:eofcode File:doubly_linked_list.cCode Date:    2015.01.15e-mail: [Email protected]code Description:here is a implementation for doubly linked list. If there is something wrong with my code please touch Meby e-mail, thank you.****************************************** /#include <stdio.h> #include <stdlib.h> #define Array_size 10//abstraction for our node    Which is in list.struct node{int num;    struct node *next; struct node *prev;};         struct node *list_search (struct node* p_head, int k) {if (!p_head) {printf ("Empty list!\n");    return NULL;    } struct node* p_tmp = p_head->next;        for (;p _tmp! = NULL; p_tmp = P_tmp->next) {if (P_tmp->num = = k) {break; }} return p_tmp;}         int List_insert (struct node *p_head, int num) {if (!p_head) {printf ("Empty list!\n"); return-1;   } struct Node *p_node = malloc (sizeof (struct node));        if (!p_node) {printf ("malloc failed!\n");    return-1;    } p_node->num = num;    P_node->next = p_head->next;    P_node->prev = P_head;    if (p_head->next! = NULL) {P_head->next->prev = P_node;    } p_head->next = P_node;   return 0;    }int list_delete (struct node *p_head, int num) {struct node *p_node = p_head;    struct node *p_tmp = NULL;        if (!p_head | |!p_node) {printf ("Empty list!\n");    return-1;            } for (;p _node->next! = NULL; p_node = P_node->next) {if (p_node->next->num = = num) {            P_tmp = p_node->next;            if (p_node->next->next! = NULL) {P_node->next->next->prev = P_node;            } P_node->next = p_tmp->next;            Free (p_tmp);            P_tmp = NULL;        return 0; }} printf ("There is node element%d in the list\n ", num); return-1;}    struct node *init_list (void) {struct node *p_head = malloc (sizeof (struct node));        if (!p_head) {printf ("malloc failed!\n");    return NULL;    } p_head->next = NULL;    P_head->prev = NULL;    /* * * * * * * Just garbage number, we don ' t use this * * Member in header node */p_head->num =-1; return p_head;}    void Release_list (struct node* p_head) {if (!p_head) {return;    } struct Node *p_node = NULL;    struct node *p_tmp = NULL; /*------------------------------use operating doubly linked list, so I-try to Dem    o use @prev.        for (P_node = P_head;         P_node->next! = NULL;        P_node = p_node->next) {p_tmp = p_node->next;        P_node->next = p_tmp->next;    Free (p_tmp); }------------------------------*/for (;p _node! = NULL; p_node = p_node->next) {p_tmp = P_node->pre      V  P_tmp->next = p_node->next;    Free (P_node); } free (P_node);}    void List_show (struct node *p_head) {if (!p_head) {return;    } struct Node *p_node = p_head;    for (; P_node->next! = NULL; p_node = p_node->next) {printf ("\t%d", p_node->next->num); } printf ("\ n");} /*-------------------------Test our API---------------------------------*/int main () {struct node *p_head = init_list (    );    int Array[array_size] = {1,2,3,4,5,6,7,8,9,10};    int i = 0;        for (i = 0; i < array_size; i++) {if (List_insert (P_head, Array[i]) < 0) {Goto failed;    }} list_show (P_head); failed:release_list (P_head); return 0;}



ring linked list circle linked listspecifically a little lazy, directly to see the question of Joseph Ring, the typical ring linked list to solve the problem.

http://blog.csdn.net/cinmyheart/article/details/19207097








Linked List {singly Linked list--doubly Linked list--Circular 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.