Stacks - UAH CPE 212 Fundamentals of Software Engineering...

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

View Full Document Right Arrow Icon
UAH CPE 212 Fundamentals of Software Engineering Agenda Class 12 Stacks Stacks Key Concepts
Background image of page 1

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

View Full DocumentRight Arrow Icon
UAH CPE 212 Today Last Time Linked Lists This Time Stacks
Background image of page 2
UAH CPE 212 Stacks Abstract Concept Can have heterogeneous or homogeneous stacks Preserves Order Often called Last In First Out (LIFO) Uses: Function Calls Backtracking Key Operations Push Pop Error Conditions Overflow Underflow 1 2 3 Empty Push Pop Top
Background image of page 3

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

View Full DocumentRight Arrow Icon
UAH CPE 212 Stacks Sample Uses Word Reversal Parenthesis Matching Calculator Post Fix Notation In Fix Notation
Background image of page 4
UAH CPE 212 Stacks Possible Implementations Array based Advantages performance DisAdvantages Resizing the stack Empty Push Pop Top Next 3 2 1 Empty Push Pop Top Next
Background image of page 5

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

View Full DocumentRight Arrow Icon
UAH CPE 212 Stacks #include "ItemType.h" class StackType { public: StackType(); void MakeEmpty(); bool IsFull() const; bool IsEmpty() const; void Push(ItemType item); void Pop(ItemType& item); private: int top; ItemType items[MAX_ITEMS]; }; CPP FILE #include "Stack1.h" StackType::StackType() { top = -1; } void StackType::MakeEmpty() { top = -1; } bool StackType::IsEmpty() const { return (top == -1); } bool StackType::IsFull() const { return (top == MAX_ITEMS-1); } void StackType::Push(ItemType newItem) { top++; items[top] = newItem; } void StackType::Pop(ItemType& item) { item = items[top]; top--; }
Background image of page 6
UAH CPE 212 Stacks 2 #include "MaxItems.h" template<class ItemType> class StackType { public: StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType item); void Pop(ItemType& item); private: int top; ItemType items[MAX_ITEMS]; }; #include "stack2.cpp" template<class ItemType> StackType<ItemType>::StackType() { top = -1; } template<class ItemType> void StackType<ItemType>::MakeEmpty() { top = -1; } template<class ItemType> bool StackType<ItemType>::IsEmpty() const { return (top == -1); } template<class ItemType> bool StackType<ItemType>::IsFull() const { return (top == MAX_ITEMS - 1); }
Background image of page 7

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

View Full DocumentRight Arrow Icon
UAH CPE 212 Stacks 3 template<class ItemType> void StackType<ItemType>::Push(ItemType newItem) { top++; items[top] = newItem; } template<class ItemType> void StackType<ItemType>::Pop(ItemType& item) { item = items[top]; top--; } Resizing? See text samples for a method Other methods
Background image of page 8
UAH CPE 212 Stacks Linked List Implementation Advantages: Resizing the stack Disadvantage: performance Head Insert and Remove at the Head only
Background image of page 9

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

View Full DocumentRight Arrow Icon
UAH CPE 212 Stack Implementation #ifndef STACK_H #define STACK_H #include <stdlib.h> // Provides size_t template <class Item> class Stack { public: // MEMBER CONSTANTS -- See page 87 for a better alternative
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/09/2008 for the course CPE 212 taught by Professor Skipper during the Spring '07 term at University of Alabama in Huntsville.

Page1 / 22

Stacks - UAH CPE 212 Fundamentals of Software Engineering...

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

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