944,144 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 555
  • C++ RSS
Oct 5th, 2009
0

Help with Queues and Stacks please

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. queue<int> reverseQueue(queue<int> q)
  2. // pre: none
  3. // 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:

C++ Syntax (Toggle Plain Text)
  1. queue<int> myQueue, rQueue;
  2. myQueue.push(1);
  3. myQueue.push(2);
  4. myQueue.push(3);
  5. 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.

C++ Syntax (Toggle Plain Text)
  1. string reverseString(string s)


Any help would be appreciated.

Thanks
Similar Threads
Reputation Points: 1
Solved Threads: 0
Light Poster
scantraXx- is offline Offline
27 posts
since Aug 2009
Oct 5th, 2009
0

Re: Help with Queues and Stacks please

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.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Oct 5th, 2009
0

Re: Help with Queues and Stacks please

Click to Expand / Collapse  Quote originally posted by scantraXx- ...
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.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Oct 6th, 2009
0
Re: Help with Queues and Stacks please
Draw these out and trace through the algorithm and it will make much more sense.
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reading an outside file mortgage calculator
Next Thread in C++ Forum Timeline: Family Age Statistics Program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC