Data Structure linked list _ single-chain table interface definition, single-chain interface definition
A linked list is the most basic data structure. A linked list is composed of a group of elements in a specific order or link. It is useful when maintaining a set of data. This is similar to the commonly used array. However, in many cases, linked lists are more advantageous than arrays. In particular, the linked list has a higher efficiency when performing insert and delete operations. The linked list needs to dynamically open up the storage space, that is, the storage space is allocated when the program is running. Because the data size in many applications cannot be determined during compilation, this dynamic space allocation feature is also an advantage of linked lists.
Single-chain table Introduction
A single-chain table (referred to as a linked list) is composed of elements linked to each other through a pointer. Each element contains two parts: a data member and a pointer called next. By using this two-member structure, the next pointer of each element is set to the element following it (see figure 1 ). The next pointer of the last element is set to NULL, which simply represents the end of the linked list. The element at the beginning of the linked list is "Header", and the element before the end of the linked list is called the end.
You need to access an element in the linked list. Starting from the linked list header, you can use the next pointer to traverse from one element to another until you can find the desired element. For a single-chain table, you can only traverse the table in one direction: start to end, because each element does not maintain the link pointing to the previous element.
Conceptually, you can think of a linked list as a series of continuous elements. However, since these elements are dynamically allocated (calling malloc in C), it is important that,Remember that these elements are actually scattered in the memory space (see figure 2). The link between elements and elements is only used to ensure that all elements can be accessed. With this kind of thinking, we will see that we need to be very careful when maintaining the link information between elements. If we mistakenly lose a link, all elements in the future cannot be accessed starting from this position. "How weak are your weaknesses and How strong are your strengths" is very suitable for describing the characteristics of linked lists.
Single-chain table Interface Definition
List_init
Void list_init (List * list, void (* destroy) (void * data ));
No returned value
DescriptionInitialize the Linked list specified by the list.
This function must be called before the linked list performs other operations. When list_destroy is called, The destroy parameter provides a way to release dynamically allocated data. For example, if the linked list contains data dynamically allocated using malloc, destroy should be set to free when the linked list is destroyed. For structured data that contains several dynamically assigned members, destroy should be set as a user-defined destructor to release data by calling free for each dynamically assigned member and the struct itself. If the linked list contains data that should not be released, destroy should be set to null.
Complexity O (1)
List_destroy
Void list_destroy (List * list );
No returned value
DescriptionDestroys the Linked list specified by the list parameter.
No other operations can be performed after list_destroy is called unless list_init is called again. List_destroy removes all elements in the linked list. If the destroy parameter passed to list_init is not null, this function is called once when every element in the linked list is removed.
Complexity O (n), n represents the number of elements in the linked list
List_ins_next
Int list_ins_next (List * list, ListElmt * element, const void * data );
Return ValueIf the element is successfully inserted, 0 is returned; otherwise,-1 is returned.
DescriptionInsert a new element behind the element in the Linked list specified by list.
If element is set to NULL, the new element is inserted into the head of the linked list. The new element contains a pointer to data, so as long as the element is still in the linked list, the memory space referenced by data should be valid. It is the caller's responsibility to manage the buckets referenced by data.
Complexity O (1)
List_rem_next
Int list_rem_next (List * list, ListElmt * element, void ** data );
Return ValueIf the element is successfully removed, 0 is returned; otherwise,-1 is returned.
DescriptionRemove the element behind the element in the Linked list specified by list.
If the element is set to NULL, the elements in the linked list header are removed. After the call is returned, data points to the data stored in the removed element. The caller is responsible for managing the buckets referenced by data.
Complexity O (1)
List_size
Int list_size (const List * list );
Return ValueThe number of elements in the linked list.
DescriptionThis is a macro used to calculate the number of elements in the Linked list specified by the parameter list.
Complexity O (1)
List_head
ListElmt * list_head (const List * list );
Return ValuePointer to the Header element in the linked list.
DescriptionThis is a macro that returns a pointer to the Header element of the linked list specified by the list parameter.
Complexity O (1)
List_tail
ListElmt * list_tail (const List * list );
Return ValuePointer to the end element in the linked list.
DescriptionThis is a macro that returns the pointer to the tail element of the linked list specified by the parameter list.
Complexity O (1)
List_is_head
Int list_is_head (const ListElmt * element );
Return ValueIf the element specified by the element is the head node of the linked list, 1 is returned; otherwise,-1 is returned.
DescriptionThis is a macro used to determine whether the element specified by the element is the head node of the linked list.
Complexity O (1)
List_is_tail
Int list_is_tail (const ListElmt * element );
Return ValueIf the element specified by the element is the end node of the linked list, 1 is returned; otherwise-1 is returned.
DescriptionThis is a macro used to determine whether the element specified by the element is the end node of the linked list.
Complexity O (1)
List_data
Void * list_data (const ListElmt * element );
Return ValueThe data stored in the node.
DescriptionThis is a macro that returns the data stored in the linked list node element specified by the element.
Complexity O (1)
List_next
ListEmlt * list_next (const ListElmt * element );
Return ValueReturns the next node of the node specified by the element.
DescriptionThis is a macro that returns the next node of the node specified by the element in the linked list.
Complexity O (1)