資料結構常式——單鏈表應用舉例,常式單鏈

來源:互聯網
上載者:User

資料結構常式——單鏈表應用舉例,常式單鏈

  本文針對資料結構基礎系列網路課程(2):線性表中第11課時單鏈表應用舉例。

例:拆分單鏈表 (linklist.h是單鏈表“演算法庫”中的標頭檔,詳情單擊連結…)

#include <stdio.h>#include <malloc.h>#include "linklist.h"void split(LinkList *&L,LinkList *&L1,LinkList *&L2){    LinkList *p=L->next,*q,*r1; //p指向第1個資料節點    L1=L;       //L1利用原來L的前端節點    r1=L1;                  //r1始終指向L1的尾節點    L2=(LinkList *)malloc(sizeof(LinkList));    //建立L2的前端節點    L2->next=NULL;          //置L2的指標域為NULL    while (p!=NULL)    {        r1->next=p;         //採用尾插法將*p(data值為ai)插入L1中        r1=p;        p=p->next;          //p移向下一個節點(data值為bi)        q=p->next;          //由於頭插法修改p的next域,故用q儲存*p的後繼節點        p->next=L2->next;   //採用頭插法將*p插入L2中        L2->next=p;        p=q;                //p重新指向ai+1的節點    }    r1->next=NULL;          //尾節點next置空}int main(){    LinkList *L,*L1,*L2;    int i;    ElemType a[]= {1,2,3,4,5,6,7,8,9,10};    InitList(L);    InitList(L1);    InitList(L2);    for(i=9; i>=0; i--)        ListInsert(L, 1, a[i]);    printf("L:");    DispList(L);    printf("L->L1,L2\n");    split(L,L1,L2);    printf("L1:");    DispList(L1);    printf("L2:");    DispList(L2);    DestroyList(L1);    DestroyList(L2);    return 0;}

例:刪除元素最大的節點(linklist.h是單鏈表“演算法庫”中的標頭檔,詳情單擊連結…)

#include <stdio.h>#include <malloc.h>#include "linklist.h"void delmaxnode(LinkList *&L){    LinkList *p=L->next,*pre=L,*maxp=p,*maxpre=pre;    while (p!=NULL) //用p掃描整個單鏈表,pre始終指向其前驅節點    {        if (maxp->data<p->data)  //若找到一個更大的節點        {            maxp=p; //更改maxp            maxpre=pre; //更改maxpre        }        pre=p;      //p、pre同步後移一個節點        p=p->next;    }    maxpre->next=maxp->next;    //刪除*maxp節點    free(maxp);         //釋放*maxp節點}int main(){    LinkList *L;    int i;    ElemType a[]= {1,3,2,9,0,4,7,6,5,8};    InitList(L);    for(i=9; i>=0; i--)        ListInsert(L, 1, a[i]);    printf("L:");    DispList(L);    printf("刪除最大值節點\n");    delmaxnode(L);    printf("L:");    DispList(L);    DestroyList(L);    return 0;}

例:增序排列節點(linklist.h是單鏈表“演算法庫”中的標頭檔,詳情單擊連結…)

#include <stdio.h>#include <malloc.h>#include "linklist.h"void sort(LinkList *&L){    LinkList *p,*pre,*q;    p=L->next->next;        //p指向L的第2個資料節點    L->next->next=NULL;     //構造只含一個資料節點的有序表    while (p!=NULL)    {        q=p->next;          //q儲存*p節點後繼節點的指標        pre=L;              //從有序表開頭進行比較,pre指向插入*p的前驅節點        while (pre->next!=NULL && pre->next->data<p->data)            pre=pre->next;  //在有序表中找插入*p的前驅節點*pre        p->next=pre->next;  //將*pre之後插入*p        pre->next=p;        p=q;                //掃描原單鏈表餘下的節點    }}int main(){    LinkList *L;    int i;    ElemType a[]= {1,3,2,9,0,4,7,6,5,8};    InitList(L);    for(i=9; i>=0; i--)        ListInsert(L, 1, a[i]);    printf("L:");    DispList(L);    printf("排序\n");    sort(L);    printf("L:");    DispList(L);    DestroyList(L);    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.