Data structure (1) linked list, Data Structure
Introduction
A linked list is a non-sequential storage structure on a physical storage unit. The logical sequence of data elements is achieved through the pointer links in the linked list.
A linked list consists of a series of nodes (each element in a linked list is called a node), which can be dynamically generated at runtime.
Each node consists of two parts: one is the data domain that stores data elements, and the other is the pointer domain that stores the next node address.
Compared with the sequential structure of a linear table, the operation is complex. Because it does not have to be stored in order, the chain table can reach O (1) complexity during insertion, which is much faster than the sequential table of another linear table, however, it takes O (n) Time to search for a node or access a node with a specific number. the time complexity of linear and sequential tables is O (logn) and O (1) respectively ).
The linked list structure can be used to overcome the disadvantages that the array linked list needs to know the data size in advance. The linked list structure can make full use of the computer memory space for flexible dynamic memory management. However, the linked list loses the advantage of random array reading. At the same time, the linked list has a large space overhead due to the addition of pointer fields of nodes.
The most obvious advantage of a linked list is that the regular array arrangement of associated projects may be different from the order of these data items in the memory or disk. Data Access is usually converted in different order. The linked list allows you to insert and remove nodes at any position on the table, but does not allow random access.
There are many different types of linked lists: one-way linked list, two-way linked list, and cyclic linked list.
Concept of one-way linked list: one-way linked list (single-chain table) is a type of linked list. Its link direction is one-way, and access to the linked list should start from the header through sequential reading; A linked list is a list constructed by pointers. It is also called a node list because the linked list is assembled by nodes. Each node has a pointer member variable that refers to the next node in the list;
A list consists of nodes. The head Pointer Points to the first node that becomes the header and ends with the last pointer pointing to NULL;
/** February 27, 2015 16:42:27 by: My love course ** // ** function: Create, insert, and print a single-chain table (SingleLinkList -- SLL) **/# include <stdio. h> # include <malloc. h> // 0. single-chain table (SLL) node typedef struct node {char data; // data field struct node * next; // pointer field} LNode; // 1. initialize a single-chain table (SLL) and return the head pointer LNode * InitSLL () {// open the head node LNode * head = NULL; // check whether the initialization is successful if (NULL! = (Head = (LNode *) malloc (sizeof (LNode) head-> next = NULL; return head;} // 2. insert into a single-chain table (SLL) node "header Insertion Method" // head --- linked list header pointer // data --- data to be inserted void InsertNode (LNode * head, char data) {LNode * pNode = NULL; // whether node development is successful if (NULL! = (PNode = (LNode *) malloc (sizeof (LNode) {pNode-> data = data; // Insert the node into the Linked List (SLL) head pNode-> next = head-> next; head-> next = pNode;} // 3. print a single-chain table (SLL) // head --- head pointer void DisPlaySLL (LNode * head) {LNode * h = head-> next; while (NULL! = H) {printf ("% c", h-> data); h = h-> next;} int main (void) {LNode * head = NULL; // 1. initialize a single-chain table (SLL) head = InitSLL (); // 2. insert node "header insertion" InsertNode (head, 'A'); InsertNode (head, 'B'); InsertNode (head, 'C'); InsertNode (head, 'C, 'D'); InsertNode (head, 'E'); // 3. print a single-chain table (SLL) DisPlaySLL (head); return 0 ;}Cyclic linked list
Concept: Circular linked list is another form of chain storage structure. It points the pointer field of the last node in the table to the header node, and the entire linked list forms a ring.
Here, the initialization part of the aforementioned one-way linked list is slightly modified to form a Circular Single-chain table.
// 1. initialize the circular linked list (CL) and return the head pointer LNode * InitCL () {// open the head node LNode * head = NULL; // check whether the initialization is successful if (NULL! = (Head = (LNode *) malloc (sizeof (LNode) {// mark the head node head with "#" here-> data = '#'; // link the beginning and end of the linked list ~~~ Head-> next = head;} return head ;}Bidirectional cyclic linked list
Concept: A two-way linked list, also known as a double-chain table, is a type of linked list. Each of its data nodes has two pointers pointing to the direct successor and direct precursor respectively. Therefore, starting from any node in the two-way linked list, you can easily access its precursor node and successor node. Generally, we construct a two-way circular linked list.
/** February 27, 2015 17:42:30 by: My love course ** // ** function: create a two-way linked list (TwoLinkList -- TLL) **/# include <stdio. h> # include <malloc. h> // 0. two-way linked list (TLL) node typedef struct node {char data; // data field struct node * prior; // front pointer struct node * next; // rear pointer} LNode; // 1. create a two-way linked list "two-way linked list of leading nodes" LNode * CreateTLL () {LNode * head = NULL, * L = NULL; LNode * R = NULL; // temporary node char ch; // create a header node head = (LNode *) malloc (sizeof (LNode); head-> prior = head; head-> next = head; L = head ;// Create a two-way linked list while ('\ n '! = (Ch = getchar () {R = (LNode *) malloc (sizeof (LNode); R-> data = ch; // Add a node "APPEND at the end" R-> prior = L; R-> next = head; // form a circular bidirectional linked list head-> prior = R; l-> next = R; L = R;} return head;} void DisPlayTLL (LNode * head) {LNode * L = head-> prior, * R = head-> next; // 1. print the while (head! = R) {printf ("% c", R-> data); R = R-> next;} printf ("\ n"); // 2. print the while (head! = L) {printf ("% c", L-> data); L = L-> prior;} int main (void) {LNode * head = NULL; // 1. create a two-way linked list (TLL) head = CreateTLL (); // 3. print two-way linked list (TLL) DisPlayTLL (head); return 0 ;}
(To be continued ......)
References:
1) Baidu encyclopedia, linked list, http://baike.baidu.com/view/549479.htm, February 27, 2015
2) Baidu encyclopedia, one-way linked list, http://baike.baidu.com/view/2073053.htm, February 27, 2015
3) Baidu encyclopedia, circular linked list, http://baike.baidu.com/view/178643.htm, February 27, 2015
4) Baidu encyclopedia, two-way linked list, http://baike.baidu.com/view/1627720.htm, February 27, 2015
5) Net guy, [data structure] Create and read a two-way linked list, http://www.cnblogs.com/ngnetboy/archive/2012/09/25/2701774.html, February 27, 2015