Queue - Slight problem

Hey,
Im having a play around with making queues I have a problem though. While my code appears to successfully add elements but when I run the display function I keep receiving "The queue is empty ". Some problem with my for loop I guess any ideas?

Thanks

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
#include <iostream>
#include "conio.h"
#include <stdlib.h>

using namespace std;

class queue
{
	int queuel[500],rear,front;

public:
	queue()
	{
		front = rear = -1;
}
	void insert(int x)
	{
		if(rear>500)
		{
			cout <<"The Que is FULL";//arry full
			front = rear = -1;// when full and empty value is -1
		}
		queuel[1+(-1)];// iff not empty icremint rear value by 1 value stored in 0 position of array.
		cout<< "sucessfully inserted :" <<x;

	}

	void del()
	{
		if (front==rear) //if its empty value cannot be deleted.
		{ 
			cout<< "The queue is empty";
			return;
		
		}
		cout<<"Sucessfully deleted: "<<queuel[++front]; //incriment front as value has been removed
	}
	void display()
	{
	if(front==rear)
	{
		cout<< "The queue is empty";
			return;
	}
	for(int i=front+1;i<=rear;i++)
		cout<<queuel[i]<<"";//displays all elements
	
	}

};
int main ()
{
	int ch;
	queue qu;
	while(1)
	{
		cout<<"\n press 1 to INSERT \n Press 2 to DELETE \ press 3 to DISPLAY \n Press 4 to EXIT";
		cin>>ch;
		switch(ch)//value given by user is worked on
		{
		case 1:cout<<"Enter the Element to INSERT:";
			cin>>ch;
			qu.insert (ch);
			break;

		case 2:qu.del();
			break;

		case 3:qu.display();
			break;
		case 4:exit(0);
			break;
		}
	}

			_getch();
	return (0);

   }
You are never changing rear variable.

queuel[1+(-1)]; basic math tells me that it is queuel[0]; and C++ knowledge tells me that this statement doesn't do anything.
Last edited on
Ah ok, queuel[++rear]=x; seems to have done it! Cheers
So taking this further any clues how I could delete a specified element rather than the first element?
Topic archived. No new replies allowed.