Data Structure practice-single-chain table: Join and incremental judgment, single-chain Data Structure
This article focuses on the basic series of online courses on data structures (2): practical projects of linear tables.
[Project-single-chain table algorithm] (the Single-chain table algorithm has been used in the program. For details about the header file LinkList. h and the function implementation, see the single-chain table Algorithm Library)
1. It is known that L1 and L2 point to the header nodes of two single-chain tables, and their lengths are m and n respectively. design an algorithm to connect L2 to the back of L1. Implement the algorithm, complete the test, and analyze the complexity of the algorithm.
[Reference]
# Include <stdio. h> # include <malloc. h> # include "linklist. h "void Link (LinkList * & L1, LinkList * & L2) {LinkList * p = L1; while (p-> next! = NULL) // locate the L1 End Node p = p-> next; p-> next = L2-> next; // connect the first L2 data node to the last L1 node and then free (L2); // release unused L2 header node} int main () {LinkList * A, * B; int I; ElemType a [] = {,}; ElemType B [] = {,}; InitList (); for (I = 3; I> = 0; I --) ListInsert (A, 1, a [I]); InitList (B); for (I = 5; i> = 0; I --) ListInsert (B, 1, B [I]); Link (A, B); printf ("A:"); DispList (); destroyList (A); return 0 ;}
2. design an algorithm to determine whether the L of a single-chain table is incremental. Implement this algorithm and complete the test. (The Single-chain table algorithm that has been implemented is used in the program. For details about the header file LinkList. h and the function implementation, see the single-chain table algorithm library)
[Reference]
# Include <stdio. h> # include <malloc. h> # include "linklist. h "bool increase (LinkList * L) {LinkList * p = L-> next, * q; // p points to 1st data nodes if (p! = NULL) {while (p-> next! = NULL) {q = p-> next; // q is the successor of p. if (q-> data> p-> data) // as long as it is incremental, continue to check its successor p = q; else return false; // as long as one is not later than the predecessor, It is not incremental} return true;} int main () {LinkList * A, * B; int I; ElemType a [] = {1, 3, 2, 9}; ElemType B [] = {0, 4, 5, 6, 7, 8}; InitList (A); for (I = 3; I> = 0; I --) ListInsert (A, 1, a [I]); initList (B); for (I = 5; I> = 0; I --) ListInsert (B, 1, B [I]); printf (": % c \ n ", increase ()? 'Y': 'n'); printf ("B: % c \ N", increase (B )? 'Y': 'n'); DestroyList (A); DestroyList (B); return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.