C and pointer (pointers on C) -- Chapter 12th: Using structures and pointers

Source: Internet
Author: User

C and pointer (pointers on C) -- Chapter 12th: Using structures and pointers

Chapter 4 Structure and pointer


This chapter is the linked list. A single-chain table first, followed by a two-way linked list.



Summary:
A single-chain table is a data structure that uses pointers to store values. Each node in the linked list contains a field to point to the next node in the linked list.
There is an independent root pointer pointing to the 1st nodes of the linked list. A single-chain table can only be traversed in one direction.
How to insert a single-chain table: 1. The link field of the new node must be set to the node that points to it. 2. The link field of the previous node must point to the new node.
To prevent insertion of the starting position of the linked list, you can save a pointer to the link field that must be modified in C instead of a pointer to the forward node.


Each node in the double-link table contains two link fields: one pointing to the next node of the linked list, and the other pointing to the previous node.
A double-linked table has two root pointers, one pointing to the first node and the other pointing to the last node. Therefore, the traversal process can start from any end and change the direction during the traversal process.
To insert a new node into a double-link table, we must modify four pointers. The forward and backward link fields of the new node must be set. The fwd of the previous node and the bwd of the next node must also be modified to point to the new node.


Warning:
1. It falls behind the end of the linked list.
2. Exercise caution when using pointers because C does not provide a security net for their use.
3. refining a statement from an if statement may change the test result.


Programming tips:
1. Eliminate special circumstances to make the code easier to maintain.
2. Do not extract statements easily. This will make your statements more difficult to maintain.


Programming example: (this chapter no longer gets exercises, and there will be a lot of code for training on the data structure)

1. Insert the extracted single-chain table

# Include "stdlib. h "typedef struct NODE {struct NODE * link; intvalue;} Node; int sll_int (register Node ** linkp, int new_value) {register Node * current; // point to the current Node register Node * new_node; // point to the inserted Node/*** to find the correct Insertion Location and access the linked list in sequence, 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 the linkp to the link of the next Node}/* allocate new memory, coexist to the new NODE */new_node = (NODE *) malloc (sizeof (NODE); if (new_node = NULL) {return 0;} new_node-> value = new_value; /* Insert a new node */new_node-> link = current; * linkp = new_node; return 1 ;}

2. double-stranded table insertion

# Include "stdlib. h "typedef struct NODE {struct NODE * fwd; struct NODE * bwd; int value;} Node; int dll_insert (Node * rootp, int value) {/* Insert a value to a two-way linked list. The value indicated by the root node is the new value returned by the insert operation. If the value already exists in the linked list, 0 is returned. If the memory is insufficient and the insertion fails,-1 is returned. If yes, 1 is returned. */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-> fwd ;} /* apply for memory space for the new Node */new_node = (Node *) malloc (sizeof (Node); if (new_node = NULL) return-1; new_node-> value = value; /* if the inserted node is not at the end of the linked list, then is not at the starting position of the linked list or else is at the starting position of the linked list at the end of the linked list, then is not at the starting position of the linked list or is at the starting position of the linked list) */if (next_node-> fwd! = NULL) {/* not at the end of the linked list */if (this_node! = Rootp) {/* not in the head of the linked list */this_node-> fwd = new_node; next_node-> bwd = new_node; new_node-> bwd = this_node; new_node-> fwd = next_node;} else {/* in the head of the linked list */rootp-> fwd = new_node; next_node-> bwd = new_node; new_node-> bwd = rootp; new_node-> fwd = next_node;} else {/* at the end of the linked list */if (this_node-> bwd! = Rootp) {/* not in the head of the linked list */new_node-> fwd = NULL; new_node-> bwd = this_node; this_node-> fwd = new_node; rootp-> bwd = new_node;} else {/* in the head of the linked list */new_node-> fwd = NULL; new_node-> bwd = NULL; rootp-> bwd = new_node; rootp-> fwd = new_node ;}}}


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.