Question
Using C++, write a stack class using two STL lists. Naturally, a single STL list is more than enough to represent a stack. However, you
Using C++, write a stack class using two STL lists. Naturally, a single STL list is more than enough to represent a stack. However, you are only limited to the following STL list methods: a) void push back(const Object & x); //adds x to the end of list b) void pop front(); //removes the object at the front of the list c) Object & front(); //returns the object at the front of the list d) bool empty() const; //true if empty container
Limited to these methods alone, our STL list essentially becomes a queue (enqueue is basically push back while dequeue is a combination of front and pop front). At the minimum, your stack class needs to have the following:
a) An STL list is stored as an instance member of the stack class. You can think of this main list as storing your stack. b) A pop method that returns an Object at the top of the stack.
c) A push method that takes an Object and places it at the top of the stack. d) If your list is empty, your pop method should throw an exception (any exception will do). e) Inside your push method, you can create a temporary list. As you are only limited to placing items at the back of your list (enqueue / push back), you can use this temporary list as storage to help move items from your main list to this list, add the new item to your main list, and then move back the items from the temporary list back to your main list.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started