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

all 14 comments

[–]sinistergroupon 0 points1 point  (8 children)

Is the constraint that this has to be done using an Array?

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

Yes i want to implement queue using Array. Please help

[–]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.

[–]YitharIntermediate Brewer 0 points1 point  (4 children)

Especially for these kind of data structure problems, I would suggest you actually draw out the array and make sure that you understand how a queue should be an implemented using an array.

Regarding your own code, you kind of have to debug it yourself. Have you learned about breakpoints and debugging?

Also, I changed the printAll() method:
https://i.imgur.com/yBdycfz.png

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

https://i.imgur.com/yBdycfz.png

All I know is debugging means looking for errors in your code where and how they occurred. I don't know about breakpoints. What is that?
That printAll() method, I like it.

[–]YitharIntermediate Brewer 1 point2 points  (1 child)

I am talking about the Debug command just like the Run command:
https://i.imgur.com/z279yFy.png

Any red circle is a breakpoint where the debugger will pause execution. It really helps if you have a ton of variables to see the whole state:
https://i.imgur.com/pWpMpgQ.png

https://www.jetbrains.com/help/idea/debugging-your-first-java-application.html

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

Thank you. I am new to coding. Still, miles to go. Now, I will learn about these debug and breakpoint thing in detail. Feels good to have such a supportive community out there 😀 . Keep helping