C and pointers (pointers on C)--chapter 12th: Using Structures and pointers

Source: Internet
Author: User

The 12th chapter uses structure and pointers


This chapter is linked list. First single linked list, then doubly linked list.



Summarize:
A single-linked list is a data structure that uses pointers to store values. Each node in the list includes a field that points to the next node in the list.


There is a separate root pointer pointing to the 1th node of the list.

A single-linked list can only traverse from One direction.
How to insert a single linked list: 1, the Link field of the new node must be set to point to its back node.

2. The link field of the previous node must point to this new node.


To prevent situations where a linked list might be inserted, in C, you can save a pointer to the link field that must be changed. Instead of saving a pointer to the previous node.


Each node in a doubly linked list consists of two link fields: one to the next node in the list. There is also a pointer to the previous node.
The doubly linked list has two root pointers, one pointing to the first node and one pointing to the last node. As a result, the traversal process can start at any one end. and can change direction during the Traverse.
In order to insert a new node into the doubly linked list. We have to change 4 pointers. The forward and Back link fields of the new node must be set, and the previous node's FWD and the bwd of the latter node are also changed to point to the new node.


Warning:
1, fall to the back of the tail of the list.
2, use the pointer should be extra careful. Because C does not provide a safety net for their use.


3. Refining a statement from an if statement may change the test result.


Programming Tips:
1, eliminate special circumstances make the code easier to maintain.
2, do not easily refine the statement, this will make your statement more difficult to maintain.




Programming Example: (This chapter will no longer exercise, about the data structure this piece will have a lot of code to train)

1. Single-linked list insert operation after refining

#include "stdlib.h" typedef struct NODE{STRUCT NODE *link;intvalue;} Node;int sll_int (Register node **LINKP, int new_value) {Register node *current;//points to the current node register node *new_node;//points to the insertion node/ Look for the correct insertion position, and sequentially visit the list until there is a value greater than or equal to the new value */while (current = current->link)! = NULL && Current->value < New_ Value) {LINKP =¤t->link;//Move LINKP point to the next node link}/* allocate new memory and coexist to the new node */new_node = (node *) malloc (sizeof (node)); if (new _node = = NULL) {return 0;} New_node->value = new_value;/* Insert new node */new_node->link = CURRENT;*LINKP = New_node;return 1;}

2. Double-linked list insert operation

#include "stdlib.h" typedef struct NODE{STRUCT node *fwd;struct node *bwd;int value;} Node;int Dll_insert (Node *rootp, int value) {/* Inserts a value into a doubly linked list. ROOTP is a pointer to the root node value is the new value that is inserted in the return value: Assume that a list already exists. Returns 0 assuming that the memory is insufficient to insert, return-1, successfully returned 1;*/node *this_node; Node *next_node; Node *new_node;for (this_node = ROOTP; Next_node! = NULL; this_node = Next_node) {if (Next_node->value = = value) return 0;if (Next_node->value < value) Break;next_node = NEXT_NODE-&GT;FWD;} /* Request memory space for new node */new_node = (node *) malloc (sizeof (node)), if (New_node = = NULL) Return-1;new_node->value = value;/* Insert Section The dot if is not at the end of the list and then is not at the beginning of the list or at the beginning of the linked list else at the end of the linked list, or at the beginning of the linked list (empty list) */if (NEXT_NODE-&GT;FWD! = null) {/* Not in the chain footer Part */if (This_node! = ROOTP) {/* Not in the head of the list */this_node->fwd = NEW_NODE;NEXT_NODE-&GT;BWD = NEW_NODE;NEW_NODE-&GT;BWD = This _NODE;NEW_NODE-&GT;FWD = Next_node;} else{/* in the head of the list */rootp->fwd = NEW_NODE;NEXT_NODE-&GT;BWD = NEW_NODE;NEW_NODE-&GT;BWD = ROOTP;NEW_NODE-&GT;FWD = Next_ node;}} else{/* at the tail of the list */if (THIS_NODE-&GT;BWD! = ROOTP) {/* Not at the head of the list */new_node->fwd = NULL;NEW_NODE-&GT;BWD = THIS_NODE;THIS_NODE-&GT;FWD = NE W_NODE;ROOTP-&GT;BWD = New_node;} else{/* in the head of the list */new_node->fwd = NULL;NEW_NODE-&GT;BWD = NULL;ROOTP-&GT;BWD = NEW_NODE;ROOTP-&GT;FWD = New_node;}}}


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

C and pointers (pointers on C)--chapter 12th: Using Structures and pointers

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.