Static linked list

Source: Internet
Author: User

Why a static linked list is required:

C has pointers that make it easy to manipulate addresses and data in memory, such as Java, C #, and other object-oriented languages without correcting them, but they enable the object. But some early-morning programming languages, such as Basic, Fortran, and so on without pointers, need static lists. second, static linked list is what:a static linked list is a column-based storage structure that uses arrays to describe new tables in a county. When he defines it, there are data fields, and the pointer field next, where the pointer is actually the subscript in the array where the element is stored. (This image is from internet users)As shown in the figure, the non-idle list islink the elements in order by saving the array subscript of the next element. Iii. Summary of the list: An array logic is divided into two parts, all of which are connected by pointers.
The idle list is linked by an idle head node, and the last node of the idle list is typically 0 next .A non-idle list is connected by a non-idle head node, since each request is an idle node from the next location of the idle head node, so the non-idle head node is the one labeled 1 on the arrayafter the element is saved in the list, the pointer field of the last node (list[maxsize-1].next) of the list is modified to 1, which is the head node that holds the non-empty list. Four, actual combat  
    1. The basic function
 
    • Static linked list definition
#define                   MaxSize-struct {int data;     int Next; //     }list;

    • Initializing static idle list
void INIT_SL (List list[])    {        int  i;          for (i=0; i<maxsize;i++)        {            List[i].next=i+1; // because they are not assigned, they point directly to the next         }        list[maxsize-1].next=0;    

    • Static linked list length
int listlength (List list[])    {        int j=0;         int i=list[maxsize-1].next;          while (i) // Stop when I<=0         {            i=list[i].next;            J++        ;        } return j;    }

    • Apply for allocation of idle nodes
idea: allocating nodes is the first node after the allocation of the idle chain header node, because the first points are allocated, so to adjust the head node, let him refer to the next idle node (that is, the original second idle node), and finally the application of the node array subscript returned  
int MALLOC_SL (List list[])    {        int i=list[0].next; //         if(i)        {            list[0].next=list[i].next;           }        returni ;   //    
    • Inserting a new element before the first element
idea: first find the first element of I, in let the new application next point I
 BOOLListinsert (List list[],intIinte) {intj,k,l; K=maxsize-1; if(i<1|| I>listlength (list) +1)//determine if the location is illegal            return false; J=MALLOC_SL (list);//apply for an idle node.        if(j) {List[j].data=e;  for(j=1; j<=i-1; j + +) {k=list[k].next;//element before the first element is found} list[j].next=list[k].next;//assign the first element before next to the new application next (also want to let J point i)List[k].next=j;//in the previous element of let I point to the J            return true; }        return false; }

    • Freeing memory
    void Free_ssl (List list[],int  k)    {         list[k].next=list[0].next;         list[0].next=k;    }
    • Static linked list Delete operation
 
BOOLListdelet (List list[],inti) {intj,k; if(i<1|| I>listlength (list) +1)        return false; Else{k=maxsize-1;  for(j=1; j<i;j++) {k=List[k].next; }//find the previous element of IJ=list[k].next;//Let J save the subscript for the first elementlist[k].next=List[j].next; return true; }        }    

 

Static 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.