C++ Queue

Changing the order

Page 1 of 1

2 Replies - 3306 Views - Last Post: 30 April 2009 - 03:03 AM Rate Topic: -----

#1 C_Dingo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-December 08

C++ Queue

Posted 29 April 2009 - 10:12 PM

Hey. For an assignment, I need to create a function (refered to as moveNthFront) that takes a positive integer as a paramenter. The function moves the selected element of the queue to the front and leaves the rest of the elements in order (example:

queue = ( 1, 2, 3, 4) and selected element = 3

queue = ( 3, 1, 2, 4))

So far this is what I have:
#include <list>
#include <iostream>
#include <queue>
#include <deque>

using namespace std;

// Using queue with list
typedef list<int > INTLIST;
typedef queue<int> INTQUEUE;

// Using queue with deque
typedef deque<char*> CHARDEQUE;
typedef queue<char*> CHARQUEUE;

int main(void)
	int selectedLocation;

	// Insert items in the queue(uses list)

	cout << "The queue is: " << endl;
	// Output items in queue using front()
	// and use pop() to get to next item until
	// queue is empty
	while (!q.empty())
		cout << q.front() << endl;



I understand the concept of queues, but I don't understand how I'm supposed to move one of the elements to the front using a function. Any help is greatly appreciated. Thank you in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Queue

#2 ladyinblack  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 9
  • View blog
  • Posts: 419
  • Joined: 08-April 09

Re: C++ Queue

Posted 30 April 2009 - 02:57 AM


Make an attempt with the selected element, and see how your program fairs out. You know full well that this site expects to see what attempt you have made. Another thing, consider two queues, so when you testing your output, you'll know what to do next.
Was This Post Helpful? 0
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: C++ Queue

Posted 30 April 2009 - 03:03 AM

Is n counting from the front of the queue?
Is it really a double ended queue (deque)?

Assuming it is.
pop() n elements off the queue, storing them somewhere safe.
push() back the elements you pop()ed off in the new desired order.


More to read here:

If my assumptions are wrong you are going to need to be more explicit about what requirements you have been given.

Understanding the requirements is the starting point for all programming.

This post has been edited by janotte: 30 April 2009 - 03:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1