Data Structure-single-chain table (Class C language description)

Source: Internet
Author: User

Single-chain table


1. Link Storage Method


A linear table stored by link is called a Linked List ).
The storage of the linked list is as follows:
① Use a group of arbitrary storage units to store the nodes of a linear table (this group of storage units can be continuous or discontinuous)
② The logical and physical order of nodes in the linked list is not necessarily the same. To correctly represent the logical relationship between nodes, you must store the address (or location) Information (referred to as the pointer) indicating the next node while storing the value of each node) or link ))
Note:
Chain storage is one of the most common storage methods. It can not only represent linear tables, but also represent various non-linear data structures.

2. node Structure of the linked list
── ┬ ── ┐
│ Data │ next │
── ┴ ── ┘
Data domain-data domain for storing node values
Next domain-pointer domain (Chain Domain) that stores the direct successor address (location) of the node)
Note:
① The chain table links n nodes of a linear table in the logical order through the chain fields of each node.
② Each node has only one Chain Domain Linked List called a Single Linked List ).

3. Expression of the header pointer and terminal node pointer Fields
The storage address of each node in a single-link table is stored in the next domain of the front node, and the Start Node has no front node. Therefore, a head pointer should be set to point to the Start Node.
Note:
The linked list is uniquely identified by the header pointer. A single-chain table can be named by the header pointer name.
[Example] The head pointer is a head linked list.
The terminal node has no successor, so the pointer field of the terminal node is NULL.

4. General Graphic Method of a single-chain table
Because we often focus only on the logic order between nodes and do not care about the actual location of each node, arrows can be used to represent pointers in the chain domain, linear tables (bat, cat, fat, hat, jat, A single-chain table of lat, mat) can be expressed as a form.


5. Single-chain table type description
Typedef char DataType; // assume that the data field type of the node is a character.
Typedef struct node {// node Type Definition
DataType data; // The data field of the node
Struct node * next; // pointer field of the node
} ListNode;
Typedef ListNode * LinkList;
ListNode * p;
LinkList head;
Note:
① LinkList and ListNode * are the same pointer type with different names (different names are used for more explicit concepts)
② The head variable of the LinkList type indicates that it is the header pointer of a single-chain table.
③ The pointer Variable p of the ListNode * type indicates that it is a pointer to a certain node.


6. pointer and node Variables

┌ ── ┬ ── ─ ┐
│ Pointer variable │ node variable │
├ ── ┼ ── ─ ┤
│ Definitions │ explicit definitions in the variable description section │ when the program is executed, the standard │
│ Function malloc generation │
├ ── ┼ ── ─ ┤
│ When the value is │ non-empty, it stores the content of a certain type of node │ actual content of each region of the node │
│ Address │
├ ── ┼ ── ─ ┤
│ Operation method │ access via pointer variable name │ generate, access, and release via pointer │
└ ── ┴ ── ─ ┘

① Generate standard functions for node Variables
P = (ListNode *) malloc (sizeof (ListNode ));
// The malloc function allocates space for a node variable of the ListNode type and puts its first address in the pointer Variable p.
② Release the Standard Functions of the node variable space
Free (p); // release the node variable space referred to by p.
③ Access to node Components
Use the name of the node variable * p to access the node component
Method 1: (* p). data and (* p). next
Method 2: p-> data and p-> next
④ Relationship between pointer Variable p and node variable * p
Value of pointer Variable p-Node Address
Node variable * p value-node content
(* P). data Value -- value of the data field of the node pointed by p pointer
(* P). next value -- * address of the p successor Node
* (* P). next) -- * p successor Node
Note:
① If the p value of the pointer variable is NULL, it does not point to any node. If * p is used to access the node, it means to access a non-existent variable, causing program errors.
② For a detailed explanation of the meaning and description of the pointer type, [refer to the relevant information in C Language ].


Related Article

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: info-contact@alibabacloud.com 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.