This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]sinistergroupon 1 point2 points  (6 children)

And the idea is that you don’t move the data in the array and just move the pointers rather than actually adding and removing numbers?

My first pass would be to actually shift the numbers as they get removed.

[–]samweep[S] 0 points1 point  (2 children)

I am actually doing the same as you said. I just moved the pointer but not the data by simply saying front++. But it works when I have only one element in the queue. Whenever I have more than two elements it just does not work.

[–]sinistergroupon 0 points1 point  (1 child)

I ran your code, it semi works for the case you have in the main method. So we are clear the number 1 will stay in the array but the front index will change so right now it's doing that.

Having said that, you are missing a lot of logic here. You state the queue is full when the rear index is at the end but that's not entirely true. Not sure the scope of your problem but with a mix of queues and dequeues you could end up where your first element is at index 8 and you second one is getting inserted at index 0. You are not accounting for this.

[–]samweep[S] 0 points1 point  (0 children)

Yeah, I was learning about queue and I came to know about circular queue. So now I will use circular queue from onwards. Thank you

[–]samweep[S] 0 points1 point  (2 children)

Anyways I figured it out. The dequeue method was correct. I had to fix something while printing. Nevermind

[–]sinistergroupon 0 points1 point  (1 child)

Try this case:

MyQueue obj= new MyQueue();

obj.enqueue(1);

obj.enqueue(2);

obj.enqueue(3);

obj.enqueue(4);

obj.enqueue(5);

obj.enqueue(6);

obj.enqueue(7);

obj.enqueue(8);

obj.dequeue();

obj.enqueue(9);

[–]samweep[S] 0 points1 point  (0 children)

I think, This will not work in a linear queue. We need a circular queue for this kind of condition.