Data structure routine -- merge ordered tables and data structure routine merge
This article focuses on the data structure basic series of online courses (2): 15th class sequence tables in linear tables.
Problem: There are two ordered tables la and lb, which are combined into an ordered table LC. Do not destroy the original tables LA AND LB
Algorithm idea:
Solution 1: Use a sequence table (supported algorithm libraries and list. H files. Click the link ...)
# Include "list. h "void UnionList (SqList * LA, SqList * LB, SqList * & LC) {int I = 0, j = 0, k = 0; // I, j, and k are subscript LC = (SqList *) malloc (sizeof (SqList); LC-> length = 0; while (I <LA-> length & j <LB-> length) {if (LA-> data [I] <LB-> data [j]) {LC-> data [k] = LA-> data [I]; I ++; k ++ ;} else // LA-> data [I]> LB-> data [j] {LC-> data [k] = LB-> data [j]; j ++; k ++ ;}}while (I <LA-> length) // LA has not been scanned, insert other elements into LC {LC-> data [k] = LA-> data [I]; I ++; k ++ ;} while (j <LB-> length) // LB has not been scanned, insert other elements into LC {LC-> data [k] = LB-> data [j]; j ++; k ++;} LC-> length = k;} int main () {SqList * L1, * L2, * L3; ElemType a [] = {1, 3, 5}; ElemType B [] = {2, 4, 8, 10}; CreateList (L1, a, 3); printf ("L1:"); DispList (L1); CreateList (L2, b, 4); printf ("L2:"); DispList (L2); printf ("merge \ n"); UnionList (L1, L2, L3 ); printf ("L3:"); DispList (L3); DestroyList (L1); DestroyList (L2); DestroyList (L3 );}
Solution 2: Use a single-chain table (supported algorithm libraries and linklist. H files, click the link ...)
# Include <stdio. h> # include <malloc. h> # include "linklist. h "void UnionList1 (LinkList * LA, LinkList * LB, LinkList * & LC) {LinkList * pa = LA-> next, * pb = LB-> next, * pc, * s; LC = (LinkList *) malloc (sizeof (LinkList); // create the LC header node pc = LC; // pc always points to the last node of LC while (pa! = NULL & pb! = NULL) {if (pa-> data <pb-> data) {s = (LinkList *) malloc (sizeof (LinkList )); // copy * pa node s-> data = pa-> data; pc-> next = s; pc = s; // insert * s into the final pa of LC by means of End Plug-In = pa-> next;} else {s = (LinkList *) malloc (sizeof (LinkList )); // copy * pb node s-> data = pb-> data; pc-> next = s; pc = s; // use the tail plug method to insert * s to the last pb of LC = pb-> next ;}} while (pa! = NULL) {s = (LinkList *) malloc (sizeof (LinkList); // copy * pa node s-> data = pa-> data; pc-> next = s; pc = s; // use the end plug method to insert * s to the last pa of LC = pa-> next;} while (pb! = NULL) {s = (LinkList *) malloc (sizeof (LinkList); // copy * pa node s-> data = pb-> data; pc-> next = s; pc = s; // use the end plug method to insert the last pb of * s into LC = pb-> next;} pc-> next = NULL ;} int main () {LinkList * L1, * L2, * L3; ElemType a [] = {, 5}; ElemType B [] = }; createListR (L1, a, 3); printf ("L1:"); DispList (L1); CreateListR (L2, B, 4); printf ("L2 :"); dispList (L2); printf ("merge \ n"); UnionList1 (L1, L2, L3); printf ("L3:"); DispList (L3 ); destroyList (L1); DestroyList (L2); DestroyList (L3); return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.