Table
A table (list) is a common data structure. Mathematically, a table is an ordered set of elements. In C-language memory, tables are stored as scattered nodes (node). Each node contains an element and a pointer to the next (or previous) element. As shown in the following:
Table: Orange storage data, blue storage pointer
There are four nodes in the table in the figure. The first node is the head node, which is not used to store elements and is used only to indicate the start of the table. The head node allows us to easily insert or delete the first element of a table. The entire table contains three elements (5, 2, 15). Each node has a pointer to the next node. The pointer to the last node is null, and we use "ground" to illustrate the pointer.
The function of a table is similar to an array, an array is an ordered set of elements, but the array is contiguous in memory, and the memory occupied by each node of the table can be discrete. In the array, we look for a numbered element by skipping the fixed memory length. But in the table, we have to traverse the query element along the long chain that the pointer is linked to. In addition, the array has a fixed size, the table can be inserted or deleted according to the operation of the node, dynamic change size. When a table is inserted into a node, it needs to open up memory space from the heap in the process space to store the node. Deleting a node can return the memory occupied by the node to the process space.
Delete node, free memory
Inserting nodes, malloc opens up memory
There are many variants of the table. In the table above, the pointer points to the front-back, called the Unidirectional list (linked lists). There is also a doubly linked list (double-linked list), that is, each node adds a pointer to the previous element. As well as the circular list (tabular list), the pointer to the last element is not NULL, but instead points to the head node. Different types of linked lists have different scenarios.
Doubly linked list
Circular link List
Bidirectional loop Linked List
C Implementation of unidirectional linked list
A data structure is implemented in two ways: 1. The memory expression of data structure; 2. Define the operation on the data structure. Here we implement the simplest one-way linked list. The operations supported by the table are flexible and we define some of the most common operations here. Each operation is written as a function.
/* by Vamei */
#include <stdio.h> #include <stdlib.h>
typedef struct node *list;
typedef struct node *position;
/* node, */struct nodes {int element; Position next;};/ *
* Operations (stereotype)
* Operation
*/
List init_list (void); void delete_list (list);
int Is_null (LIST);
void Insert_node (position, int); void Delete_node (LIST, position);
Position Find_last (list);p osition find_value (list, int);p osition find_previous (list, position);
void Print_list (list);
/* For testing Purpose */void main () {LIST L; Position NP; int i; /* elements to is put into the list */int a[] = {1, 3, 5, 7, 9}; /* Initiate a list */L = Init_list (); Print_list (L); /* INSERT nodes. Insert just after head node */for (i=4; i>=0; i--) {Insert_node (L, a[i]); } print_list (L); /* Delete First node with value 5 */np = Find_value (L, 5); Delete_node (L, NP); Print_list (L); /* Delete list */delete_list (L); /* Initiate a list */L = Init_list (); Print_list (L); /* INSERT nodes. Insert just after head node */for (i=4; i>=0; i--) {Insert_node (L, a[i]); } print_list (L); /* Delete list */delete_list (L);} /* * Traverse the list and print each element
* Print table */void print_list (list L) {position NP; if (Is_null (L)) {printf ("Empty list\n\n"); Return } NP = L; while (np->next! = NULL) {NP = np->next; printf ("%p:%d \ n", NP, np->element); } printf ("\ n");} /* * Initialize a linked list. This list has a head node * head node doesn ' t store valid element value
* CREATE TABLE */list init_list (void) {LIST L; L = (position) malloc (sizeof (struct node)); L->next = NULL; return L;} /* * Delete all nodes in a list
* Delete Table */void delete_list (list L) {position NP, next; NP = L; do {next = np->next; Free (NP); NP = Next; } while (next! = NULL); }/* * If a list only have head node, then the list is null.
* Determine if the table is empty */int is_null (LIST L) {return ((L->next) ==null);} /* * Insert a node after position NP
* After the NP node, insert the node */void insert_node (position np, int value) {position nodeaddr; Nodeaddr = (position) malloc (sizeof (struct node)); Nodeaddr->element = value; Nodeaddr->next = np->next; Np->next = nodeaddr; }/* * Delete node at position NP
* Delete NP node */void delete_node (LIST L, position NP) {position previous, next; Next = np->next; Previous = Find_previous (L, NP); if (previous! = NULL) {previous->next = next; Free (NP); } else {printf ("ERROR:NP not in the list"); }}/*
* Find the last node of the list
* Find the last node of the table
*/position find_last (LIST L) {position NP; NP = L; while (np->next! = NULL) {NP = np->next; } return NP;} /* * This function serves for 2 purposes: * 1. Find Previous node * 2. Return NULL if the position isn ' t in the list
* Look for nodes in front of the Nptarget node */position find_previous (LIST L, position Nptarget) {position NP; NP = L; while (np->next! = NULL) {if (Np->next = = Nptarget) return NP; NP = np->next; } return NULL; /* Find the first node with specific value
* Query */position Find_value (LIST L, int value) {position NP; NP = L; while (np->next! = NULL) {NP = np->next; if (np->element = = value) return NP; } return NULL;
In the main () function, we initialize the table and then insert (1, 3, 5, 7, 9). Delete Element 5 again. As you can see, the nodes are scattered across the memory. Deleting a node operation does not affect where the other nodes are stored.
We then delete the table and recreate the table. As you can see, this time the table occupies memory in a different location than the first time.
The following is the result of the operation of the main () function.
Empty List
0x154d0b0:1
0x154d090:3
0x154d070:5
0x154d050:7
0x154d030:9
0x154d0b0:1
0x154d090:3
0x154d050:7
0x154d030:9
Empty List
0x154d070:1
0x154d010:3
0x154d0b0:5
0x154d090:7
0x154d050:9
Summarize
Table: Ordered nodes with discrete distributions in memory
Insert, delete node
Table (list)