Static linked list C language description

Source: Internet
Author: User

Static linked list
1. A cursor with subscript 0 holds the last cursor that holds the data node, which is the first subscript that does not contain the element (alternate list)
2. The last node holds the first count worth subscript
3. The first and last data are not stored as the first subscript of the standby list
4. The last node that stores data is a cursor of 0

Static linked list is mainly based on the cursor to traverse, before the idea of the use of pointers

If I want to delete an element

Figure not much description, draw on your own to understand, then the code part. All have comments,

1#include <stdio.h>2 #defineElemtype int3 #defineStatus int4 #defineMAXSIZE 10005typedefstruct{6Elemtype data;//Data7     intCur//Cursors8 }component,staticlinklist[maxsize];9 Ten  One //initializing a static linked list AStatus initlist (staticlinklist space)//initialize static linked list equals initialize array - { -     inti; the      for(i=0; i<maxsize-1; i++)   -space [i].cur=i+1;//assigning cursors to all nodes -        -space[maxsize-1].cur =0;//the cursor for the last node is the first element +       return 0; - } +  A  at //gets the first element subscript of an alternate list (insert operation) - intmalloc_sll (staticlinklist space) - { -     inti=space[0].cur;//use I to store cursors for the first node of a static list -     if(space[0].cur)//If the first cursor is nonzero, the cursor of the last node is assigned to the first node -space[0].cur=space[i].cur;//put the next one as the first element of the alternate list in        -       returnI//Subscript to insert to  }  +   -   the   *   $  //get the length of a linked listPanax Notoginseng  intlistlength (staticlinklist L) -  { the      intj=0;//used to calculate +      inti=l[maxsize-1].cur;//The cursor of the last element, the subscript of the first node that stored the data A       the       while(i)//Subscript does not perform loops for 0 +      { -I =l[i].cur;//traversing the node that knows the last data stored is subscript 0 $J + +; $     } -     returnJ; -  } the    -  Wuyi   the   -   Wu   -  //inserting a new data element before the first element in the static linked list L AboutStatus Listinsert (Staticlinklist L,inti,elemtype e) $  { -      intj,k,l; -       -K=maxsize-1;  A      if(i<1|| I>listlength (L) +1) +        return-1; the       -J=MALLOC_SLL (L);//gets the first element subscript of an alternate list $      if(j)//if the static linked list is not empty the      { theL[j].data=e;//inserts a value into the first element of the alternate list the           for(l=1; l<=i-1; l++)//gets the cursor of the element before it thek=l[k].cur; -               in               theL[j].cur=l[k].cur;//assigns the retrieved cursor to the newly inserted cursor theL[k].cur=j;//Assigns the subscript of the newly inserted node to the cursor of the first i-1 node About              return 0; the           the       }  the       return-1; +  } -   the  Bayi   the   the   -  //Reclaim the Idle node labeled K into the alternate table - voidFREE_SLL (staticlinklist space,intk) the { thespace[k].cur=space[0].cur; thespace[0].cur=K; the } -   the   the   the  94   the  //delete the I-element data in L theStatus Listdelete (Staticlinklist L,inti) the  {98      intj,k; About      if(i<1|| I>listlength (L))//Out of length, exiting the program -        return-1;101        102K=maxsize-1;//first cursor to save data103           for(j=1; j<=i-1; j + +)//Gets the previous cursor to delete the element104k=l[k].cur; the              106j=l[k].cur; 107L[k].cur=l[j].cur; 108     109 free_sll (l,j); the     return 0;111   }  the   

Insertion principle: Find the previous node cursor where you want to insert it, change its cursor to the subscript of the inserted element, and insert the cursor of the element to record the next subscript.

Although the static list is replaced by pointers, but this idea to understand, in the future when the development can be used.

Static linked list C language description

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.