Data structure practice--cyclic doubly linked list application

Source: Internet
Author: User

This paper is aimed at the data structure Basic Series Network course (2): Linear table Practice project.

"Project-circular double-linked list application"
The non-null linear table HA and HB are represented by a cyclic doubly linked list of lead nodes. Design an algorithm insert (ha,hb,i). Its function is: when i=0, the linear table HB is inserted at the front of the linear table ha, when i>0, the linear table HB is inserted into the linear table ha in the back of the node I, when I is greater than the length of the linear table ha, the linear table HB is inserted into the final surface of the linear table ha.
When implementing the algorithm, the rest of the work can be supported by the algorithm completed in Project 4, in addition to the special requirements given in the project.

[Reference] (the basic arithmetic of circular doubly linked list, please refer to the relevant algorithm library (CDLINKLIST.H and Cdlinklist.cpp))

#include <Stdio.H>#include <Malloc.H>#include "Cdlinklist.h"voidInsert (cdlinklist*&Ha, cdlinklist*&Hb,int i) {cdlinklist*P=Ha -Next*Q int Lena=1J=1; while(p -Next!=Ha//Find out the length of Ha Lena{Lena++; P=P -Next }if(I==0)//Insert all data nodes of HB into the head node of HA and the 1th data node .{p=Hb -Prior//p point to the last node of HB/P -Next=Ha -Next//*p chain to the 1th data node of HAHa -Next -Prior=P Ha -Next=Hb -Next Hb -Next -Prior=Ha//link the HA head node to the 1th data node of the HB}Else if(I<Lena//HB inserted into HA intermediate{p=Ha -Next while(j<I//Find the first node in Ha *p{J++; P=P -Next } q=P -Next//q points to the *p nodeP -Next=Hb -Next//hb->prior point to the last node of HBHb -Next -Prior=P Hb -Prior -Next=Q Q -Prior=Hb -Prior }Else                        //After the HB chain to ha{ha -Prior -Next=Hb -Next//ha->prior points to the last node of HaHb -Next -Prior=Ha -Prior Hb -Prior -Next=Ha Ha -Prior=Hb -Prior } free (HB);//release HB head Junction}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 NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure practice--cyclic doubly linked list application

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.