Data structure routine -- merge ordered tables and data structure routine merge

Source: Internet
Author: User

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.

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.