"C Language Data structure" static single-linked list

Source: Internet
Author: User

StaticLinkLinst.h

#ifndef  STATIC_LINKLIST_H#define STATIC_LINKLIST_Htypedef void StaticLinkListNode;     //static single-linked list node typedef void staticlinklist;         //static single-linked list/* *  create static single-link list  *  @param  capacity  maximum capacity of static single-linked list  *  @return   Returns a static single-linked list pointer  */staticlinklist* staticlinklist_create (int capacity);/* *  destroys a static single-linked list  *   @param  list  pointer  */void staticlinklist_destroy for static single-linked list (staticlinklist *list);/*  *  empty static single-link list  *  @param  list  static single-linked list pointer  */void staticlinklist_clear ( staticlinklist *list);/* *  Insert element  *  @param  list  static single-linked list pointer to static single-linked list pos position  *   @param  node  element pointer  *  @param  pos  inserted index  */int staticlinklist_insert ( Staticlinklist *list,staticlinklistnode *node,int pos);/* *  Gets the element at the index position in the static single-linked list  *   @param  list    static single-linked table pointer  *  @param  pos    static single-linked list index values  *  @param  return  element pointers  */staticlinklistnode* staticlinklist_get (Staticlinklist *list,int pos);/* *  Delete values at index locations in a static single-linked list  *  @param  list  pointers to static single-linked lists  *  @param  pos    Static single-linked list index  *  @param  return  non-0 means delete succeeded  */int staticlinklist_remove (staticlinklist  *list,int pos);/* *  Gets the number of currently stored elements in a static single-linked list  *  @param  list  pointer to a static single-linked list  *   @return   Number of stored elements in a static single-linked list  */int staticlinklist_length (staticlinklist *list);/* *   Get the maximum number of storage elements for a static single-linked list  *  @param  list  pointers to static single-linked lists  *  @return   static single-linked list maximum number of stored elements  */int staticlinklist_capacity (staticlinklist *list); #endif  // staticlinklist_h

Staticlinklist.c

