Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef QUEUE_H
- #define QUEUE_H
- #include"Link.h"
- #include<iostream>
- using namespace std;
- template <typename E> class Queue{
- private:
- Link<E>* head;
- Link<E>* tail;
- int size;
- public:
- Queue() {
- head = tail = new Link<E>();
- size = 0;
- }
- ~Queue() {
- clear();
- delete head;
- }
- void clear() {
- while(head->next != 0) {
- dequeue();
- }
- tail = head;
- size = 0;
- }
- void enqueue(const E& it) {
- tail->next = new Link<E>(it, 0);
- tail = tail->next;
- size++;
- }
- E dequeue() {
- if(size != 0) {
- E it = head->next->element;
- Link<E>* aux = head->next;
- head->next = aux->next;
- if (tail == aux){
- tail = head;
- }
- delete aux;
- size --;
- return it;
- }
- else{
- return 0;
- }
- }
- const E& headValue() const {
- if(size != 0) {
- return head->next->element;
- }
- else{
- return 0;
- }
- }
- int length() const {
- return size;
- }
- };
- #endif // QUEUE_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement