Now I'am using Microsoft Visual Studio to compile this program in C++ but when I compile it I have so many 00000 with no end for this procces..

# include<iostream>
using namespace std;
# define SIZE 5


class queue
{
private:
int a[SIZE];
int front;
int rear;
public: 
queue();
~queue();
void insert(int i);
int remove();
int isempty();
int isfull();
int display(int n);
};

queue::queue()
{
front=0;
rear=0;
}
queue::~queue()
{

}
void queue::insert(int i)
{
if(isfull())
{
cout<<"\n******Queue is FULL !!!No insertion allowed further.******";
return;
}
a[rear] = i;
rear++;
}
int queue::remove()
{


if(isempty())
{
cout<<"\n******Queue Empty !!!Value returned will be garbage.******\n";

return (-9999);
}
int i = SIZE ; 
while( i--)
{
a[i] = 0;
};

return(a[0]);

}

int queue::isempty()
{
if(front == rear)
return 1;
else
return 0;
}

int queue::isfull()
{
if(rear == SIZE)
return 1;
else
return 0;
}
int queue::display( int n )
{
int i = a[n];
return i; 
}

void main()
{

queue q;
int item;

while (!q.isfull())
{
cin>>item;
q.insert(item);
}

while(!q.isempty())
{
int n = SIZE ; 
cout << q.display( n) ;
n--;
}

cout<<"\n "<<q.remove();
cout<<"\n"<<q.remove();

}

1)

int n = SIZE ;

That should be just before the loop, and initialised to SIZE - 1.

2) You don't seem to change front and rear, thus the termination condition never reaches.

3) A queue is a FIFO(First In First Out) data structure. What you are doing seems like a Stack(Last In First Out)

1)

int n = SIZE ;

That should be just before the loop, and initialised to SIZE - 1.

2) You don't seem to change front and rear, thus the termination condition never reaches.

3) A queue is a FIFO(First In First Out) data structure. What you are doing seems like a Stack(Last In First Out)

Iwant from this pro to allow the user continue to input and it is full, Display a message, "It's Full!".
and Allow the user to choose an option, to delete an item or not, if the user delete an item, display the item deleted. If the user choose not to delete, display all the items

I gather your intention is to have a ring buffer queue, correct? I think that this solves most of the problems you are having:

#include <iostream>

const int SIZE = 6;


class queue
{
private:
    int a[SIZE + 1];
    int front;
    int rear;
public:
    queue();
    ~queue();
    bool insert(int i);
    int remove();
    bool isempty();
    bool isfull();
    int display(int n);
    friend std::ostream& operator<<(std::ostream& os, queue& q);
};

queue::queue()
{
    front=0;
    rear=0;
}
queue::~queue()
{

}
bool queue::insert(int i)
{
    if(isfull())
    {
        return false;
    }
    a[rear] = i;
    rear = (rear + 1) % SIZE;
    return true;
}
int queue::remove()
{
    if(isempty())
    {
        return 0;
    }
    int current = a[front];
    a[front] = 0;
    front = (front + 1) % SIZE;

    return(current);
}

bool queue::isempty()
{
    if(front == rear)
        return true;
    else
        return false;
}

bool queue::isfull()
{
    if(rear == (front + SIZE - 1) % SIZE)
        return true;
    else
        return false;
}

int queue::display( int n )
{
    int i = a[(front + n) % SIZE ];
    return i;
}


std::ostream& operator<<(std::ostream& os, queue& q)
{
    for (int n = 0; n < SIZE - 2; n++)
    {
        os << q.display(n) << ", ";
    }

    os << q.display(SIZE - 2);

    return os;
}


int main()
{

    queue q;
    int item;

    while (true)
    {
        if (q.isfull())
        {
            char answer;
            std::cout << "The queue is full. Do you want to remove an element(Y/N)? ";
            std::cin >> answer;
            std::cin.ignore();
            if (toupper(answer) == 'Y')
            {
                int top = q.remove();
                std::cout << "You have removed " << top << " from the queue." << std::endl;
            }
            else
            {
                break;
            }
        }
        else
        {
            std::cin >> item;
            std::cin.ignore();
            q.insert(item);
        }
    }

    std::cout << q << std::endl;

    return 0;
}

The key here is taking everything modulo SIZE, which ensures that the front and rear stay within the ring while still moving around. This particular code isn't exactly right, but it should be close enough that you can tweak it to do what you want.

Edited 5 Years Ago by Schol-R-LEA: n/a

I gather your intention is to have a ring buffer queue, correct? I think that this solves most of the problems you are having:

#include <iostream>

const int SIZE = 6;


