Data Structure 2.6 single-chain table application example, data structure 2.6

Source: Internet
Author: User

Data Structure 2.6 single-chain table application example, data structure 2.6

1 // inverted single-chain table (Head insertion method, time complexity O (n) 2/* algorithm idea: 3. Extract each node in the original linked list in sequence, insert it into the original linked list as the first node each time. Because of the header insertion method, the insertion sequence is the opposite to that of node 4. Therefore, the inverted operation can be completed. 5 */6 void reverseList (LinkList h) // reverse: Back, reverse, reverse 7 {8 LNode * p; 9 p = h-> next; // * p points to the First Data Element Node for 10 h-> next = NULL; // empty table 11 while (p) 12 {13 q = p; 14 p = p-> next; 15 q-> next = h-> next; // Insert the first node to the end of the current node (that is, the current node is inserted to the end of the header node) 16 h-> next = q; 17} 18} 19 20 // Delete duplicate nodes (nodes with the same data domain value in a single-chain table are called duplicate nodes, time complexity O (n * n) 21/* algorithm ideas: 22. point p to the first data node from its direct successor node until the end of the linked list. Locate the node with the same value and delete it. p points to the next node of 23, repeat the preceding operation. So on. When p points to the End Node of the table The algorithm ends. 24 */25 void del_LinkList (LinkList h) 26 {27 LNode * p, * q, * r; 28 p = L-> next; // p points to the first node 29 if (p = NULL) return 0; 30 while (q-> next) 31 {32 q = p; 33 while (q-> next) // start searching for duplicate nodes 34 {35 if (q-> next-> data = p-> data) 36 {37 r = q-> next; // r points to the repeated node 38 q-> next = r-> next; 39 free (r); // Delete r 40} 41 else 42 q = q-> next; 43} 44 p = p-> next; // p points to the next node 45} 46} 47 48 // merge a single-chain table (header insertion method, time complexity O (m + n), merge: me Rge) 49 // The elements in two single-chain tables A and B are incremental and ordered. Now, A and B are merged into an ordered single-chain table C according to the non-incrementing element values (the same value is allowed, you do not need to apply for a node again. 50/* algorithm idea: 51 uses the incremental and ordered features of A and B to extract the current node in sequence for comparison, remove the smaller value, and insert the header of C; because the 52-header insertion method is used, the minimum node that is first found will be at the end of C; and so on, the resulting C is a non-incremental and ordered single-chain table. 53 */54 LinkList merge_LinkList (LinkList A, LinkList B) // A and B are the Single-Chain List of the leading node 55 {56 LinkList C; 57 LNode * p, * q, * r; 58 p = A-> next; 59 q = B-> next; 60 c = A; // The C header node 61 c-> next = NULL; 62 free (B); // Release B's header node 63 while (p & q) 64 {65 if (p-> data <q-> data) 66 {67 r = p; p = p-> next; 68} 69 else 70 {71 r = q; q = q-> next; // remove the smaller node 72 r-> next = C-> next from source A and B; // Insert the first node to the end of the current node (that is, the header inserted to C) 73 C-> next = R; 74} 75 if (p = NULL) p = q; 76 while (p) // remove the remaining nodes one by one, insert the C header 77 {78 r = p; p = p-> next; 79 r-> next = C-> next; 80 C-> next = r; 81} 82} 83} 84 85 // representation of a polynomial and addition time complexity O (m + n) 86/* in mathematics, an n-element polynomial can be expressed as Pn (x) = p0 + p1 * x + p2 * x ^ 2 +... + Pn * x ^ n, which is uniquely determined by n + 1 coefficients. 87 in a computer, it can be expressed by a linear table P, that is, P = (p0, p1, p2 ,... Pn), the exponential I of each item is hidden in the serial number of its coefficient pi. Similarly, 88 Qm (x) indicates Q = (q0, q1, q2 ,... Qm), set m <n, and the sum result Rn (x) is expressed as R = (p0 + q0, p1 + q1, p2 + q2 ,..., Pm + qm, pm + 1 ,..., Pn ). 89 in order to effectively and reasonably use the storage space, all items with a coefficient of 0 are not stored, and only the coefficients without 0 items and their corresponding indexes are stored. 90 algorithm idea: 91 set PA and PB as two head pointers involved in the operation polynomial respectively, PC as the result linked list head pointer, the pointers ha and hb point to the nodes currently compared in polynomial PA and PB, and 92 compare the index items of the two nodes, and perform the following operations. 93 (1) the index value of the node referred to by the pointer ha is smaller than the index value of the node referred to by the pointer hb. Insert the node referred to by ha into the end of the PC chain, and move the pointer ha to a node. 94 (2) the exponent value of the node indicated by the pointer ha is equal to the exponent value of the node indicated by the pointer hb. The coefficients of the two nodes ha and hb are added. If the sum of coefficients is zero, two nodes are deleted; 95 if the sum of coefficients is not zero, the sum of coefficients is assigned to the nodes indicated by ha and inserted to the end of the PC chain, deleting the nodes indicated by hb; 96 (3) the value indicated by the pointer ha is greater than the value indicated by the pointer hb. the pointer hb is inserted into the end of the PC chain and then moved to a node. 97 If a linked list in PA and PB is compared first, the rest of the chain list that has not been compared can be directly connected to the end of the table in the PC linked list. 98 */99 // define node 100 typedef struct Lnode101 {102 float coef; // coefficient (coeficient) 103 int exp; // index (exponent) 104 struct Lnode * next; 105} Lnode, * LinkList; 106 // polynomial addition 107 void Add (Lnode * PA, Lnode * PB, Lnode * PC) 108 {109 Lnode * ha, * hb, * temp; 110 int sum; 111 ha = PA-> next; 112 hb = PB-> next; 113 PC = PA; // The PC pointer is initially directed to the result link header 114 while (ha! = NULL & hb! = NULL) 115 {116 if (ha-> exp 

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.