[英]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類型的變量
如您所見,即時消息將元素從數組中移出了臨時變量。 我的問題是我該如何解決? 我將如何上下移動索引以將項目推到數組的前面? 我試圖獲取數組的內容以保留在數組中。
有什么想法嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.