Example 8


#include <iostream>
#include <stdexcept>
#include <utility> //pair library

typedef std::pair<int,double> pairIntDouble;

template <class T>
struct Node{
    T data;
    Node<T> *next;//next Node
};

template <class T>
class Queue{
private:
    Node<T>* _front;
    Node<T>* _back;
    int _size;
public:
    Queue(); //class constructor - initialize variables
    ~Queue(); //class destructor - return memory used by queue elements

    void push(const T); //add an item to the back of the queue
    void pop(); //remove the first item from the queue
    T back() const; // return last element
    T front() const; //return first element
    bool empty() const; //returns true if there are no elements in the queue
    int size() const; //return the number of elements
};

template < class T >
Queue<T>::Queue(){
     _front = NULL;
     _back = NULL;
     _size = 0;
}
template < class T >
void Queue<T>::push(const T newQueue){
    if(not empty()){ //if the queue is not empty
       _back->next = new Node<T>; //create a new node
       _back = _back->next; //pointer to new Node
       _back->data = newQueue; // set data
       _back->next = NULL;    
    }
    else{
        //in the case we got emptry queue
        //by the way, we could make a ghost Node to void this comparation
       _back = new Node<T>; //create a new node
       _back->data = newQueue;       
       _back->next = NULL;
       _front = _back; 
    }
    _size++; //increment the amount of elements in the queue
}
template < class T >
void Queue<T>::pop(){
     Node<T>* tmp = _front;
     if(not empty() ){
         _front = _front->next;
         _size--;
         delete tmp;
         if(_front == NULL)//if now is empty update back
             _back = NULL;
     }
}
template < class T >
T Queue<T>::back() const{
     if(not empty())
         return _back->data;
     throw "error";
}
template < class T >
T Queue<T>::front() const{
     if(not empty())
         return _front->data;
     throw "error";
}
template < class T >
bool Queue<T>::empty() const{
     return (_size == 0);
}
template < class T >
int Queue<T>::size() const{
     return _size;
}
template < class T >
Queue<T>::~Queue(){
     while(not empty()) pop(); 
}


int main(){
     Queue<std::string> myQueue;
     
     myQueue.push("aaaaa");
     myQueue.push("abbbb");
     myQueue.push("acccc");
     
     while(not myQueue.empty()){
         std::cout << "( " << myQueue.front() << ", " << myQueue.back() << ") " << myQueue.size() << std::endl;
         myQueue.pop();
     }
     
     Queue< pairIntDouble > myQueuePair;
     
     myQueuePair.push(pairIntDouble(12,12.3));
     myQueuePair.push(pairIntDouble(1,2.3));
     myQueuePair.push(pairIntDouble(3,2.3));
     
     while(not myQueuePair.empty()){
         pairIntDouble a = myQueuePair.front();
         pairIntDouble b = myQueuePair.back();
         std::cout << "(" << a.first << ", " << a.second << ") - ";
         std::cout << "(" << b.first << ", " << b.second << ")" << std::endl;
         
         myQueuePair.pop();
     }
}

output:


(1, 4)with size:4
( aaaaa, acccc) 3
( abbbb, acccc) 2
( acccc, acccc) 1
(12, 12.3) - (3, 2.3)
(1, 2.3) - (3, 2.3)
(3, 2.3) - (3, 2.3)

RUN SUCCESSFUL (total time: 243ms)

No comments:

Post a Comment