C++雙向鏈表怎么實(shí)現(xiàn)

本篇內(nèi)容主要講解“C++雙向鏈表怎么實(shí)現(xiàn)”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“C++雙向鏈表怎么實(shí)現(xiàn)”吧!

10年積累的成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有泌陽(yáng)免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

C++雙向鏈表怎么實(shí)現(xiàn)

#include <iostream>
using namespace std;
typedef struct node
{
    int data;
    struct node * front;
    struct node *next;
}NODE;
typedef struct doublelist
{
    NODE* head;
    NODE* tail;
}LIST;
LIST* create_list()
{
    LIST* list = new LIST;
    list->head = NULL;
    list->tail = NULL;
    return list;
}
NODE* create_node(int data)
{
    NODE* node = new NODE;
    node->data = data;
    node->front = NULL;
    node->next = NULL;
    return node;
}
void list_append(LIST* list, int data)
{
    NODE* node = create_node(data);
    if(list->tail == NULL)
    {
        list->head = node;
        list->tail = node;
    }
    else
    {
        list->tail->next = node;
        node->front = list->tail;
        list->tail = node;
    }
}
void front_print(LIST* list)
{
    NODE* node = NULL;
    for(node=list->head;node;node=node->next)
    {
        cout<<node->data<<" ";
    }
    cout<<endl;
}


void reverse_print(LIST* list)
{
    NODE* node = NULL;
    for(node =list->tail;node;node=node->front)
    {
        cout<<node->data<<" ";
    }
    cout<<endl;
}
NODE* destroy_node(NODE* node)
{
    NODE* next = node->next;
    delete node;
    return next;
}
void clear(LIST* list)
{
    while(list->head)
    {
        list->head = destroy_node(list->head);
    }
    list->tail = NULL;
}
void destroy_list(LIST* list)
{
    clear(list);
    delete list;
}
void list_insertAfter(LIST* list, int data,int pos) //后插 最起碼應(yīng)該第一個(gè)后面
{
    NODE* find = NULL;
    for(find =list->head;find;find=find->next)
    {
        if(!--pos)
        {
            NODE* node =create_node(data);
            if(find->next == NULL)
            {
                node->front =list->tail;
                list->tail->next = node; list->tail = node;
            }
            else
            {
                node->front = find;
                node->next = find->next;
                find->next->front =node;
                find->next = node;
            }
        }
    }
}
void list_delete(LIST* list, int data)
{
    NODE* Front = NULL;
    NODE* node = list->head;
    while(node)
    {
        if(data == node->data)
        {
            if(list->head ==node)
            {
                list->head = node->next;
                node->next->front = NULL;
                delete node;
                node = list->head;
            }
            else
            {
                Front->next = node->next;
                if(node->next != NULL)
                {
                    node->next->front = Front;
                    delete node;
                    node = Front->next;
                }
                else
                {
                    list->tail = node->front;
                    delete node;
                }
            }
        }
        else
        {
            Front = node;
            node = node->next;
        }
    }
}
void list_insertFront(LIST* list,int data,int pos)
{
    //NODE* front = NULL;
    NODE* find = NULL;
    for(find=list->head;find;find=find->next)
    {
        if(!--pos)
        {
            NODE* node = create_node(data);
            if(find==list->head)
            {
                node->next = list->head;
                list->head->front = node;
                list->head = node;
            }
            else
            {
                node->next = find;
                node->front = find->front;
                find->front->next = node;
                find->front = node;
            }
        }
    }
}
int main()
{
    LIST* list = create_list();
    for(int i=10;i<=50;i+=10)
    {
        list_append(list, i);
    }
    //    list_insertAfter(list,55,5);
    //    list_insertAfter(list,15,1);
    list_insertFront(list,15,2);
    list_insertFront(list,50,6);
     list_insertAfter(list,10,7);
    front_print(list);
    list_delete(list,10);
    front_print(list);
    reverse_print(list);
    destroy_list(list);
    return 0;
}

到此,相信大家對(duì)“C++雙向鏈表怎么實(shí)現(xiàn)”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

網(wǎng)頁(yè)標(biāo)題:C++雙向鏈表怎么實(shí)現(xiàn)
網(wǎng)頁(yè)路徑:http://muchs.cn/article44/ihjdhe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站策劃、小程序開(kāi)發(fā)、品牌網(wǎng)站建設(shè)微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)