Implementation of linked list

Source: Internet
Author: User

The appearance of the linked list solves the problem that arrays cannot be stored inProgramIt increases the flexibility of programming.

Linked lists can be divided into single-chain tables, cyclic linked lists, and two-way linked lists.

1. Single-chain table

 Head-> node1-> node2-> ...... noden.

Resolution:

(1) The head node of a single-chain table is the head node pointing to the first address of the linked list in the memory.

(2) the data type of each node in the linked list is the structure type and contains data members (the header node does not have data members) and a pointer pointing to the next node (no data structure pointing to the next node ).

(3) The access to each node by the linked list should start from the head of the linked list. The address of the subsequent node is provided by the current node. No matter which node you access in the table, you must start from the head of the linked list and search for it in sequence. Because no subsequent node exists at the end of the linked list, the pointer field is empty and the writing isN u L.

(4) node definition:

Struct Node

{

Int num;

Struct node * P;

};

In the definition of a linked list node, except for an integer member,MemberPIs a pointer pointing to the same type as the node.

In the data structure of the linked list node,A very special point is that the data type of the pointer field in the structure uses an undefined data type.This is inCYou can use the data structure defined later.

(5) Create an instance for a single-chain table: Create a positive integer-9 9 9And print the output.

 

C Language : Temporary self-use code # Include <stdlib. h> /* Header file containing malloc */
# Include <stdio. h>
/* L node Structure Definition of the linked list */
Struct Node
{
Int Num ;
Struct Node * Next ;
}
Main ()
{
Struct Node * Creat (); /* Function declaration */
Void Printf ();
Struct Node * Head ;
Head = Null ; /* Create an empty table */
Head = Creat ( Head ); /* Call the function to create a single-chain table */
Printf ( Head ); /* Print a single-chain table */
}

Struct Node * Creat ( Struct Node * Head )
{
Struct Node * P1 , * P2 ;
P1 = P2 = ( Struct Node * ) Malloc ( Sizeof ( Struct Node )); /* Apply for a new node */
Scanf ( "% D" , & P -> Num );; /* Enter the node value */
P1 -> Next = Null ; /* Set the pointer of the new node to null */
While ( P1 -> Num > 0 ) /* The value of the input node is greater than 0 */
{
If ( Head = Null )
{
Head = P1 ; /* Empty table, access header */
}
Else
{
P2 -> Next = P1 ;/* Non-empty table, connected to the end of the table */
}
P2 = P1 ;
P1 = ( Struct Node * ) Malloc ( Sizeof ( Struct Node ));/* Shen Please Next new node */
Scanf ( "% D" , & P1 -> Num ); /* Enter the node value */
}
Return Head ; /* Return the head pointer of the linked list */
}

Void Print ( Struct Node * Head ) /*InputReturns the value of each node in the linked list with the head as the header */
{
Struct Node * Temp ;
Temp = Head ; /* Get the head pointer of the linked list */
While ( Temp ! = Null ) /* As long as the table is not empty */
{
Printf ( "% 6D" , Temp -> Num ); /* Output the value of the linked list node */
Temp = Temp -> Next ; /* Track the growth of linked lists */
}
}

 

2. Two-way linked list

 

Read the full text

Category:View comments by default

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.