CSE225 Lecture07 Stack Queue - Array
CSE225 Lecture07 Stack Queue - Array
3
Specification of StackType
Push(ItemType newItem)
Function Adds newItem to the top of the stack.
Precondition Stack has been initialized.
Postcondition If (stack is full), exception FullStack is thrown, else newItem is at
the top of the stack.
Pop()
Function Removes top item from the stack.
Precondition Stack has been initialized.
Postcondition If (stack is empty), exception EmptyStack is thrown, else top
element has been removed from stack.
ItemType Top()
Function Returns a copy of the top item on the stack.
Precondition Stack has been initialized.
Postcondition If (stack is empty), exception EmptyStack is thrown, else a copy of
the top element is returned.
4
stacktype.h
#ifndef STACKTYPE_H_INCLUDED
#define STACKTYPE_H_INCLUDED
class FullStack
{}; // Exception class thrown by Push when stack is full.
class EmptyStack
{}; // Exception class thrown by Pop and Top when stack is empty.
6
stacktype.cpp
#include "StackType.h" template <class ItemType>
template <class ItemType> void StackType<ItemType>::Push(ItemType
newItem)
StackType<ItemType>::StackType()
{
{
O(1)
top = -1; if( IsFull() )
throw FullStack();
}
template <class ItemType>
bool StackType<ItemType>::IsEmpty()
top++;
items[top] = newItem;
O(1)
{ }
}
return (top == -1);
O(1) template <class ItemType>
void StackType<ItemType>::Pop()
void StackType<ItemType>::MakeEmpty() {
O(1)
{
if( IsEmpty() )
}
top = -1;
7
Application of Stack
()
(())()(()())()
(())()((()
(())))((()
8
Application of Stack
(())()(()())()
10
Application of Stack
(())()(()())()
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack
11
Application of Stack
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack (
12
Application of Stack
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack ( (
13
Application of Stack
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack (
14
Application of Stack
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack
15
Application of Stack
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack (
16
Application of Stack
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack
17
Application of Stack
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack (
18
Application of Stack
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack ( (
19
Application of Stack
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack (
20
Application of Stack
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack ( (
21
Application of Stack
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack (
22
Application of Stack
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack
23
Application of Stack
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack (
24
Application of Stack
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack
25
Application of Stack
string ( ( ) ) ( ) ( ( ) ( ) ) ( )
stack
empty stack
Indicates balanced string of parentheses
26
Application of Stack
(())()((()
stack ( (
non-empty stack
Indicates unbalanced string of parentheses
27
Application of Stack
(())))((()
stack
unsuccessful pop
Indicates unbalanced string of parentheses
28
Application of Stack
2-3*4+5 234*-5+ -5
(2 - 3) * (4 + 5) 23-45+* -9
30
Application of Stack
4 8 + 6 5 - * 3 2 – 2 2 + * /
31
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack
33
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 4
34
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 4 8
35
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12
36
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 6
37
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 6 5
38
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 1
39
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12
40
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 3
41
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 3 2
42
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 1
43
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 1 2
44
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 1 2 2
45
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 1 4
46
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 4
47
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 3
48
Application of Stack
stack 3
49
Queue
• A list
• Data items can be added and deleted
• Maintains First In First Out (FIFO) order
Specification of QueueType
Structure: Elements are added to the rear and removed from the front of the queue.
Definitions (provided by user):
MAX_ITEMS Maximum number of items that might be on the queue.
ItemType Data type of the items on the queue.
Operations (provided by the ADT):
MakeEmpty
Function Sets queueto an empty state.
Postcondition Queue is empty.
Boolean IsEmpty
Function Determines whether the queueis empty.
Precondition Queue has been initialized.
Postcondition Returns true if queue is empty and false otherwise.
Boolean IsFull
Function Determines whether the queue is full.
Precondition Queue has been initialized.
Postcondition Returns true if queue is full and false otherwise.
51
Specification of QueueType
Enqueue(ItemType newItem)
Function Adds newItem to the rear of the queue.
Precondition Queue has been initialized.
Postcondition If (queue is full), FullQueue exception is thrown, else newItem is
at rear of queue.
Dequeue(ItemType& item)
Function Removes front item from the queue and returns it as item.
Precondition Queue has been initialized.
Postcondition If (queue is empty), EmptyQueue exception is thrown and item is
undefined, else front element has been removed from queue and
item is a copy of removed element.
52
Implementation Issues
• Always insert elements at the back of the array.
• Complexity of deletion: O(N)
Implementation Issues
• Maintain two indices: front and rear
• Increment the indices as enqueue and dequeue are
performed (rear++ for enqueue and front++ for dequeue)
Dead
space
Implementation Issues
QueType::~QueType()
{
delete [] items;
}
60
queuetype.cpp
void QueType::Enqueue(ItemType newItem)
{
if (IsFull())
throw FullQueue();
else
{
rear = (rear +1) % maxQue;
items[rear] = newItem;
}
}
61
queuetype.cpp
#include "QueType.h" void QueType::MakeEmpty()
{
QueType::QueType(int max)
O(1)
front = maxQue - 1;
{ rear = maxQue - 1;
maxQue = max + 1;
front = maxQue - 1;
rear = maxQue - 1;
O(1) }
QueType::QueType() }
return (rear == front); O(1)
{
maxQue = DEFAULT_SIZE + 1; bool QueType::IsFull()
O(1)
front = maxQue - 1; {
rear = maxQue - 1;
items = new ItemType[maxQue]; return ((rear+1)%maxQue == front);
} }
O(1)
QueType::~QueType()
{
delete [] items; O(1)
}
queuetype.cpp
void QueType::Enqueue(ItemType newItem)
{
if (IsFull())
throw FullQueue();
else
{
rear = (rear +1) % maxQue;
}
items[rear] = newItem;
O(1)
}
}
}
O(1)