943,682 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1112
  • C++ RSS
Sep 26th, 2008
0

Container Adapter Confusion

Expand Post »
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Yuruke is offline Offline
2 posts
since Sep 2008
Sep 26th, 2008
0

Re: Container Adapter Confusion

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.

C++ Syntax (Toggle Plain Text)
  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. };
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Sep 26th, 2008
0

Re: Container Adapter Confusion

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.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007

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: Can't get past this segmentation fault
Next Thread in C++ Forum Timeline: a few questions about C++ source code





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


Follow us on Twitter


© 2011 DaniWeb® LLC