欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C++ 模版雙向鏈表的實(shí)現(xiàn)詳解

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

代碼如下所示:

復(fù)制代碼 代碼如下:

#include <iostream>
template <typename T>
class double_linked
{
    struct node
    {
        T data;
        node* prev;
        node* next;
        node(T t, node* p, node* n) : data(t), prev(p), next(n) {}
    };
    node* head;
    node* tail;
public:
    double_linked() : head( NULL ), tail ( NULL ) {}
    template<int N>
    double_linked( T (&arr) [N]) : head( NULL ), tail ( NULL )
    {
        for( int i(0); i != N; ++i)
            push_back(arr[i]);
    }
    bool empty() const { return ( !head || !tail ); }
    operator bool() const { return !empty(); }
    void push_back(T);
    void push_front(T);
    T pop_back();
    T pop_front();
    ~double_linked()
    {
        while(head)
        {
            node* temp(head);
            head=head->next;
            delete temp;
        }
    }
};
template <typename T>
void double_linked<T>::push_back(T data)
{
    tail = new node(data, tail, NULL);
    if( tail->prev )
        tail->prev->next = tail;
    if( empty() )
        head = tail;
}
template <typename T>
void double_linked<T>::push_front(T data)
{
    head = new node(data, NULL, head);
    if( head->next )
        head->next->prev = head;
    if( empty() )
        tail = head;
}
template<typename T>
T double_linked<T>::pop_back()
{
    if( empty() )
        throw("double_linked : list empty");
    node* temp(tail);
    T data( tail->data );
    tail = tail->prev ;
    if( tail )
        tail->next = NULL;
    else
        head = NULL ;
    delete temp;
    return data;
}
template<typename T>
T double_linked<T>::pop_front()
{
    if( empty() )
        throw("double_linked : list empty");
    node* temp(head);
    T data( head->data );
    head = head->next ;
    if( head )
        head->prev = NULL;
    else
        tail = NULL;
    delete temp;
    return data;
}
int main()
{
    int arr[] = { 4, 6, 8, 32, 19 } ;
    double_linked<int> dlist ( arr );
    dlist.push_back( 11 );
    dlist.push_front( 100 );
    while( dlist )
        std::cout << dlist.pop_back()  << " ";
}

上一篇:深入linux下遍歷目錄樹的方法總結(jié)分析

欄    目:C語言

下一篇:C語言文件操作函數(shù)大全(超詳細(xì))

本文標(biāo)題:C++ 模版雙向鏈表的實(shí)現(xiàn)詳解

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4371.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有