given a queue:

queue<int> myqueue;

write a function to remove the last entry in the queue.

remark: you can't use stack, pointers, or linked list to do that. You can only use functions such as myqueue.pop(), myqueue.front() etc...

Anyone has any ideas?

Recommended Answers

All 7 Replies

Use a function that returns the last element of the queue (depending of the representation of the queue), then search in the queue for that value and if you find something, use delete and set correctly the pointers or whatever how your queue is represented.

Use a function that returns the last element of the queue (depending of the representation of the queue), then search in the queue for that value and if you find something, use delete and set correctly the pointers or whatever how your queue is represented.

**NO POINTERS OR ARRAY OR LINKED LIST IS ALLOWED**

This is the rule, you can simply use myqueue.pop(), myqueue.push(), myqueue.empty(), myqueue.front() commands to do this.

Also, I can't think of any standard functions that can return the last element of queue. Queue is First-in-first-out.(FIFO)

Ok, declare an array of queue elements, pop() while empty() returns false and push() all the elements except the last one that has been popped. An easy task with the use of loops and a control variable.

Not very efficiently, you could simply push the front() element onto the back of the queue, then pop it from the front. Do this until you get to the last element, but simply pop it instead of pushing and popping it. Because of the 'remark' I take it that this is an assignment and therefore you aren't worried about efficiency, just getting it work. Let me know if that's an acceptable solution.

Dave

Ok, declare an array of queue elements, pop() while empty() returns false and push() all the elements except the last one that has been popped. An easy task with the use of loops and a control variable.

This violates the rule.

**NO POINTERS OR ARRAY OR LINKED LIST IS ALLOWED**

This is a really hard task, isn't it?

The last suggestion respects the rules.

Not very efficiently, you could simply push the front() element onto the back of the queue, then pop it from the front. Do this until you get to the last element, but simply pop it instead of pushing and popping it. Because of the 'remark' I take it that this is an assignment and therefore you aren't worried about efficiency, just getting it work. Let me know if that's an acceptable solution.

Dave

GOOD JOB!!!!!!!!! IT WORKS VERY WELL!!!

THANK YOU VERY MUCH!!!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.