#include   "StaticLinkList.h" #include   "malloc.h" #define  no_node  -1typedef struct  _staticlinklistnode{    unsigned int data;  //data field Pointer      int next;           //the array subscript for the next node} tstaticlinklistnode;typedef struct _staticlinklist{    int length;     int capacity;    tstaticlinklistnode node[]; // Flexible array for storing static linked lists}tstaticlinklist;/* *  create static single-link list  *  @param  capacity  maximum capacity of static single-linked list  *   @return   Returns a pointer to a static single-linked list  */staticlinklist* staticlinklist_create (int capacity) {     //because the 0-bit position of a flexible array is used as the head node, the capacity is actually capapcity + 1    size_t size  = sizeof (tstaticlinklist)  + sizeof (Tstaticlinklistnode)  *  (capacity + &NBSP;1);     tstaticlinklist *list =  (tstaticlinklist *) malloc (size);     if (list !=  0)     {        int i;         list->capacity = capacity;         list->length = 0;        list->node[0]. Next = 0;        for (i = 1;i <=  capacity;i++)         {             list->node[i].next = NO_NODE;         }    }    return list;} /* *  destroy static single-link list  *  @param  list  static single-linked list pointer  */void staticlinklist_destroy ( Staticlinklist *list) {    free (list);} /* *  empty static single-link list  *  @param  list  static single-linked list pointer  */void staticlinklist_clear ( Staticlinklist *list) {    if (list != 0)     {         TStaticLinkList *s_list =  (tstaticlinklist *) list;        s_list->length = 0;    } }/* *  inserting elements  *  @param  list  static single-link table pointers to static single-linked list pos locations  *  @param  node  Element pointer  *  @param  pos  inserted index  *  @param  return  non 0 for insert success  */int  Staticlinklist_insert (Staticlinklist *list,staticlinklistnode *node,int pos) {     TStaticLinkList *s_list =  (tstaticlinklist *) list;    // Determine that the linked list and node pointers are not empty, the position is valid, and the capacity will not be greater than the maximum capacity     int ret =  (  (s_list != 0)  &&  (node != 0) &Nbsp;&&  (pos >= 0)  &&                   (pos <= s_list->length)  &&   (s_list->length + 1 <= s_list->capacity + 1)  );     if (ret)     {        int index  = -1;                     //array subscript         int current to be placed in the element  = 0;                    //array subscript for current node         int i = 0;         //traversal finds the free items in an array          for (I&NBSP;=1;&NBSP;I&NBSP;&LT;=&NBsp;s_list->capacity;i++)         {             if (S_list->node[i].next == no_node)              {                 index = i;                 break;             }        }        The  //is moved to the precursor         for where the insertion position is required (i = 0; i <  pos ; i++)         {             current = s_list->node[current].next;         }         s_list->node[index].next = s_list->node[ current].next;        s_list->node[index].data =  ( Unsigned int) node;        s_list->node[current].next =  index;        s_list->length++;    }     return ret;} /* *  gets the element at index position in a static single-linked list  *  @param  list    static single-link table pointer  *  @param   pos    static single-linked list index values  *  @param  return  element Pointers  */StaticLinkListNode*  Staticlinklist_get (staticlinklist *list,int pos) {    tstaticlinklistnode *s _node = 0;    tstaticlinklist *s_list =  (TStaticLinkList *) List;    if (  (list != 0)  &&  (pos >=0) &NBSP;&Amp;&  (pos < s_list->length))     {         int current = 0;        int  index = -1;        int i;         //move to the location where you want to query         for (i = 0; i  < pos ; i++)         {             current = s_list->node[current].next;         }        //gets the array subscript of the element          index = s_list->node[current].next;         //casts the type in data to staticlinklistnode * because the node's pointer is saved when inserted          s_node =  (staticlinklistnode *) s_list->node[index].data;    }     return s_node;} /* *  Delete the value at index position in a static single-linked list  *  @param  list  pointer to a static single-linked list  *  @param  pos     static single-linked list index  *  @param  return  0 = delete Success  */int staticlinklist_remove ( Staticlinklist *list,int pos) {    tstaticlinklist *s_list =  ( tstaticlinklist *) list;    int ret =  (  (s_list != 0)  &&  (pos >= 0)  &&  (pos < s_list->length));     if (ret)     {        int  index = -1;        int current = 0;         int i = 0;         for (i = 0; i < pos;i++)         {             current = s_list->node[current] . next;        }        // Array subscript for deleted elements         index = s_list->node[current].next;         //assigns the subsequent subscript of the deleted element to the successor subscript of the precursor of the removed element          s_list->node[current].next = s_list->node[index].next;         //setting the subsequent subscript for the deleted element is no_node        s_list- >node[index].next = NO_NODE;        s_list->length--;     }    return ret;} /* *  gets the number of currently stored elements in a static single-linked list  *  @param  list  pointer to a static single-linked list  *  @return   Number of stored elements in a static single-linked list  */int staticlinklist_length (staticlinklist *list) {     int ret = -1;    if (list != 0)      {        TStaticLinkList *s_list =  ( tstaticlinklist *) list;        ret = s_list->length;     }    return ret;} /* *  gets the number of maximum storage elements for a static single-linked list  *  @param  list  pointers to static single-linked lists  *  @return   Static single-linked list maximum number of  */int staticlinklist_capacity elements (staticlinklist *list) {    int  ret = -1;    if (list != 0)     {         TStaticLinkList *s_list =  (tstaticlinklist *) list;         ret = s_list->capacitY;    }    return ret;} 

Test Code

 #include  <stdio.h> #include   "StaticLinkList.h" int  Main (void) {    int i,*j;    int a[5];     staticlinklist *list = staticlinklist_create (5);    for (i = 0;i  < 5;i++)    {       a[i] = i;    }   for (i = 0; i < 5;i++)    {         staticlinklist_insert (list,& (a[i]), 0);   }    staticlinklist_remove (list,0);    for (i = 0; i <  Staticlinklist_length (list); i++)    {      j =  Staticlinklist_get (list,i);       printf ("%d\n", *j);   }    return 0;} 


This article is from the "Walt" blog, make sure to keep this source http://water3700348.blog.51cto.com/13058625/1965202

"C Language Data structure" static single-linked list

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.