Chapter11 - Chapter 11 Stacks A stack is a kind of...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 11 Stacks A stack is a kind of container. Like all containers, stacks permit the addition and removal of values. The defining characteristic of stacks is that every removal operation returns the most recently added value. In computer jargon, stacks are first-in-last-out (FILO). The classic example of a stack is the type of spring loaded plate dispensers one sees in restaurants. Stacks show up all over the place in computing, and have their own vocabulary. Adding a value to a stack is called a push, and removing a value is called a pop. The following diagrams illustrate a sequence of eight stack operations. Start at the upper left and read ach line left to right. The stack is initially empty, and is empty after the operations have all been performed. The class definition on the next page defines stacks containing integer values. As the definition is intended solely to illustrate the functionality of a typical stack class, most of the private section has been omitted. Three constructors are provided. The default constructor creates a stack capable of containing any number of values (subject only to the amount of memory available), while the second constructor creates a stack having a fixed capacity. The third constructor (the copy constructor) creates a stack having the same attributes and contents as some other stack. Stack Empty D A D pushed B A B pushed C B A C pushed A Popping gives B B A Popping gives C A Popping gives D A A pushed Popping gives A class IntStack { private: // *** Other Implementation Details Omitted *** int count; // number of values on stack public: // creates an elastic stack IntStack (); // creates a stack with a fixed capacity IntStack (int capacity); IntStack (const IntStack &otherStack); // copy constructor ~IntStack (); // destructor // pushes a value onto the stack // throws an overflow_error exception if the stack has a fixed capacity and is full. void push (int value); // pops a value from the stack // throws an overflow_error exception if the stack is empty int pop (); bool isEmpty() { return count == 0; } int getcount() { return count; } // copies contents only (capacity attributes are not copied). // throws an overflow_error exception if the destination stack // has a fixed capacity and this is not large enough. IntStack& operator= (const IntStack &otherStack); }; Checking for matching parentheses is one (of very, very many) stack applications. Assume that expressions can contain round (()), curly ({}), and square () brackets. We have an expression and want to determine whether the brackets in it are correct. Every opening bracket must have a matching closing bracket, and the brackets must not be mixed up. ([)] is illegal, even though there is a closing bracket for every opening bracket. A suitable algorithm is given on the next page. A suitable algorithm is given on the next page....
View Full Document

Page1 / 15

Chapter11 - Chapter 11 Stacks A stack is a kind of...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online