Help with Queues and Stacks please

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2009
Posts: 27
Reputation: scantraXx- has a little shameless behaviour in the past 
Solved Threads: 0
scantraXx- scantraXx- is offline Offline
Light Poster

Help with Queues and Stacks please

 
0
  #1
Oct 5th, 2009
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.

  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:

  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.

  1. string reverseString(string s)


Any help would be appreciated.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz

Re: Help with Queues and Stacks please

 
0
  #2
Oct 5th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 467
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training

Re: Help with Queues and Stacks please

 
0
  #3
Oct 5th, 2009
Originally Posted by scantraXx- View Post
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.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 64
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training
 
0
  #4
Oct 6th, 2009
Draw these out and trace through the algorithm and it will make much more sense.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC