Data structure-single-linked list (class C language description narrative)

Source: Internet
Author: User

single Linked list

1. Link Storage method

The linear table that is stored by link is referred to as the linked list (Linked list).
The detailed storage of the linked list is expressed as:
① uses a random set of storage units to hold the nodes of a linear table, which can be contiguous. can also be discontinuous)
The logical and physical order of the nodes in a ② list is not necessarily the same.

To correctly represent the logical relationship between nodes, at the same time that each node value is stored, you must also store the address (or position) information (called a pointer (pointer) or chain) that indicates its successor.
Chained storage is one of the most frequently used storage methods, and it is not only used to represent linear tables. And can be used to represent a variety of non-linear data structures.

2. Node structure of linked list
Data domain-------the field of the node value
Next field-The Pointer field (chain field) that holds the direct successor address (location) of the node
The ① linked list links The n nodes of a linear table to their logical order by the chain field of each node.
② Each node has only one linked list of links that is called a single list (Linked list).

3. Representation of head pointer and end node pointer field
The storage address of each node in a single linked list is stored in its forward node next field. The beginning of the knot is unprecedented. Therefore, the head pointer head should be set to point to the start node.
The linked list is uniquely determined by the headers pointer. A single-linked list can be named with the name of the head pointer.
"Example" a linked list of head pointers named head is called the table head.
The terminal node has no successor, so the pointer field of the terminal node is empty, i.e. null.

4, single-linked list of the general graphical method
Because we often focus only on the logical order between nodes. Do not care about the actual position of each node, can use arrows to represent the pointer in the Chain field, Linear table (Bat,cat. Fat,hat. Jat,lat,mat) can be expressed in the form of a single linked list.

5. Single-linked list type descriptive narration
typedef char DataType; If the data field type of the node is a character
typedef struct node{//node type definition
DataType data; Data fields for nodes
The pointer field of a struct node *next;//node
typedef listnode *linklist;
ListNode *p;
Linklist head;
①linklist and ListNode * are different names of the same pointer type (the naming difference is to be conceptually more clear)
The pointer variable head of the ②linklist type indicates that it is the head pointer of a single-linked list
③listnode * Type pointer variable p indicates that it is a pointer to a node

6. Pointer variables and node variables

││ pointer Variable │ Node Variable │
│ Definition │ In the variable description section explicitly defined │ when the program runs, through the standard │
│││ function malloc Generation │
│ Value │ non-empty, store a certain type of node │ actual storage node Domain content │
││ 's address ││
│ How to operate │ Access by pointer variable name │ generate, access, and release with pointers │

① standard function for generating node variables
p= (ListNode *) malloc (sizeof (ListNode));
The function malloc allocates a space for a node variable of type ListNode and puts its first address into the pointer variable p
② standard function for releasing node variable space
Free (p). Releasing the node variable space referred to by P
Interview of ③ node component
*p Access to node components using the name of the node variable
Method One: (*p). Data and (*P). Next
Method Two: P-﹥data and P-﹥next
The relationship between ④ pointer variable p and node variable *p
Value of pointer variable P--node address
The value of node variable *p--node content
(*p). Data value--p The value of the data field of the node to which the pointer refers
(*p). Next value--*p The address of the successor node
* ((*P). Next)--*p successor node
① If the value of the pointer variable p is null (NULL), it does not point to whatever node it is. At this point, access to the node via *p means visiting a nonexistent variable, which can cause a program error.
② about the meaning and description of the pointer type, "refer to the relevant information of C language".

Data structure-single-linked list (class C language description narrative)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.