Create a single-chain table for header insertion and tail insertion.

Source: Internet
Author: User

Create a single-chain table for header insertion and tail insertion.

/*
Objective: to create a single-chain table, such as tail insertion, header insertion, and traversal table
*/


# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>

// Define a struct to represent a node, including the pointer field and Data Field

Struct node
{
Int data; // data domain

Struct node * next; // pointer field, pointing to the node pointer, used to connect two nodes
};

// Define a function to create a node and allocate heap memory to the node. Because the heap memory contains random values, the memory is cleared after creation.
// The Return Value of the function must be of the struct node * type. Because a node is created and you need to operate on the node, You need to obtain the address information of the node.

Struct node * create_node (int data) // int data: transmits a value for the data field of the node.
{
Struct node * p = (struct node *) malloc (sizeof (struct node); // create a node and return its address value and save it to p.

If (NULL = p) // determines whether the node is successfully created.
{
Printf ("malloc erro ");
Return NULL;
}

Bzero (p, sizeof (struct node); // call the function in # include <string. h> to clear the applied dirty memory

// After the node is successfully created and cleared, the node is filled
P-> data = data;
P-> next = NULL;

Return p; // return the node address value
}


// Implementation of the end insertion function: Find the last node through the header pointer, and then associate the new node with the last node. The insertion is complete;
Void insert_tail (struct node * pHeader, struct node * new)
{
// Receives the value of the header pointer, starts the traversal table, and finds the End Node
Struct node * p = pHeader;

While (NULL! = P-> next) // you can find the end node of the traversal link table.
{

P = p-> next;

}

// The traversal ends. Find the last node is p.
// Place the address value of the new node into the pointer field of the last Node

P-> next = new; // The end is inserted successfully.

New-> next = NULL;
}

 

// Implementation of the header insertion function: first place the address value of the first node into the pointer field of the new node, and then put the address of the new node into the pointer field of the first node.
Void insert_head (struct node * hNode, struct node * new)
{
If (NULL = hNode-> next)
{
HNode-> next = new;
New-> next = NULL;
}
Else
{
New-> next = hNode-> next;
HNode-> next = new;
}

}

 

 

 

 

 

Int main (void)
{
Struct node * pHeader = create_node (0 );
Insert_head (pHeader, create_node (1 ));
Insert_head (pHeader, create_node (2 ));
Insert_head (pHeader, create_node (3 ));
Insert_head (pHeader, create_node (4 ));

Printf ("********************************** header insertion ** * ***************************** \ n ");
Printf ("node 1: % d. \ n", pHeader-> next-> data );
Printf ("node 2: % d. \ n", pHeader-> next-> data );
Printf ("node 3: % d. \ n", pHeader-> next-> data );
Printf ("node 4: % d. \ n", pHeader-> next-> data );


/*************************************** **************************************** ****************/

Struct node * pH = create_node (0 );
Insert_tail (pH, create_node (1 ));
Insert_tail (pH, create_node (2 ));
Insert_tail (pH, create_node (3 ));
Insert_tail (pH, create_node (4 ));

Printf ("********************************** tail insert ** * ***************************** \ n ");
Printf ("node 1: % d. \ n", pH-> next-> data );
Printf ("node 2: % d. \ n", pH-> next-> data );
Printf ("node 3: % d. \ n", pH-> next-> data );
Printf ("node 4: % d. \ n", pH-> next-> data );

Return 0;
}

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.