Container Adapter Confusion

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

Join Date: Sep 2008
Posts: 2
Reputation: Yuruke is an unknown quantity at this point 
Solved Threads: 0
Yuruke Yuruke is offline Offline
Newbie Poster

Container Adapter Confusion

 
0
  #1
Sep 26th, 2008
Having trouble getting started on an assignment for my c++ class. I understand how stacks/queues work, but what I don't quite understand is how they work as adapters, i've read plenty of sites but none really have given me a good example of how they work when implemented using a container. The project we were given asks us to use the deque class to store 2 stacks.One stack updates its elements with push_front( ) / pop_front( ), and the other stack uses push_back( ) / pop_back( ).
My original idea:
-create a stack class, and #include <deque>
-create a deque named dualstk to use to store the stacks.
-create the methods/constructors used for stacks.

Overall the main confusion I myself am having is how to go about this, I don't really need code, I just need a better explanation/algorithm or something to help me understand how to work this. Any help would be greatly appreciated! Thanks!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Container Adapter Confusion

 
0
  #2
Sep 26th, 2008
have each of the two stacks to store a reference to a std::deque.
initialize them to refer to the same std::deque.
write a wrapper class to hold the two stacks.

  1. template< typename T > struct dual_stack
  2. {
  3.  
  4. struct first_stack
  5. {
  6. explicit first_stack( std::deque<T>& seq ) : sequence(seq) {}
  7. // implement stack members
  8. // push/pop => sequence.push/pop_front()
  9. private : std::deque<T>& sequence ;
  10. };
  11.  
  12. struct second_stack
  13. {
  14. explicit second_stack( std::deque<T>& seq ) : sequence(seq) {}
  15. // implement stack members
  16. // push/pop => sequence.push/pop_back()
  17. private : std::deque<T>& sequence ;
  18. };
  19.  
  20.  
  21. dual_stack() : _first_stack(sequence), _second_stack(sequence) {}
  22.  
  23. first_stack& first() { return _first_stack ; }
  24. second_stack& second() { return _second_stack ; }
  25. // const versions of the above, other members.
  26.  
  27. private:
  28. std::deque<T> sequence ;
  29.  
  30. first_stack _first_stack ;
  31. second_stack _second_stack ;
  32. };
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Container Adapter Confusion

 
0
  #3
Sep 26th, 2008
Lets assume you have Stack1 (push_back, pop_back) and Stack2 (push_front, pop_front)

Say you want to enqueue 1,2,3

Lets enqueue the numbers onto Stack1 using push_back

Stack1 looks like : back |3|2|1| front

Stack2 looks like: back |EMPTY| front

Now you want to dequeue a number. So lets pop_back all the numbers from Stack1 and push_front them onto Stack2

Stack1 looks like: back |EMPTY| front

Stack2 looks like: back |3|2|1| front

Now you can dequeue the number using pop_front on Stack2.

Now say you want to enqueue 10 .

Stack1 looks like: back |10| front

Stack2 looks like: back |3|2|1| front

So now you will keep enqueueing the elements onto Stack1 using push_back and dequeueing them from Stack2 using pop_front. Once Stack2 is empty, if you need to dequeue further elements, then you need to pop_back all the elements from Stack1 and add them onto Stack2.

I hope that helps.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC