Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename MyType>
- class Queue
- {
- MyType *arr;
- int count;
- int _size;
- public:
- Queue(){
- _size = 5;
- arr = new MyType[_size];
- count = 0;
- }
- void push(MyType val){
- if (count == _size){
- _size += 5;
- MyType *cpy_arr = new MyType[_size];
- for (int i = 0; i < count; i++)
- cpy_arr[i] = arr[i];
- delete [] arr;
- arr = cpy_arr;
- }
- arr[count++] = val;
- }
- void pop(){
- if (count == 0)
- return;
- for (int i = 0; i < count-1; i++)
- arr[i] = arr[i+1];
- --count;
- }
- void pr(){
- if (count == 0){
- std::cout << "No elem in queue" << std::endl;
- return;
- }
- for (int i = 0; i < count; i++)
- std::cout << arr[i] << " ";
- std::cout << std::endl;
- }
- MyType front(){
- if (count == 0){
- std::cout << "No elem in queue" << std::endl;
- return 0;
- }
- return arr[0];
- }
- bool empty(){
- return count == 0;
- }
- };
- int main(){
- Queue<int> q;
- q.push('1');
- q.push('f');
- q.push('g');
- q.push('b');
- q.push('c');
- q.push('v');
- q.pr();
- std::cout << q.front() << std::endl;
- q.pop();
- q.pop();
- q.pr();
- std::cout << q.front() << std::endl;
- std::cout << q.empty() << std::endl;
- q.pop();
- q.pop();
- q.pop();
- q.pop();
- q.pr();
- std::cout << q.empty() << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement