日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【笔记 】栈底层 循环队列的处理 链栈 链队列

發布時間:2024/9/30 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【笔记 】栈底层 循环队列的处理 链栈 链队列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

隊列

解決“假溢出”問題的方法:

采用循環隊列方式:將數組的頭尾看作是相鄰的元素,
即將元素data[0]看作是data[maxlen-1]的下一個元素。如圖所示。
因此,插入和刪除以及狀態檢測需要作相應的調整:
插入操作中移動指示位置的計算:
if ( rear+1 == maxlen ) rear = 0;
else rear++;
或者:rear = ( rear + 1 ) % maxlen ;
或者:rear = ( rear + 1 == maxlen ) ? 0 : rear ++ ;
刪除操作:front = ( front + 1 ) % maxlen ;
隊列空條件: front == rear

入隊與出隊:
error_code queue::append(const elementtype x)
{
if ( full() ) return overflow;
rear = ( rear + 1 ) % maxlen ;
data[rear] = x;
count ++;
return success;
}

error_code queue::serve()
{
if ( empty() ) return underflow;
front = ( front + 1 ) % maxlen;
count --;
return success;
}
分析:
算法的時間復雜度均為O(1).

鏈棧

class stack{
public: // 函數成員
stack();
~stack();
bool empty()const;
bool full()const;
error_code get_top(elementtype &x) const;
error_code push(const elementtyoe x);
error_code pop();
private: // 數據成員
int count;
node * top; //棧頂指針
};
入棧運算的實現 入棧就是將待插入元素裝入一個結點后,連接到鏈表的表頭上。
因此,操作包括:產生結點;裝入元素的值到結點中;
連接結點到表頭–

鏈隊列

class queue{public: // 函數成員queue();~queue();bool empty()const;bool full()const;error_code get_front(elmenttype &x)const;error_code append(const elementtype x);error_code serve();private: // 數據成員int count;node * front, * rear;

};

算法如下:
error_code queue::append(const elementtype x ){
node *s = new node;
s -> next = NULL;
s -> data = x; // 封裝
rear -> next = s;
rear = s; //插入
count ++;
return success;
}

總結

以上是生活随笔為你收集整理的【笔记 】栈底层 循环队列的处理 链栈 链队列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。