Chain storage structure. the creation of the ordered storage structure of single-chain table 2 is essentially an array initialization. The storage space is continuous and its size and type are fixed. The storage space of Single-Chain tables is not continuous, it is a dynamic structure, and the size and location of the space occupied by it do not need to be pre-allocated. It can be generated in real time according to the system situation and actual needs. I. the whole of a single-chain table
Chain storage structure. the creation of the ordered storage structure of single-chain table 2 is essentially an array initialization. The storage space is continuous and its size and type are fixed. The storage space of Single-Chain tables is not continuous, it is a dynamic structure, and the size and location of the space occupied by it do not need to be pre-allocated. It can be generated in real time according to the system situation and actual needs. I. the whole of a single-chain table
Chain storage structure. Single-chain table 2
The creation of an ordered storage structure is essentially an array initialization. The storage space is continuous and its size and type are fixed. The storage space of a single-chain table is not continuous, it is a dynamic structure, and the size and location of the space occupied by it do not need to be pre-allocated. It can be generated in real time according to the system situation and actual needs.
I. Create a whole table for a single-chain tableThe process of creating a single-chain table is a process of dynamically generating a linked list, that is, starting from the initialization of an "empty table", creating each element node in sequence and inserting the linked list one by one.
1. algorithm ideas(1) declare a node p and the counter variable I; (2) Initialize an empty linked list L (3) point the pointer of the head node of the linked list L to NULL, create a single-chain table * L = (LinkList) malloc (sizeof (Node); (* L)-> next = NULL; (4) loop:. generate a new node and assign it to p; B. randomly generate a data field p-> data; c. insert p between the header node and the new node
2. Source Code Implementation
(1) Head insertion: always keep the new node at the first position/* Randomly generate n element values, and create a single-chain linear table L (header Insertion Method) */typedef struct Node * LinkList; // defines LinkListvoid CreateListHead (LinkList * L, int n) {LinkList p; int I; srand (time (0); // initialize the Random Seed/* 1. create a single-chain table */* L = (LinkList) malloc (sizeof (Node) for the lead Node; // Initialize an empty linked list L (* L)-> next = NULL; // point the pointer to the head node of linked list L to NULL/* 2. insert a new node cyclically into a single-chain table */for (I = 0; I Data = rand () % 100 + 1; // set the data domain of the new node p-> next = (* L)-> next; // set the pointer domain of the new node: set the successor node of the p node to the node (* L) pointed to before the header pointer-> next = p; // change the successor node of the header node to the new node p }}
(2) Plug-In: insert each new node behind the terminal node/* Randomly generate n element values, and create a single-chain linear table L (tail Insertion Method) */typedef struct Node * LinkList; // define LinkListvoid CreateListTail (LinkList * L, int n) {LinkList p, r; int I; srand (time (0); // initialize the random number seed/* 1. create a single-chain table with a lead Node and set the end Node */* L = (LinkList) malloc (sizeof (Node); // Initialize an empty linked list L, L indicates the entire single-chain table r = * L; // r indicates the end node/* 2. insert a new node cyclically into a single-chain table */for (I = 0; I Data = rand () % 100 + 1; // set the data domain r-> next = p of the new node; // point the pointer to the End Node of the table to the new node r = p; // define the current new node as the end terminal node} r-> next = NULL; // The End Node is p at this time, which is equivalent to p-> next = null ,, indicates the end of the current linked list} Note: note the relationship between L and r. L refers to the whole single-chain table, and r refers to the variable pointing to the End Node. r will change the node continuously with the cycle, L is a multi-node linked list as the cycle grows.
Note for Sublimation: 1. How to create a single-chain table with leading nodes? (1) Initialize an empty linked list L (2) Let the pointer of the head node of the linked list L point to the NULL source code implementation: LinkList * L; * L = (LinkList) malloc (sizeof (Node); (* L)-> next = NULL; 2. how can I insert a new node in the header insertion method? (1) generate a new node (2) set the data domain of the new node (3) set the pointer domain of the new node (4) use the new node as the source code of the successor node of the previous node: p = (LinkList) malloc (sizeof (Node); p-> data = e; // e is the data p-> next = (* L)-> next; // set the pointer field of the new node: Set the successor node of the p node to the node (* L) pointed to before the header pointer-> next = p; // change the successor node of the header node to the new node p3. how can I insert a new node through the tail difference method? (1) declare a node r and set it as the end node of linked list L (2) generate a new node p (3) set the data domain of the new node (4) point the pointer to the end terminal node of the table to the new node, and set the new node to the End Node (5). Set the pointer to NULL to indicate that the source code of the end of the current linked list is LinkList * L, p; * L = (LinkList) malloc (sizeof (Node); r = * L; p = (LinkList) malloc (sizeof (N [this article comes from the Internet of Hong (http://www.68idc.cn)] ode); p-> data = rand () % 100 + 1; r-> next = p; r = p; 2. delete an entire table from a single-linked table
1. algorithm ideas
(1) declare a node p and q; (2) Assign the first node of the single-chain table to p (3) loop:. assign the next node to q B. release p c. assign q to p
2. Source Code Implementation:/* Initialization condition: the Single Chain linear table L already exists. Operation Result: reset the single chain table L to an empty table */typedef struct Node * LinkList; // defines the LinkList
Typedef int Status;
Status ClearList (LinkList * L) {LinkList p, q; p = (* L)-> next; // assign the first node of the single-chain table to the node p while (p) {q = p-> next; // set the next node of p to q free (p); // release node p = q; // p points to the next node q} (* L)-> next = NULL; // Finally, the single-link header node pointer field is blank returm OK ;}
Iii. Advantages and Disadvantages of single-chain table structure and sequential Storage Structure
1. Storage Allocation Method(1) sequential storage structure stores data elements of a linear table in sequence using a storage unit of a link (2) A single-chain table uses a chain Storage Structure and uses any group of storage units to store elements of a linear table
2. Time Performance(1) Search: sequential storage structure O (1); single-chain table O (n) (2) Delete and insert: The sequential storage structure requires moving an average of half of the table length, the time is O (n). After a single-chain table points out a pointer at a certain position, the insertion and deletion time is only O (1)
3. Space Performance(1) The ordered storage structure requires pre-allocation of storage space, which is too large, wasted, and easily overflows. (2) The Single-linked table does not need to be allocated storage space, the number of elements is not limited.