Hey guys.

I need help with these questions please :(

1. Write a function that takes as a parameter a queue of integers and returns a copy of the queue with all the elements stored in reverse order. Use a stack as a helper data structure.

queue<int> reverseQueue(queue<int> q)
// pre: none
// post: returns a queue with the elements stored in reverse order

For example, after creating a queue in the following way and calling the function:

queue<int> myQueue, rQueue;
      myQueue.push(1);
      myQueue.push(2);
      myQueue.push(3);
      rQueue = reverseQueue(myQueue);

then the resulting rQueue has 1 at the back, 2 in the middle, and 3 at the front.

2. Write a function that reverses a string by making use of a stack. Your function could push elements on the stack one by one, and then build up the result by popping them off the stack.

string reverseString(string s)

Any help would be appreciated.

Thanks :)

prob 1.

Remove all the elements of the queue and push it in the stack.
Now pop from the stack one element at a time and add it to the new queue until the stack is empty. Now return the new queue.

prob 2.

the answer is there in your question itself. Just do as its said.


***IMP : Dont expect readymade code in here.
If u want them then u r in the wrong place.

2. Write a function that reverses a string by making use of a stack. Your function could push elements on the stack one by one, and then build up the result by popping them off the stack.

Use the push function to add each element of the string onto the stack, then use the pop function to remove each element. You can print each value after popping, then move onto the next element until the stack is empty.

Draw these out and trace through the algorithm and it will make much more sense.

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.