Monday, February 6, 2023
HomeSoftware DevelopmentEnter Restricted Queue - GeeksforGeeks

Enter Restricted Queue – GeeksforGeeks


  

#embody <iostream>

utilizing namespace std;

  

#outline MAX 100

  

class Deque {

    int arr[MAX];

    int entrance;

    int rear;

    int dimension;

  

public:

    Deque(int dimension)

    {

        entrance = -1;

        rear = 0;

        this->dimension = dimension;

    }

  

    

    

    void insertrear(int key);

    void deletefront();

    void deleterear();

    bool isFull();

    bool isEmpty();

    int getFront();

    int getRear();

};

  

bool Deque::isFull()

entrance == rear + 1);

  

bool Deque::isEmpty() { return (entrance == -1); }

  

void Deque::insertrear(int key)

{

    if (isFull()) {

        cout << " Overflown " << endl;

        return;

    }

  

    

    if (entrance == -1) {

        entrance = 0;

        rear = 0;

    }

  

    

    else if (rear == dimension - 1)

        rear = 0;

  

    

    else

        rear = rear + 1;

  

    

    arr[rear] = key;

}

  

void Deque::deletefront()

{

  

    

    

    if (isEmpty()) {

        cout << "Queue Underflown"

             << endl;

        return;

    }

  

    

    if (entrance == rear) {

        entrance = -1;

        rear = -1;

    }

    else

  

        

        if (entrance == dimension - 1)

        entrance = 0;

  

    else

  

        

        

        entrance = entrance + 1;

}

  

void Deque::deleterear()

{

    if (isEmpty()) {

        cout << " Underflown"

             << endl;

        return;

    }

  

    

    if (entrance == rear) {

        entrance = -1;

        rear = -1;

    }

    else if (rear == 0)

        rear = dimension - 1;

    else

        rear = rear - 1;

}

  

int Deque::getFront()

{

  

    

    

    if (isEmpty()) {

        cout << " Underflown"

             << endl;

        return -1;

    }

    return arr[front];

}

  

int Deque::getRear()

{

  

    

    

    if (isEmpty() || rear < 0) {

        cout << " Underflown"

             << endl;

        return -1;

    }

    return arr[rear];

}

  

int primary()

{

    Deque dq(5);

  

    

    cout << "Insert component at rear finish : 5 n";

    dq.insertrear(5);

  

    cout << "insert component at rear finish : 10 n";

    dq.insertrear(10);

  

    cout << "insert component at rear finish : 15 n";

    dq.insertrear(15);

  

    cout << "Get rear component : "

         << " " << dq.getRear() << endl;

  

    dq.deleterear();

    cout << "After delete rear component new rear"

         << " change into : " << dq.getRear() << endl;

  

    cout << "Get entrance component : " << dq.getFront() << endl;

  

    dq.deletefront();

  

    cout << "After delete entrance component new "

         << "entrance change into : " << dq.getFront() << endl;

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments