deque

c++ deque

  • A deque is a double-ended queue
  • Insertion at the front and back is fast
  • Random access is fast
  1. Start: 
    DequeFinal
  2. push_front(0); 
    DequeFinal
  3. push_front(1); 
    push_front(2); 
    push_front(3); 
    DequeFinal
  4. push_front(4); 
    DequeFinal
  5. push_back(0); 
    DequeFinal
  6. push_back(1); 
    push_back(2); 
    push_back(3); 
    DequeFinal
  7. push_back(4); 
    DequeFinal
  8. push_front(5); 
    push_front(6); 
    push_front(7); 
    push_front(8); 
    DequeFinal
  • C++ deque usage
    1:  
    // cppDequeExample.cpp - download  here

    2:  

    3:  
    #include
     
    <
    deque>

    4:  
    #include
     
    <
    iostream>

    5:  

    6:  
    int
     
    main(
    int
     
    argc, 
    char
     
    argv[
    ]
    )
    {

    7:  
     
     

    8:  
     
     
    std:
    :
    deque<
    int
    >
     
    dq1;

    9:  
     
     
    dq1.push_front(
    1)
    ;

    10: 
     
     
    dq1.push_front(
    2)
    ;

    11: 
     
     
    dq1.push_back(
    3)
    ;

    12: 

    13: 
     
     
    for
    (
    size_t
     
    =
     
    0;
     
    <
     
    dq1.size(
    )
    ;
     
    ++i)
    {

    14: 
     
     
     
     
    std:
    :
    cout 
    <
    <
     
    dq1[
    i]
     
    <
    <
     
    " "
    ;

    15: 
     
     
    }

    16: 

    17: 
     
     
    return
     
    0;

    18: 
    }

  • Deque Algorithms
    1:  

    2:  
    void
     
    Deque:
    :
    push_back(
    int
     
    value)
    {

    3:  
     
     
    if
    (
    m_LastBlockStop 
    == 
    m_BlockSize)
    {

    4:  
     
     
     
     
    m_MiddleBlocks.push_back(
    m_LastBlock)
    ;

    5:  
     
     
     
     
    m_LastBlockStop 
    0;

    6:  
     
     
     
     
    m_LastBlock 
    new
     
    int
    [
    m_BlockSize]
    ;

    7:  
     
     
    }

    8:  
     
     
    m_LastBlock[
    m_LastBlockStop]
     
    value;

    9:  
     
     
    ++m_LastBlockStop;

    10: 
     
     
    ++m_Size;

    11: 
    }

    12: 

    13: 
    void
     
    Deque:
    :
    push_front(
    int
     
    value)
    {

    14: 
     
     
    if
    (
    m_FirstBlockStart 
    == 
    m_BlockSize)
    {

    15: 
     
     
     
     
    m_MiddleBlocks.push_front(
    m_FirstBlock)
    ;

    16: 
     
     
     
     
    m_FirstBlockStart 
    0;

    17: 
     
     
     
     
    m_FirstBlock 
    new
     
    int
    [
    m_BlockSize]
    ;

    18: 
     
     
    }

    19: 
     
     
    m_FirstBlock[
    m_BlockSize 
    m_FirstBlockStart 
    1]
     
    value;

    20: 
     
     
    ++m_FirstBlockStart;

    21: 
     
     
    ++m_Size;

    22: 
    }

    23: 

    24: 
    int
     
    Deque:
    :
    getBlock(
    int
     
    block_index)
    {

    25: 
     
     
    std:
    :
    list<int 
    *>
    :
    :
    iterator 
    iter 
    m_MiddleBlocks.begin(
    )
    ;

    26: 
     
     
    for
    (
    int
     
    0;
     
    <
     
    block_index;
     
    ++i)
    {

    27: 
     
     
     
     
    ++iter;

    28: 
     
     
    }

    29: 
     
     
    return
     
    *iter;

    30: 
    }

    31: 

    32: 
    int
     
    Deque:
    :
    get(
    int
     
    index)
    {

    33: 
     
     
    if
    (
    index 
    <
     
    m_FirstBlockStart)
    {

    34: 
     
     
     
     
    int
     
    block_index 
    m_BlockSize 
    m_FirstBlockStart 
    index;

    35: 
     
     
     
     
    return
     
    m_FirstBlock[
    block_index]
    ;

    36: 
     
     
    }
     
    else
     
    if
    (
    index 
    <
     
    m_Size 
    m_LastBlockStop)
    {

    37: 
     
     
     
     
    index 
    -= 
    m_FirstBlockStart;

    38: 
     
     
     
     
    int
     
    block 
    index 
    m_BlockSize;

    39: 
     
     
     
     
    int
     
    curr 
    getBlock(
    block)
    ;

    40: 
     
     
     
     
    int
     
    item 
    index 
    m_BlockSize;

    41: 
     
     
     
     
    return
     
    curr[
    item]
    ;

    42: 
     
     
    }
     
    else
     
    {

    43: 
     
     
     
     
    int
     
    last_index 
    index 
    m_FirstBlockStart 
    (
    m_MiddleBlocks.size(
    )
     
    m_BlockSize)
    ;

    44: 
     
     
     
     
    return
     
    m_LastBlock[
    last_index]
    ;

    45: 
     
     
    }

    46: 
    }

    47: 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值