Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions

Question

LO6.1 Discuss price elasticity of demand and how it is calculated.

Answered: 1 week ago