C Language Improvement--from structure and pointer to data structure linked list

Source: Internet
Author: User

By combining structures and pointers, you can create powerful data structures. This blog is only for the single linked list to see.

Link List Description:

Each node of a linked list is a struct with two elements: 1, a pointer to the next node, and the node data (value). Where a root pointer is required to point to the first node (root).

Insert Data:Initial code:

The idea of inserting is to determine the insertion position by comparing the current structure's data with the size of the data to be inserted (New_value). You first need to define two pointer variables, one pointing to the current structure, and one to the previous structure (previous) of the current structure. When you have a good position, you need to assign a new node and insert the node.

The code is as follows:

#include <stdlib.h> #include <stdio.h> #include "sll_node.h" #define FALSE 0#define TRUE  1int Sll_insert (Node *current, int new_value) {Node *previous; Node *new;/*** looks for the correct insertion location by sequentially accessing the linked list until it reaches its value greater than or equal to the new inserted node value. */while (Current->value < New_value) {previous = Current;current = Current->link;} /*** allocates memory for the new node and stores the new value in the new junction, and if the memory allocation fails, the * * function returns FALSE. */new = (node *) malloc (sizeof (node)), if (new = = NULL) return False;new->value = new_value;/*** Inserts the new node into the list and returns True. */new->link = Current;previous->link = New;return TRUE;}

Debug Upgrade:

However, there are the following problems: 1, in the process of judging the insertion position, to determine whether the last node has been accessed, if you do not make this judgment in the loop pointer assignment process because the current null pointer will cause indirect access failure. 2, if you need to change the root pointer when inserting a node in the first position of the list, you need to define a pointer to the root pointer (ROOTP) to manipulate the root node by pointers to the root pointer. Compare the previous code with the following code:

#include <stdlib.h> #include <stdio.h> #include "sll_node.h" #define FALSE 0#define TRUE  1int Sll_insert (Node **rootp, int new_value) {Node *current; Node *previous; Node *new;/*** gets a pointer to the first node. */current = *rootp;previous = null;/*** looks for the correct insertion position by sequentially accessing the linked list until it reaches its value greater than or equal to the new inserted node value. */while (Current! = NULL && Current->value < New_value) {previous = Current;current = Current->link;} /*** allocates memory for the new node and stores the new value in the new junction, and if the memory allocation fails, the * * function returns FALSE. */new = (node *) malloc (sizeof (node)), if (new = = NULL) return False;new->value = new_value;/*** Inserts the new node into the list and returns True. */new->link = current;if (previous = = NULL) *ROOTP = New;elseprevious->link = New;return TRUE;}

Optimize your upgrade:

It seems that after debugging upgrade has been very good, but in fact, can also be optimized upgrade, the idea of optimizing the upgrade is to exclude the starting position of the special situation, seize the commonality to operate. The common denominator of a list is that each node has a pointer to it. The pointer to the first node is the root pointer (root), and the pointer to the other node is the pointer Element (link) of the previous node. So here you define a pointer (LINKP), which can point to a field that refers to a pointer to a node that is down, initially pointing to the root pointer, which is a pointer to the first node. You also need a pointer to the current node. So we'll focus on the center from the node to the pointer to the next node. Compare the above code as follows:

#include <stdlib.h> #include <stdio.h> #include "sll_node.h" #define FALSE 0#define TRUE  1int Sll_insert (register Node **LINKP, int new_value) {Register node *current;register node *new;/*** looks for the correct insertion location by sequentially accessing the linked list until it reaches its value greater than or equal to the new inserted node value. */while (current = *LINKP) = NULL && Current->value < New_value) {LINKP =¤t->link;} /*** allocates memory for the new node and stores the new value in the new junction, and if the memory allocation fails, the * * function returns FALSE. */new = (node *) malloc (sizeof (node)), if (new = = NULL) return False;new->value = new_value;/*** Inserts the new node into the list and returns True. */new->link = CURRENT;*LINKP = New;return TRUE;}

Summary of the understanding:

The second step from the optimization of these three steps is the process of the first step of the error processing, the third step is to optimize the operation of the second step, the idea of optimization from the look of different operations to summarize the commonality, that is, the abstraction, to find common after you can use an operation to complete a number of seemingly different tasks, in fact, we have been Not only from the knowledge to find commonalities, from life can also find commonalities, from the phenomenon of life is abstract out of the law, in fact, the law can also do further abstraction, step by step upward abstraction can find some of the most fundamental laws and truths, Then we can focus on these laws and the truth to pay attention to our daily specific behavior and actions, so as to help us deal with some of the problems.

C Language Improvement--from structure and pointer to data structure linked list

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.