Static linked lists facilitate the use of linked list structures in high-level languages without pointer types, static linked lists are described by arrays, one component of an array represents a node, and a cursor (indicator cur) is used instead of a pointer to represent the relative position of the node in the array.
In addition, we treat the first and last elements of the array as special elements, and do not save the data.
The first element of the array, the cur of the element labeled 0, holds the subscript for the first node of the alternate list, while the last cur of the array holds the subscript for the first valid value, which is equivalent to the node in the single-linked list.
/* October 11, 2016 10:47:23 static linked list */#include <stdio.h> #define MAXSIZE 100typedef struct{int data; data field int cur; The cursor, for 0, is a non-pointing}component, the slinklist[maxsize];//function declares void Initspace_sl (slinklist space); int malloc_sl (slinklist space) void Free_sl (slinklist space, int k), void Listtraverse_sl (slinklist space); int listlength_sl (slinklist space); bool LISTINSERT_SL (slinklist space, int pos, int val), bool Listdelete_sl (slinklist space, int pos, int *val); int main (void) { int Val; Slinklist space; INITSPACE_SL (space); /* LISTINSERT_SL (space, 1, 99); LISTINSERT_SL (space, 2, 100); LISTINSERT_SL (Space, 3, 101); LISTINSERT_SL (Space, 4, 102); */LISTTRAVERSE_SL (space); LISTDELETE_SL (space, 2, &val); LISTTRAVERSE_SL (space); return 0;} void Initspace_sl (slinklist space)//Do not need pointers, because changing the value of a function parameter group will change the value of the real parameter group {int i; int Len; The number of the nodes used to store the int Val; The value used to store the node for (i = 0; i < MAXSIZE-1; ++i) {space[i].cur = i+1; }space[maxsize-1].cur = 0; The current static list is empty, and the last element cur is 0 printf ("Please enter the number of static list nodes: \ n"); printf ("len ="); scanf ("%d", &len); for (i = 1; I <= len; ++i) {printf ("Please enter the value of%d nodes:", I); scanf ("%d", &val); Space[i].data = val; } space[0].cur = len + 1; The cur of the first array element is used to hold the first node of the alternate list subscript space[len].cur = 0; Space[maxsize-1].cur = 1; The last array element holds the subscript return for the first valid node;} int MALLOC_SL (slinklist space)//If the alternate spatial list is not empty, the assigned node subscript is returned, otherwise the 0{int i is returned; i = space[0].cur; Because the first node of the array holds the subscript if (0! = space[0].cur) {space[0].cur = space[i].cur) of the first alternate list; Update to the next node of the alternate list} return i; void Free_sl (slinklist space, int k)//The idle node labeled K is recycled to the alternate list {space[k].cur = Space[0].cur; The first node of the original backup list is pointed to the K node space[0].cur = k; Return the K-node as the first node of the new alternate list;} void Listtraverse_sl (slinklist space)//traversal output {int i = space[maxsize-1].cur; while (0! = i) {printf ("%d\n", space[i].data); i = space[I].cur; } return; int LOCATEELEM_SL (slinklist space, int e)//finds the first element of E in the static linked list L, returns the bit order in L if found, otherwise returns 0{int i = space[maxsize-1].cur; while ((E! = space[i].data) && (0 = i)) {i = space[i].cur; } return i;} int LISTLENGTH_SL (slinklist space)//Returns the number of valid values in the static linked list space {int len = 0; int i = space[maxsize-1].cur; while (i) {len++; i = space[i].cur; } return len; BOOL Listinsert_sl (slinklist space, int pos, int val)//Insert a new element in the static linked list L POS position Val,pos starting from 1 {int i,j,k; if (POS < 1) | | (Pos > Listlength_sl (Space) +1)) return false without considering full condition; i = MALLOC_SL (space); Or the subscript of the idle element j = MAXSIZE-1; if (0! = i) {space[i].data = val; for (k = 0; k < pos-1; ++k) {j = space[j].cur; Locate the position before the pos element} space[i].cur = Space[j].cur; Space[j].cur = i; return true; } return false;} BOOL Listdelete_sl (slinklist space, int pos, int *vaL)//delete static linked list L in POS element, and return with Val, Pos starting from 1 {int I, j, K; if (POS < 1) | | (Pos > Listlength_sl (Space))) return false; K = MAXSIZE-1; for (i = 0; i < pos-1; ++i) {k = space[k].cur; Position before the first POS element is found} j = Space[k].cur; The element to be deleted *val = Space[j].data; Space[k].cur = Space[j].cur; FREE_SL (space, J); return true;}
Static linked list implemented in C language