Data Structure practices-circular double-stranded table applications and data structure practices
This article focuses on the basic series of online courses on data structures (2): practical projects of linear tables.
[Project-circular double-stranded table application]
The non-empty linear table ha and hb are represented by the cyclic double-chain table of the lead node. Design an algorithm Insert (ha, hb, I ). When I = 0, the hb of the linear table is inserted to the beginning of the ha of the linear table. When I> 0, insert the hb of the linear table to the end of the I node of the linear table ha. When I is greater than or equal to the ha length of the linear table, insert the hb of the linear table to the end of the linear table ha.
When implementing the algorithm, except for the special requirements given in the project, other work can be supported by the algorithm completed in Project 4.
[Reference] (for basic calculation algorithms of cyclic double-stranded tables, see related algorithm libraries (cdlinklist. h and cdlinklist. cpp ))
# Include <stdio. h> # include <malloc. h> # include "cdlinklist. h "void Insert (CDLinkList * & ha, CDLinkList * & hb, int I) {CDLinkList * p = ha-> next, * q; int lena = 1, j = 1; while (p-> next! = Ha) // obtain the ha length lena {lena ++; p = p-> next;} if (I = 0) // insert all data nodes of hb to the ha header and 1st data nodes {p = hb-> prior; // p points to the last node of hb/p-> next = ha-> next; // chain * p to the first of the 1st data nodes in ha ha-> next-> prior = p; ha-> next = hb-> next; hb-> next-> prior = ha; // link the ha header node with the hb's 1st data nodes} else if (I <lena) // insert hb to the ha center {p = ha-> next; while (j <I) // find the I-th node in ha * p {j ++; p = p-> next;} q = p-> next; // q points to the next node of * p node/p-> next = hb-> next; // hb-> prior points to the last hb node hb-> next-> prior = p; hb-> prior-> next = q; q-> prior = hb-> prior;} else // link hb to ha and then {ha-> prior-> next = hb-> next; // ha-> prior points to the last ha node hb-> next-> prior = ha-> prior; hb-> prior-> next = ha; ha-> prior = hb-> prior;} free (hb); // release the hb header node} int main () {CDLinkList * HA, * HB; elemType ha [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; InitList (HA); CreateListF (HA, ha, 10); ElemType hb [] ={ 100,200,300,400,500}; InitList (HB); CreateListF (HB, hb, 5); printf ("HA:"); DispList (HA ); printf ("HB:"); DispList (HB); Insert (HA, HB, 0); // change 0 to another value, run the program multiple times to complete the test printf ("new HA:"); DispList (HA); DestroyList (HA); return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.