A simple Queue problem but I cannot fix this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<iostream>
using namespace std;
class Queue{
    private:
        int *queue;
		int q;
        int front;
		int rear;
    public:
        Queue(int);
        void enqueue(int);
        void dequeue();
        int size();
        void display();
};

Queue::Queue(int x)
{
    q=x;
	queue=new int[q];
	rear=-1;
    front=0;
}

void Queue::enqueue(int x)
{
	if(size()==q)
		cout<<"Queue is full! ";
	else{
		queue[front]=x;
		++rear;
	}
}

void Queue::dequeue()
{
	if(front>rear)
		cout<<"Queue is empty! ";
	else{
		queue[rear++];
		cout<<"The data dequeued is :"<<queue[rear];;
	}
}

void Queue::display()
{
    for(int i=front; i<=rear; i++)
        cout<<queue[i]<<" ";
}

int Queue::size()
{
    return (rear-front+1);
}

int main()
{
    Queue q1(2);
    int ch, x;
    while(true){
        cout<<"\n1. Enqueue\n2. Dequeue\n3. Size\n4. Display all element\n5. Quit";
        cout<<"\nWhat do you want to do?\n";
		cout<<"Enter your choice: ";
        cin>>ch;
        switch(ch){
            case 1:
                cout<<"\nEnter data: ";
                cin>>x;
                q1.enqueue(x);
            break;

            case 2:         
                q1.dequeue();
            break;

            case 3:
                cout<<"Size of Queue is "<<q1.size();
            break;

            case 4:
                q1.display();
            break;

            case 5:
		exit(0);
            break;
		}
    }
    
    system ("Pause");
    return 0;
}


Question 1.
When I dequeue a value, it displays a garbage value. Whats wrong in the following function?
1
2
3
4
5
6
7
8
9
void Queue::dequeue()
{
	if(front>rear)
		cout<<"Queue is empty! ";
	else{
		queue[rear++];
		cout<<"The data dequeued is :"<<queue[rear];;
	}
}


Question 2.
Is there any other way to break the while loop(Line no. 61) in the program instead of using
exit(0)
(Line no. 87)?
Last edited on
closed account (o1vk4iN6)

#1

You are incrementing rear instead of decrementing it and you are trying to show the current value of the rear instead of what was there.

#2

1
2
3
4
5
6
7
8
9
10
11
bool running = true;

while(running)
{

    switch()
    {
     case 0: running = false; break;
     }
}
It has worked. Thanks a lot man..
A couple of other things.

1. When enqueuing, you should insert onto the back and modify the back pointer.

2. When dequeuing, you should remove from the front and modify the front pointer.

3. With the design you currently have, you have a limit to the total number of elements you can EVER place on the queue (not just at 1 time). When you remove elements from a fixed sized queue, you need to make sure that the space you empty can be reclaimed for future enqueues. The most intuitive (but extremely time consuming) way is to move each element 1 space forward in the queue (kind of like people moving forward in a line at the post office), and adjusting the back pointer. A better way is to use circular queues. Google it.
Topic archived. No new replies allowed.