簡體   English   中英

圓數組C ++

[英]Circular Array C++

下面的代碼是我自己的實現,用於我的items數組的push_front方法。 我想知道是否有人可以幫助我弄清楚如何實現該方法,以便我只是向上或向下移動索引。 我需要將項目保留在數組中,而不是將它們轉儲到“前端”變量中:

stack::stack(int capacity) : items(new item[capacity]), maxSize(capacity),
count(0), top(-1)
{
    intFront = 0;
}


bool stack::pushFront(const int nPushFront)
{     
    if ( count == maxSize ) // indicates a full array
    {
        return false;
    }
    for ( int shift = 0; shift < count; )
    {
        if ( shift == top+1 )
        {
            intFront = items[top+1].n;
        }
        items->n = items[++shift].n;
        items[shift].n = intFront;
        if ( shift != maxSize-1 )
        {
            intFront = items[++shift].n;
            items[shift].n = items->n;
        }
    }
    ++count;
    items[top+1].n = nPushFront;

    return true;    
}

items-> n指向結構成員n,這是一個int類型的變量

如您所見,即時消息將元素從數組中移出了臨時變量。 我的問題是我該如何解決? 我將如何上下移動索引以將項目推到數組的前面? 我試圖獲取數組的內容以保留在數組中。

有什么想法嗎?

您在那里或之前曾經擁有的是一堆

我想您想要的是一個環形緩沖區

當索引超過數組的末尾時,將其重置為0。(它會自動換行。)當索引超過末尾的索引時,請將該索引重新設置為開始。 您可以在結束索引之后插入元素,只要它不與開始索引重疊即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM