Documente Academic
Documente Profesional
Documente Cultură
Warning
Definitions
ImageType.h
ImageType.cpp
Driver.cpp
class specification
class implementation
application
(client code)
Question
Why?
What is a stack?
Stack Implementations
Array-based
Linked-list-based
Array-based Stacks
template<class ItemType>
class StackType {
public:
StackType(int);
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Push(ItemType);
void Pop(ItemType&);
private:
int top, maxStack;
ItemType *items;
};
dynamically allocated array
top = -1;
maxStack = size;
items = new ItemType[max];
O(1)
template<class ItemType>
StackType<ItemType>::~StackType()
{
delete [ ] items;
}
O(1)
O(1)
template<class ItemType>
bool StackType<ItemType>::IsFull() const
{
O(1)
Stack overflow
top++;
items[top] = newItem;
}
O(1)
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.
Stack underflow
if(!stack.IsEmpty())
stack.Pop(item);
Templates
Templates allow the compiler to generate multiple
Compiling Templates
Cannot anymore compile StackType.cpp separately
from the application of the class (e.g., driver.cpp)
Linked-list-based Stacks
template<class ItemType>
struct NodeType<ItemType> {
ItemType info;
NodeType<ItemType>* next;
};
O(1)
topPtr = NULL;
}
template<class ItemType>
void StackType<ItemType>::MakeEmpty()
{
NodeType<ItemType>* tempPtr;
while(topPtr != NULL) {
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
}
}
O(N)
O(N)
template<class ItemType>
bool StackType<ItemType>::IsEmpty() const
{
return(topPtr == NULL);
O(1)
}
Pushing on a
non-empty
stack
Function Push
template <class ItemType>
void StackType<ItemType>::Push(ItemType
Push
item)
{
NodeType<ItemType>* location;
location = new NodeType<ItemType>;
location->info = newItem;
location->next = topPtr;
topPtr = location;
}
O(1)
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.
tempPtr
Function Pop
template <class ItemType>
void StackType<ItemType>::Pop(ItemType&
item)
Pop
{
NodeType<ItemType>* tempPtr;
item = topPtr->info;
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
}
O(1)
O(1)
O(1)
O(1)
O(1)
O(1)
O(1)
O(1)
O(1)
O(N)
O(1)
O(1)
O(1)
O(1)
O(N)
Array-vs Linked-list-based
Stack Implementations
expressions.
In postfix notation, the operator is written after the two
operands.
infix: 2+5
postfix: 2 5 +
Postfix expressions:
Algorithm using stacks (cont.)
Stack
tempStack
ItemType item;
StackType tempStack;
while (!Stack.IsEmpty()) {
Stack.Pop(item);
if (item==oldItem)
tempStack.Push(newItem);
else
tempStack.Push(item);
}
while (!tempStack.IsEmpty()) {
tempStack.Pop(item);
Stack.Push(item);
}
}
Stack
3
oldItem = 2
newItem = 5
{
ItemType item;
StackType tempStack;
while (!Stack.IsEmpty()) {
Stack.Pop(item);
if (item==oldItem)
tempStack.Push(newItem);
else
tempStack.Push(item);
}
while (!tempStack.IsEmpty()) {
tempStack.Pop(item);
Stack.Push(item);
}
}
O(N)
Exercises 19, 20
Exercises 19, 20
Exercise 20
small
large
Exercise 20 (contd)
etc.
Exercise 20 (contd)