Parity adjustment of ordered tables in linear tables and reverse local placement of Single-Chain tables

Source: Internet
Author: User

The parity adjustment of the Order table in a linear table refers to interchange the odd and even positions of the Order table in an optimal way, because there are many methods, for example, create a new sequence table to store the parity data separately. You can also find the parity data from the beginning to the end or the header, but these will increase the consumption of time and space. The Optimal Law is to find different types of parity numbers before and after the odd and the latter (or the former even later), exchange two numbers, and implement time complexity O (n ), space O (1) solution.

Void adjustsqlist (seqlist * l) {int I = 0, j = L-> last; int temp; while (I <j) {While (L-> ELEM [I] % 2! = 0) I ++;/* starts from the left half of the table. If it is an odd number, I adds 1, until an even number is found. */while (L-> ELEM [J] % 2 = 0) j --;/* checks the right half of the table. If it is an even number, then J minus 1 until the odd number is found */if (I <j) {temp = L-> ELEM [I]; l-> ELEM [I] = L-> ELEM [J]; L-> ELEM [J] = temp ;}}}

The reverse placement of a single-chain table is the replacement of elements before and after the linked list (the leading node). The Code is as follows:

Void reverselist (linklist L) {node * P, * q; P = L-> next; L-> next = NULL; while (P! = NULL) {q = p-> next;/* q pointer reserved p-> next value */p-> next = L-> next; l-> next = P;/* Insert P into the single-link table L with a nod */P = Q;/* P points to the next node to be inserted */}}

Additional: single-chain table for Binary addition of 1

Void binadd (linklist L)/* single-chain table to implement the binary addition 1 operation */{node * q, * r, * temp, * s; q = L-> next; R = L; while (Q! = NULL)/* Find the node whose last value is 0 */{If (Q-> DATA = 0) r = Q; q = Q-> next ;} if (R! = L) {r-> DATA = 1; /* assign the value of the last node with a value of 0 to 1 */} else/* node with a value of 0 not found */{temp = r-> next; S = (node *) malloc (sizeof (node);/* apply for a new node */S-> DATA = 1; /* value range assigned to 1 */S-> next = temp; r-> next = s;/* after the header node is inserted */r = s ;} R = r-> next; while (R! = NULL)/* assign the value range of all subsequent nodes to 0 */{r-> DATA = 0; r = r-> next ;}}

Raise Question 3 single-chain table for Binary addition 1 Operation

Parity adjustment of ordered tables in linear tables and reverse local placement of Single-Chain tables

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.