模板實現簡易queue

2021-07-13 03:40:10 字數 1449 閱讀 3034

佇列提供了以下操作:

q.empty()                 //如果隊列為空返回true,否則返回false

q.size()                  //返回佇列中元素的個數  

q.pop()                  //刪除佇列首元素的值,但不返回其值  

q.front()                 //返回佇列首元素的值,但不刪除其值

q.push()                                   //在隊尾壓入新元素

q.back()                                   //返回隊尾元素的值,但不刪除其值

模擬實現:

/*queue.cpp*/

#include using namespace std;

templatestruct node

t _data;

struct node* _next;

};templateclass linklist

~linklist()

delete cur;

_head = null;

_tail = null;

}public:

void display()

cout<

if(_head == null)

else

}void popfront()

int getlistlength()

return count;

} t& getheadvalue()

t& gettailvalue()

bool judgeisempty()

return false;

}private:

node* _head;

node* _tail;

};templateclass queue

void push(const t& q)

void pop()

int size()

const t& front()

const t& back()

bool empty()

private:

container _con;

};int main()

{ queue> q1;

q1.push(1);

q1.push(2);

q1.push(3);

q1.push(4);

q1.push(5);

q1.show();

q1.pop();

q1.show();

int size = q1.size();

cout<

結果:本文出自 「pzd流川楓」 部落格,請務必保留此出處

Stack與Queue的實現(c 模板實現)

include include using namespace std template 類模板 class stack void pop void push t temp int size int main void include include using namespace std temp...

C 模板實現佇列

我準備練習一下模板的知識,然後自己實現vector類。在這之前,先用模板實現乙個佇列來熱身吧。佇列的底層是鍊錶。主要是熟悉一下模板的寫法。另外,就是模板的定義和實現都要寫在乙個檔案中 export關鍵字可以避免這樣。還沒用過 所以倒數第二行我加了個 include queue.hpp 只能是hpp,...

模板實現順序表

模板就是實現與型別無關的 增加了 的復用性。模板分為模板函式和模板類 模板函式的格式typedef 返回型別 函式名 引數列表 class也可以用typename,意義相同。當需要資料大於所給定的資料時這時我們會考慮增容。void vector checkcapacity void test 我們再...