Stacks - Stacks CS 308 Data Structures What is a stack? It...

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

View Full Document Right Arrow Icon
Stacks CS 308 – Data Structures
Background image of page 1

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

View Full DocumentRight Arrow Icon
What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack). The last element to be added is the first to be removed ( LIFO : Last In, First Out).
Background image of page 2
Stack Specification Definitions: (provided by the user) MAX_ITEMS : Max number of items that might be on the stack ItemType : Data type of the items on the stack Operations MakeEmpty Boolean IsEmpty Boolean IsFull Push (ItemType newItem) Pop (ItemType& item)
Background image of page 3

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

View Full DocumentRight Arrow Icon
Push (ItemType newItem) Function : Adds newItem to the top of the stack. Preconditions : Stack has been initialized and is not full. Postconditions : newItem is at the top of the stack.
Background image of page 4
Pop (ItemType& item) Function : Removes topItem from stack and returns it in item. Preconditions : Stack has been initialized and is not empty. Postconditions : Top element has been removed from stack and item is a copy of the removed element.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
Stack Implementation #include "ItemType.h" // Must be provided by the user of the class // Contains definitions for MAX_ITEMS and ItemType class StackType { public: StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: int top; ItemType items[MAX_ITEMS]; };
Background image of page 7

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

View Full DocumentRight Arrow Icon
StackType:: StackType()
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 26

Stacks - Stacks CS 308 Data Structures What is a stack? It...

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

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