class queue
{
private:
    int a[SIZE + 1];
    int front;
    int rear;
public:
    queue();
    ~queue();
    bool insert(int i);
    int remove();
    bool isempty();
    bool isfull();
    int display(int n);
    friend std::ostream& operator<<(std::ostream& os, queue& q);
};

queue::queue()
{
    front=0;
    rear=0;
}
queue::~queue()
{

}
bool queue::insert(int i)
{
    if(isfull())
    {
        return false;
    }
    a[rear] = i;
    rear = (rear + 1) % SIZE;
    return true;
}
int queue::remove()
{
    if(isempty())
    {
        return 0;
    }
    int current = a[front];
    a[front] = 0;
    front = (front + 1) % SIZE;

    return(current);
}

bool queue::isempty()
{
    if(front == rear)
        return true;
    else
        return false;
}

bool queue::isfull()
{
    if(rear == (front + SIZE - 1) % SIZE)
        return true;
    else
        return false;
}

int queue::display( int n )
{
    int i = a[(front + n) % SIZE ];
    return i;
}


std::ostream& operator<<(std::ostream& os, queue& q)
{
    for (int n = 0; n < SIZE - 2; n++)
    {
        os << q.display(n) << ", ";
    }

    os << q.display(SIZE - 2);

    return os;
}


int main()
{

    queue q;
    int item;

    while (true)
    {
        if (q.isfull())
        {
            char answer;
            std::cout << "The queue is full. Do you want to remove an element(Y/N)? ";
            std::cin >> answer;
            std::cin.ignore();
            if (toupper(answer) == 'Y')
            {
                int top = q.remove();
                std::cout << "You have removed " << top << " from the queue." << std::endl;
            }
            else
            {
                break;
            }
        }
        else
        {
            std::cin >> item;
            std::cin.ignore();
            q.insert(item);
        }
    }

    std::cout << q << std::endl;

    return 0;
}

The key here is taking everything modulo SIZE, which ensures that the front and rear stay within the ring while still moving around. This particular code isn't exactly right, but it should be close enough that you can tweak it to do what you want.

Thank you I will modify it to make it better..But Can you explan for me what you did in this program..

Sure. The main change I made was that both front and rear are adjusted using the modulo operator whenever they are incremented, such that the values wrap around back to zero when they reach SIZE - 1 . The result is that the queue acts as if it were a ring, rather than a flat array. Thus, if you have the queue

| 1 | 2 | 3 | 4 | 5 |
  ^               ^
  |               |
front           rear

And then remove the first element, you get the queue

| 0 | 2 | 3 | 4 | 5 |
      ^           ^
      |           |
    front        rear

If you then insert a value of 6, you get

| 6 | 2 | 3 | 4 | 5 |
  ^   ^
  |   |
rear front

However, printing out the result (using the new << operator I added) gives

2, 3, 4, 5, 6

This is because the display() function was changed so that the index is relative to the value of front , and modulo SIZE as well.

Sure. The main change I made was that both front and rear are adjusted using the modulo operator whenever they are incremented, such that the values wrap around back to zero when they reach SIZE - 1 . The result is that the queue acts as if it were a ring, rather than a flat array. Thus, if you have the queue

| 1 | 2 | 3 | 4 | 5 |
  ^               ^
  |               |
front           rear

And then remove the first element, you get the queue

| 0 | 2 | 3 | 4 | 5 |
      ^           ^
      |           |
    front        rear

If you then insert a value of 6, you get

| 6 | 2 | 3 | 4 | 5 |
  ^   ^
  |   |
rear front

However, printing out the result (using the new << operator I added) gives

2, 3, 4, 5, 6

This is because the display() function was changed so that the index is relative to the value of front , and modulo SIZE as well.

Thank you .. That was agreat help fo me..

Sure. The main change I made was that both front and rear are adjusted using the modulo operator whenever they are incremented, such that the values wrap around back to zero when they reach SIZE - 1 . The result is that the queue acts as if it were a ring, rather than a flat array. Thus, if you have the queue

| 1 | 2 | 3 | 4 | 5 |
  ^               ^
  |               |
front           rear

And then remove the first element, you get the queue

| 0 | 2 | 3 | 4 | 5 |
      ^           ^
      |           |
    front        rear

If you then insert a value of 6, you get

| 6 | 2 | 3 | 4 | 5 |
  ^   ^
  |   |
rear front

However, printing out the result (using the new << operator I added) gives

2, 3, 4, 5, 6

This is because the display() function was changed so that the index is relative to the value of front , and modulo SIZE as well.

But I want to use my program without using ring array..

This article has been dead for over six months. Start a new discussion instead.