Data structures and algorithms-Learning note 7

Source: Internet
Author: User

Static linked list

Static linked list: A linked list, described by an array, is called a cursor implementation method.

Static linked list storage structure for linear tables

#define MAXSIZE 1000

typedef struct

{

Elemtype data;//Data

int cur;//cursor

}component,staticlinklist[maxsize];

650) this.width=650; "title=" 11.jpg "src=" http://s3.51cto.com/wyfs02/M02/57/27/wKioL1STft6jGONxAADH0V1kVIU083.jpg "alt=" Wkiol1stft6jgonxaadh0v1kviu083.jpg "/>

Note that the cursor for the last element should be 0

Static linked list initialization

Status initlist (staticlinklist space)

{

int i;

for (i =0;i<maxsize-1;i++)

Space[i].cur = i+1;

space[maxsize-1].cur=0;

return OK;

}

Insert data , such as insert B in

650) this.width=650; "title=" 11.jpg "src=" http://s3.51cto.com/wyfs02/M00/57/2A/wKiom1STfa-hCmmFAAFa-SaBzE8328.jpg "alt=" Wkiom1stfa-hcmmfaafa-sabze8328.jpg "/>

First, the subscript of the idle component is obtained

int malloc_sll (staticlinklist space)

{

int i = space[0].cur;

if (space[0].cur)//If it is an empty list

Space[0].cur = space[i].cur;//as a component spare

return i;

}

Sample code

Status Listinsert (staticlinklist l,int i,elemtype e)

{

int j,k,l;

K = max_size-1;

if (i<1 | | i>listlength (L) +1)

{

return ERROR;

}

j = malloc_sll (L);//Call the function written above,

if (j)

{

L[j].data = e;

for (l=1;l<=i-1;l++)

{

K = L[k].cur;

}

L[j].cur = L[k].cur;

L[k].cur = j;


return OK;

}

return ERROR;

}

Delete operation

650) this.width=650; "title=" 11.jpg "src=" http://s3.51cto.com/wyfs02/M02/57/28/wKioL1STg1TTn7fqAAHvw03C2fQ503.jpg "alt=" Wkiol1stg1ttn7fqaahvw03c2fq503.jpg "/>

Sample code

Status Listinsert (staticlinklist l,int i)

{

int j,k;

K = max_size-1;

if (i<1 | | i>listlength (L))

{

return ERROR;

}

j = malloc_sll (L);//Call the function written above,

for (j=1;j<=i-1;j++)

{

K = L[k].cur;

}

j = l[k].cur;

L[k].cur = L[j].cur;

return OK;

}


Data structures and algorithms-Learning note 7

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.