Using arrays instead of pointers to implement static linked lists

Source: Internet
Author: User

There is no concept of pointers before the C language, but at that time there are static list operations like data storage types. The advantage of a static list is that when the information stored in it is inserted and deleted, its time complexity is O (1), then what is used to implement this kind of storage operation when there is no pointer?

That is, using arrays instead of pointers, static lists are traversed by the address of the next node stored in the previous node when the data is traversed. And all the scattered space can be used, so that the more resource-saving (the array version of the static linked list at this point does not do well, because the array needs to set its size in advance, so in order to prevent the stored data out of range need to define the array is very large, which is a waste of resources). Every node of a static list has a structure pointer to the address of the next node, so the static list of the same pointer version needs two parts, part of the data part, and a cursor, and the array is the subscript cursor that is used to store the next node's array subscripts. This allows the data to be accessed directly from the subscript. At the same time the static linked list needs a head pointer to access the first node, and if the first node doesn't know where it is, then the subsequent operation is not possible, so the array here needs to have a structure that is specifically used to store the subscript of the first node. Of course, there is no special limit to this position, if you want to use the middle of a structure to store that is also possible can be more difficult, so many times with the structure of the last structure of the array to store the head node of the subscript, while as an array in the insertion of the time there is a problem, Where the newly inserted data needs to be stored, the static linked list using pointers does not need to be considered because the compiler will do the assignment for you, but when you use the static list of arrays, the insert operation requires you to assign it a specified array position yourself. Then there needs to be a place to store the subscript of the first empty array, so this array is first initialized before use, the first and last cursor with one structure is stored as 0 data is empty, the rest of the array's cursor stores a value 1 larger than itself, and the data is empty.

Creating a static linked list

 int  staticlistsetup (staticlist *space) {     int   I;    space[ 0 ].data=nul;  for  (I=0 ; i<maxsize;i++ if  (I==maxsize-1  ) {space[i].cur  =0  ;  else   {space[i].cur=i+1  ; }}  return   OK;}  

Static linked list traversal

void staticlistprint (staticlist *space) {     int  i;      I=space[999].cur;       while (space[i].data!=NUL)     {         printf ("%c", space[i].data);         I=space[i].cur;     }  

Using arrays instead of pointers to implement static linked lists

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