Tuesday, February 28, 2012

Data Structure and Algorithm - Queue - queueAsArray.h source code

queueAsArray.h  as template


#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;

template <class Type>
class queueType{
private:
    int maxQueueSize;
    int count;
    int queueFront;
    int queueRear;
    Type* list;
public:
    void initializeQueue(){             // original queue
        queueFront = 0;
        queueRear = maxQueueSize - 1;
        count = 0;
    }
    bool isEmptyQueue(){                // Is queue empty ? [count = 0]
        return (count == 0);
    }
    bool isFullQueue(){                 // Is queue full ? [count = max of queue]
        return (count == maxQueueSize);
    }
    void destroyQueue(){                // delete all queue
        initializeQueue();
    }
    Type front(){                       // return value front of queue
        assert(!isEmptyQueue());
        return list[queueFront];
    }
    Type back(){                        // return value back or rear of queue
        assert(!isEmptyQueue());
        return list[queueRear];
    }
    int size(){                         // return value size of queue present
        return count;
    }
    int nFront(){                       // return value of front
        return queueFront;
    }
    int nRear(){
        return queueRear;               // return value of rear
    }
    Type locationQueue(int loc){        // return value of queue in any loction
        return list[loc];
    }
    int maxSize(){
        return maxQueueSize;
    }
    void addQueue(const Type& newElement){      // add queue from the rear
        if(!isFullQueue()){
            queueRear = (queueRear + 1) % maxQueueSize;
            count++;
            list[queueRear] = newElement;
        }else{
            cout<<"Queue list is Full => can not add more..."<<endl;
        }
    }
    void deleteQueue(){                 // delete queue from the front
        if(!isEmptyQueue()){
            queueFront = (queueFront + 1) % maxQueueSize;
            count--;
        }else{
            cout<<"Queue list is Empty => can not delete more..."<<endl;
        }
    }
    queueType(int queueSize){           // constructor
        if(queueSize <= 0){
            cout<<"Size of array to hold the element must be Positive.."<<endl;
            cout<<"Create a array of size = 100"<<endl;
            maxQueueSize = 100;
        }else
            maxQueueSize = queueSize;
        queueFront = 0;
        queueRear = maxQueueSize -1;
        count = 0;
        list = new Type[maxQueueSize];
        assert(list !=NULL);
    }
    ~queueType(){                       // destructor
        delete []list;
    }
};

No comments:

Post a Comment