Linked List (1) -- create a basic one-way linked list

Source: Internet
Author: User
1. Node
The storage unit used to store a single data in the linked list.
A linked list must be composed of at least two parts: the data domain and the pointer domain. Generally, nodes are defined:
Struct Node
{
ELEM data; // The ELEM type refers to the basic data type.
Struct node * next;
}
Typedef struct node elemsn;
The above two steps are equivalent:
Typedef struct Node
{
ELEM data;
Struct node * next;
} Elemsn;
2. Use the pointer Variable P to represent node members.
* P. Data // error. The correct value is (* P). Data.
P-> data // correct
P-> next // correct
Note: (.), (->), and ([]) operators have the highest priority in C ).
3. Precursor node; the previous node of a node.
Successor node: The last node of a node.
Features of one-way linked list:
(1) There is only one node without a precursor, that is, the header node. The header node points to it through the head pointer.
(2) There is only one node with no successor, that is, the end node. The value of the next field at the end node is null.
(3) All nodes except the head and end nodes have only one precursor and only one successor.
4. Example

Example 1: the most basic implementation method for creating a one-way linked list.

# Include <stdio. h> # include <malloc. h> # definenull0typedef struct node {int data; struct node * Next; // elemsn} elemsn cannot be used; int main () {elemsn * head, * P; int I, MS; /* ms is used to store the number of nodes */int x; printf ("Please input node number:"); scanf ("% d", & MS ); head = P = (elemsn *) malloc (sizeof (elemsn); printf ("Please input data:"); scanf ("% d", & X ); head-> DATA = x; head-> next = NULL;/* create the first node because it is the first node, it is somewhat different from the subsequent node, so it is written separately */for (I = 0; I <MS-1; I ++)/* create the following MS-1 nodes */{P-> next = (elemsn *) malloc (sizeof (elemsn )); printf ("Please input data:"); scanf ("% d", & X); P-> next-> DATA = x; p-> next = NULL; P = p-> next;} For (P = head; P! = NULL; P = p-> next) {printf ("% d", p-> data);} printf ("\ n ");}

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.