Pig's Data Structure Study Notes (4)
Static linked list of linear table
-- Reprinted with the source: coder-pig
This chapter introduces:
In section 2 and section 3, we learned the linear table and single-chain table in the sequence table. The linear table is somewhat similar
The array we learned earlier, while the single-chain table uses the most pointer. Here is a simple question,
If there were no pointers in the past, how would the predecessors implement a single-chain table first? Think about it!
No pointer, so what should I use instead? The predecessors came up with a clever way to use subscript + cursor.
To achieve the effect of a single-chain table! That is what we will talk about today-static linked list!
Of course, you can skip this chapter directly, because there is no need to use a static linked list with a single-chain table!
We are learning some ways of thinking. When conditions limit you, you are not allowed to use this method;
So how do you solve the same problem in another way? There is no harm in learning more things!
Let's start with this chapter!
This chapter's learning roadmap:
Body:
What is a static linked list?
The so-called static linked list uses three attributes to represent a node,
Array Subscript: identifies each node
Data: stores the data of the node.
Cursor: similar to a pointer in a linked list, pointing to the subscript of the next Element
Initialize static linked list:
Process:
Step 1: Use the for loop to assign values to each element in the array
Step 2: Set the cursor of the last node to 0
Step 3: Return OK
The sample code is as follows:
// Initialize the static linked list, which is similar to the initialization of the array: Status initlist (staticlinklist space) {int I; for (I = 0; I <maxsize-1; I ++) {// each cursor points to the next subscript, and the second cursor points to the second subscript Node space [I]. cursor = I + 1; // set the cursor of the last node to 0 space [maxsize-1]. cursor = 0; Return OK ;}}
Insert elements to a static linked list
Simple Example:
For example, if there is a static linked list s, the stored data is: {a, B, c, d, e, f}, maxsize = 20, and now you want to insert it after the third element.
An element. The process is as follows:
(1) define a method to obtain a slave node, that is, open the seventh node and return the subscript return 7;
② Assign s [7]. cursor = e to this node;
③ Modify the cursor of the third element, s [9]. cursor = s [3]. cursor; s [3]. cursor = 9;
The sample code is as follows:
// Obtain the node subscript int get_free (staticlinklist space) {int I = space [0]. cursor; // set the next node as the slave node if (space [0]. cursor) space [0] = space [I]. cursor; // return the subscript return I of the new node ;}
// Insert the data element status list_insert (staticlinklist L, int I, elemtype E) to position I of the static linked list {Int J, K, L; k = maxsize-1; // The last element of the array // determine whether the insertion position is legal if (I <1 | I> listlength (l) + 1) {return error ;} // obtain a blank node subscript J = get_free (l); If (j) {// place e in the data field of the blank node l [J]. data = E; For (L = 1; L <= I-1; l ++) {// obtain the subscript of the I-1 element. // K = L [k-1] is not used directly. cursor Is because the element may not be adjacent! K = L [K]. cursor;} l [J]. cursor = L [K]. cursor; L [K]. cursor = J; Return OK;} return error ;}
Delete elements in a static linked list:
The deletion logic is a little simpler,
For example, if we delete 5th elements, we only needS [4]. curosr = s [5]. cursor
Release the idle node, that is
The sample code is as follows:
// Delete a node status list_delete (staticlinklist L, int I) {// determine whether the deletion location is legal if (I <1 | I> listlength (L) return error; // obtain the last node because its cursor points to the first valid node int K = max_size-1; for (Int J = 1; j <I; ++ J) {// obtain the subscript K = L [k] for the I-1 element. cursor;} // obtain the subscript J = L [k] For element I. cur; // assign the cursor of the deleted node to the previous node l [K]. cur = L [J]. cur; // release to the I-th element. The subscript is jfree_node (L, J); Return OK ;}
/* Reclaim the idle node whose subscript is K to the standby node */void free_node (staticlinklist space, int K) {// store the subscript of the first node of the standby linked list to space [k] In the cursor of L [K]. cursor = space [0]. cur; // The element whose subscript is k is called the first idle Node space [0]. cursor = K ;}
Obtain the number of elements in the static linked list:
This is to traverse the table once. When the obtained value is 0, it indicates that the idle node has been reached, and the returned length is now!
The sample code is as follows:
/* Obtain the number of data elements in L */INT list_length (staticlinklist L) {Int J = 0; int I = L [Max-1]. cursor; while (I) {I = L [I]. cursor; j ++;} return J ;}
Summary of static linked list features:
It is known from the name that this is a linked list, and it must be similar to a single-chain table, such as inserting and deleting elements that do not need to be moved
You only need to modify the cursor. In addition, the random access feature of the sequential storage structure is also lost, and the table length caused by the array is not solved.
Difficult to determine! In the end, it is to provide a linked list function for programming languages without pointers.
It must be a single-chain table, but this method is very clever and worth learning!
Pig's Data Structure Study Notes (4)