Single-chain table definition
A single-chain table is a chained data structure that stores data elements in a linear table in any storage unit with a group of addresses.
Each element item in a one-way linked list stores the memory address of the next item. When the first element is found, the third element can be found through the address value of the second element stored in this element, and then the fourth, fifth, and sixth elements can be found.
Figure 1 shows the structure of a one-way linked list. In the figure, the Item1 element stores the element address value of item2, and item2 stores the Element Memory Address value of item3. So the whole single-chain table looks like a chain.
Basic operations on a single-linked table
How to operate a single-chain table? We only perform basic operations here.
1. Add elements.There are two ways to add elements: Header insertion and tail insertion.
Header Insertion Method: The left side of the linked list is the head of the linked list, and the right side is the tail of the linked list. The header is fixed on the right side. Each new element is added to the left side of the header.
Plug-in method:The left side of the linked list is the head of the linked list, and the right side is the tail of the linked list. The tail plug method is to fix the left side, and each addition is at the right end of the linked list.
2. query elements.To query related items through Element Information, You need to traverse from the header to the end.
3. delete an element.After deleting an element, you need to direct the value of the next element stored in the item on the left of the element to the value on the right of the deleted element.
Single-chain table structure definition
We define a struct where username and age are used to store the user name and age respectively, and next is used to store the memory address of the next item element.
/*** Define the data structure of a single-chain table */typedef struct single_cmd_list_s {char * username; unsigned int age; struct single_cmd_list_s * Next;} single_cmd_list;
Define a list pointer to store a single-chain table.
/*** A pointer to store a single-chain table */single_linked_list * List;
Define three methods:
/*** Insert a record */void insert (char * username, unsigned int age);/*** get a record */single_1__list * Get (char * username ); /*** delete an element using username */void Delete (char * username );
Implementation
1. Add operation. We use the end plug-in method.
void insert(char *username, unsigned int age) {single_linked_list *temp = (single_linked_list *) malloc(sizeof(single_linked_list));if (temp != NULL ) {temp->username = username;temp->age = age;temp->next = NULL;if (list == NULL ) {list = temp;} else {single_linked_list *temp_list = list;while (temp_list->next != NULL ) {temp_list = temp_list->next;}temp_list->next = temp;}}}
2. query operations.
single_linked_list *get(char *username) {if (list == NULL ) {return NULL ;}single_linked_list *temp = NULL;single_linked_list *temp_list = list;while (temp_list->next != NULL ) {if (strcmp(temp_list->username, username) == 0) {temp = temp_list;break;} else {temp_list = temp_list->next;}}return temp;}
3. delete operation.
Void Delete (char * username) {If (list = NULL) {return;} single_cmd_list * temp_pre = List; // The previous element single_cmd_list * temp_next = NULL; // The next element single_resource_list * temp_current = List; // The current element while (temp_current-> next! = NULL) {If (strcmp (temp_current-> username, username) = 0) {temp_pre-> next = temp_next; break;} temp_pre = temp_current; temp_current = temp_current-> next; If (temp_current-> next! = NULL) {temp_next = temp_current-> next ;}}}
Complete example
Single_pai_list.h
# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <unistd. h> # include <stdlib. h> # include <sys/types. h> # include <sys/STAT. h> # include <fcntl. h> # include <string. h> # include <ctype. h>/*** define the data structure of a single-chain table */typedef struct single_cmd_list_s {char * username; unsigned int age; struct single_cmd_list_s * Next;} single_cmd_list; /*** a pointer to store a single-chain table */single_into_list * List;/*** insert a record */void insert (char * username, unsigned int age ); /*** get a record */single_cmd_list * Get (char * username);/*** delete an element using username */void Delete (char * username );
Single_pai_list.c
# Include "single_linked_list.h" Void insert (char * username, unsigned int age) {single_1__list * temp = (single_1__list *) malloc (sizeof (single_1__list); If (temp! = NULL) {temp-> username = username; temp-> age = age; temp-> next = NULL; If (list = NULL) {list = temp ;} else {single_cmd_list * temp_list = List; while (temp_list-> next! = NULL) {temp_list = temp_list-> next;} temp_list-> next = temp; }}} single_cmd_list * Get (char * username) {If (list = NULL) {return NULL;} single_cmd_list * temp = NULL; single_cmd_list * temp_list = List; while (temp_list-> next! = NULL) {If (strcmp (temp_list-> username, username) = 0) {temp = temp_list; break;} else {temp_list = temp_list-> next ;}} return temp;} void Delete (char * username) {If (list = NULL) {return;} single_cmd_list * temp_pre = List; // The previous element single_cmd_list * temp_next = NULL; // The next element single_resource_list * temp_current = List; // The current element while (temp_current-> next! = NULL) {If (strcmp (temp_current-> username, username) = 0) {temp_pre-> next = temp_next; break;} temp_pre = temp_current; temp_current = temp_current-> next; If (temp_current-> next! = NULL) {temp_next = temp_current-> next ;}}}
Algorithm-single-chain table