Static linked list----data structure (C language version) algorithm 2.17 Experience

Source: Internet
Author: User

  today learned the data structure of the static linked list, just beginning some statements do not understand, the study of the majority of days finally figured out. The record is left to be viewed later, and for those who have a little bit of an understanding of the static list. The article inevitably has the understanding question, please correct me. Enter the text below.

I. Static linked list structure

A static linked list can use a linked list structure in a high-level programming language without a "pointer" type. The static list is as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/4A/wKiom1V4CFjQ9EpNAADahlgxTtE084.jpg "title=" Static linked list. png "alt=" wkiom1v4cfjq9epnaadahlgxtte084.jpg "style=" float:left;/> First, Check out the static list. A static linked list is similar to a single linked list, contains many nodes, the first node includes data space[i].data, and cursor cur, and the value of the cursor space[i].cur is the array subscript for the next node.

1: Similar to the head node in a single-linked list, the value of this node cursor is space[0].cur the array subscript for the next request for an idle node.

2: Array subscript

3: Data field

4: Ring (the value of the space[i].cur is the array subscript for the next node)

S: Static linked list of head pointers

 R: The tail pointer of a non-empty list, which always points to the tail node of a non-empty list.Here the pointer is mainly to let the non-empty node linked list can be linked up, non-empty nodes linked to the tail node of the cur are assigned a value of 0, when the re-application of a blank node, you need to assign a blank node array subscript value to the tail node cur. At first I didn't understand the point.What exactly does the needle do so that it goes into a misunderstanding and studies half of the genius to understand it.


Again, in pre-allocated space, all nodes are arranged as they are initialized. The node pairs in the space can be divided into two parts, one is a blank node chain, the other part is a non-empty node linked list. Take the first node from the list of empty nodes each time you allocate. As explained in 1 above, the first node is taken from the empty node chain each time it is allocated. Each time a node is released, the released node is placed in the first position of the empty node chain. That is, the value of Space[0].cur is the array subscript for the released node.

A static single-linked table storage structure---------linear table---------------#define MAXSIZE 1000;    The maximum length of the linked list typedef struct{ELEMTYPE data; int cur;} Component,slinklist[maxsize];


Second, initialize

void Initspace_sl (Slinklist &space) {//To chain the components of an array space into an alternate list, space[0].cur as the head pointer,//"0" for the null pointer for (i = 0;i<ma XSIZE-1;    ++i) Space[i].cur = i + 1; space[maxsize-1].cur = 0;} Initspace_sl

static single-Link table node Queue Length is MAXSIZE, the array subscript is 0~maxsize-1, the specific operation of the initialization is when i∈[0,maxsize-2], space[i].cur = i + 1, and then the last node cur set to 0, That is Space[maxsize-1].cur = 0.

In a linked list, when the node's cur is 0 o'clock, it indicates that the node is a tail node.

  The static linked list above is the result of initialization when maxsize=12.


III. Allocation of space

int Malloc_sl (slinklist &space) {//If the spare space list is not empty, the assigned node subscript is returned, otherwise 0 i = Space[0].cur is returned;    if (space[0].cur) space[0].cur = space[i].cur; return i;} Malloc_sl

The cur of the first node of a static single-linked list, that is, I = Space[0].cur is the array subscript for the requested space, if I! = 0, I is returned, and then the array is labeled as the cur of the node corresponding to I, that is space[i].cur assigned to Space[0].cur, To find the location of the next blank node the next time you apply for space.

As explained above, if i = 0, then the cur of the last application's blank node is 0, which is the tail node, and no blank nodes are allocated. Therefore, return 0.


Iv. release of the nodal points

void Free_sl (Slinklist &space,int k) {//The idle node labeled K is retracted to the alternate list//The idle node labeled K is placed in the first position of the empty node list space[k].cur = Space[0]. Cur Space[0].cur = k;} Free_sl

Note that the operation here simply frees up the space labeled K in the array. Releases the nodes that have been removed from the non-empty node linked list, and then places them in the first position of the empty node list.


Five , (A-B) U (b-a)

Void difference (slinklist &space,int &s) {    //Enter the elements of the collection A and B in turn, A static linked list that represents the set (A&NBSP;-&NBSP;B) U (A&NBSP;+&NBSP;B)     //is established in an array space, and S is its head pointer. Assuming that the spare space is large enough, Space[0].cur is its head pointer: &NBSP;&NBSP;&NBSP;&NBSP;INITSPACE_SL (space);          //Initialize alternate space &NBSP;&NBSP;&NBSP;&NBSP;S&NBSP;=&NBSP;MALLOC_SL (space);         // Generating the head node of S     r = S;           &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//R points to the current last node of S     &NBSP;SCANF (m,n);                   //Enter the number of elements of A and B     for (j = 1; j <= m; ++ &NBSP;J) {   //Creates a linked list of set a &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;I&NBSP;=&NBSP;MALLOC_SL ( Space);    //distribution Node         scanf (space[i].data);     // Enter the element value of a         space[r].cur = i; r = i;  Insert to end of table     }//for    space[r].cur       The pointer to the            //tail node is an empty     for (j &NBSP;=&NBSP;1;&NBSP;J&NBSP;&LT;=&NBSP;N;&NBSP;++J) {     //enters the element of B in turn, if it is not in the current table, insert, otherwise                                   //Delete    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF (b); p = s; k = space[0].cur;     //k points to the first node in set a         while (K != space[r] . cur && space[K].DATA&NBSP;!=&NBSP;B) { //Find in the current table              p = k; k = space[k].cur;        }// While        if (k == space[r].cur) {    / /The change element does not exist in the current table and is inserted after the node referred to by R, and r                                    //'s position is unchanged             i =  MALLOC_SL (space);             space[i].data =  b;            space[i].cur = space [r].cur;            space[r].cur = i;         }//if        else{                      //the element is already in the table, delete the             space[p].cur = space[k].cur; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FREE_SL (space, k);             if (r == k)  r = p;      //If you delete the node referred to by R, you need to modify the tail pointer         }//else     }//for}//difference

  first, initialize the static linked list of maxsize lengths. " Span style= "line-height:0px;" >

  The enters the elements of a in a static linked list in turn. The r pointer points to the tail node of the non-empty list. " Span style= "line-height:0px;" > I did not understand at first, when initializing the static linked list, each linked list already has a subsequent cur pointing to the next node, why it needs space[r].cur = i this statement. Thinking about the big half-day, suddenly realized that the non-empty list of tail node of the successor Cur is 0, must be the next application of the node array coordinates assigned to it, and then the pointer r points to I, if I is a tail node, its successor cur assigned to 0,space[r].cur = 0.

The elements in B in order and a in the comparison, if present delete the element in a, release the node, and place the node in the first position of the blank node list, and determine whether it is a tail node, if so, you need to move the tail pointer r forward. If it does not exist, add the element in B to the last of the non-empty node list and assign its successor cur to 0.


At this point, the static linked list with this algorithm has already described some of my understanding, if wrong, please correct me.



This article is from the "April" blog, so be sure to keep this source http://aprilwu.blog.51cto.com/9784463/1660940

Static linked list----data structure (C language version) algorithm 2.17 Experience

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.