資料結構實踐——迴圈雙鏈表應用,資料結構實踐迴圈

來源:互聯網
上載者:User

資料結構實踐——迴圈雙鏈表應用,資料結構實踐迴圈

本文針對資料結構基礎系列網路課程(2):線性表的實踐項目。

【項目- 迴圈雙鏈表應用】
  設非空線性表ha和hb都用帶前端節點的迴圈雙鏈表表示。設計一個演算法Insert(ha,hb,i)。其功能是:i=0時,將線性表hb插入到線性表ha的最前面;當i>0時,將線性表hb插入到線性表ha中第i個節點的後面;當i大於等於線性表ha的長度時,將線性表hb插入到線性表ha的最後面。
  請在實現演算法時,除項目中給出的特殊要求,其餘工作均可利用項目4完成的演算法支援。

[參考解答](迴圈雙鏈表的基本運算演算法,請參考相關的演算法庫(cdlinklist.h和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) //求出ha的長度lena    {        lena++;        p=p->next;    }    if (i==0)   //將hb的所有資料結點插入到ha的頭結點和第1個資料結點之間    {        p=hb->prior;        //p指向hb的最後一個結點/        p->next=ha->next;       //將*p鏈到ha的第1個資料結點前面        ha->next->prior=p;        ha->next=hb->next;        hb->next->prior=ha;     //將ha頭結點與hb的第1個資料結點鏈起來    }    else if (i<lena)            //將hb插入到ha中間    {        p=ha->next;        while (j<i)         //在ha中尋找第i個結點*p        {            j++;            p=p->next;        }        q=p->next;          //q指向*p結點的後繼結點/        p->next=hb->next;       //hb->prior指向hb的最後一個結點        hb->next->prior=p;        hb->prior->next=q;        q->prior=hb->prior;    }    else                        //將hb鏈到ha之後    {        ha->prior->next=hb->next; //ha->prior指向ha的最後一個結點        hb->next->prior=ha->prior;        hb->prior->next=ha;        ha->prior=hb->prior;    }    free(hb);   //釋放hb頭結點}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);  //將0改為其他值,多次運行程式完成測試    printf("new HA: ");    DispList(HA);    DestroyList(HA);    return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.