Untitled

 avatar
unknown
plain_text
3 years ago
2.4 kB
3
Indexable
/Queue.cpp

#include <iostream>
#include <cassert>

using namespace std;

//following class represents a Member
class Member
{
    int x, y;

public:
    //constructor
    Member()
    {
        x = 0;
        y = 0;
    }
    Member(int x, int y)
    {
        setX(x);
        setY(y);
    }

    //following method sets x value
    void setX(int x)
    {
        this->x = x;
    }

    //following method sets y value
    void setY(int y)
    {
        this->y = y;
    }

    //following method displays the member
    void display()
    {
        cout << "x = " << x << ",y = " << y << endl;
    }

    friend ostream &operator<<(ostream &out, Member &m);
};

ostream &operator<<(ostream &out, Member &m)
{
    out << "(" << m.x << ", " << m.y << ") ";
    return out;
}

//following class represents a Queue
template <typename T>
class Queue
{
    //data members
    int capacity;
    int head;   //head of the queue
    T *content; //content of the Queue

public:
    //constructor
    Queue()
    {
        capacity = 10;
        head = 0;
        content = new T[capacity];
    }

    //following method returns true if the Queue is empty
    bool empty()
    {
        return (head == 0);
    }

    //following method returns true if the Queue is full
    bool full()
    {
        return (head == capacity);
    }

    //overloading +=
    Queue &operator+=(T item)
    {
        assert(!full());
        content[head] = item;
        head++;
        cout << item;
        cout << " added to the Queue sccessfully..." << endl;
        return *this;
    }

    //following method deletes element at the head
    void deleteE()
    {
        assert(!empty());
        head--;
        cout << "head element deleted successfully..." << endl;
    }

    void display()
    {
        if (empty())
        {
            cout << "The queue is empty..." << endl;
            return;
        }

        cout << "Queue is => " << endl;

        for (int i = 0; i < head; i++)
        {
            cout << content[i] << endl;
        }
    }
};
int main()
{

    //creating a Queue
    Queue<Member> myQueue;

    //creating 3 Members
    Member m1(5, 6);
    Member m2(3, 4);
    Member m3(1, 2);

    myQueue += m1;
    myQueue += m2;
    myQueue += m3;

    myQueue.display();

    myQueue.deleteE();

    myQueue.display();

    cout << endl;
    return 0;
}

//output: