C++ Plus Data Structures
Nell Dale
Chapter 4
ADTs Stack and Queue
Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus
1
Stacks of Coins and Bills
2
What is a Stack?
Logical (or ADT) level: A stack is an
ordered group of homogeneous items
(elements), in which the removal and
addition of stack items can take place
only at the top of the stack.
A stack is a LIFO “last in, first out”
structure.
3
Stacks of Boxes and Books
TOP OF THE STACK TOP OF THE STACK
4
Stack ADT Operations
MakeEmpty -- Sets stack to an empty state.
IsEmpty -- Determines whether the stack is currently
empty.
IsFull -- Determines whether the stack is currently full.
Push (ItemType newItem) -- Adds newItem to the top
of the stack.
Pop (ItemType& item) -- Removes the item at the top
of the stack and returns it in item.
5
ItemType
Class Interface Diagram
class ItemType
ItemType
Private data
Print
value
Initialize
6
Specifying class ItemType
// SPECIFICATION FILE ( itemtype.h )
const int MAX_ITEM = 5 ;
class ItemType // declares class data type
{
public : // 3 public member functions
ItemType();
void Print ( ) const ;
void Initialize ( int ) ;
private : // 1 private data member
int value ; // could be any different type
};
7
// IMPLEMENTATION FILE ( itemtype.cpp )
// Implementation depends on the data type of value.
#include “itemtype.h”
#include <iostream.h>
ItemType::ItemType()
{ // Default Constructor
value=0;
}
void ItemType::Print ( ) const
{
cout << value << endl ;
}
void ItemType::Initialize ( int number )
{
value = number ;
}
8
ADT Stack Operations
Transformers
MakeEmpty change state
Push
Pop
Observers
IsEmpty observe state
IsFull
9
Class Interface Diagram
StackType class
StackType
Private data:
top
MakeEmpty
[MAX_ITEMS-1]
IsEmpty .
.
.
IsFull
[2]
Push [1]
Pop items [0]
10
//----------------------------------------------------------
// SPECIFICATION FILE (stack.h)
//----------------------------------------------------------
#include "bool.h"
#include "ItemType.h" // for MAX_ITEMS and
// class ItemType definition
class StackType {
public:
StackType( );
void MakeEmpty( );
bool IsEmpty( ) const;
bool IsFull( ) const;
void Push( ItemType newItem );
void Pop( ItemType& item );
private:
int top;
ItemType items[MAX_ITEMS]; // array of ItemType
}; 11
//-------------------------------------------------------
// IMPLEMENTATION FILE (Stack.cpp)
//------------------------------------------------------
#include “ItemType.h”
StackType::StackType( )
{
top = -1;
}
void StackType::MakeEmpty( )
{
top = -1;
}
12
// IMPLEMENTATION FILE continued (Stack.cpp)
//----------------------------------------------------------
bool StackType::IsEmpty( ) const
{
return ( top == -1 );
}
bool StackType::IsFull( ) const
{
return ( top == MAX_ITEMS-1 );
}
13
// IMPLEMENTATION FILE continued (Stack.cpp)
//------------------------------------------------------------
-
void StackType::Push ( ItemType newItem )
{
top++;
items[top] = newItem;
}
void StackType::Pop ( ItemType& item )
{
item = items[top];
top--;
}
14
letter ‘V’ Tracing Client Code
char letter = ‘V’;
Private data: StackType charStack;
top charStack.Push(letter);
charStack.Push(‘C’);
[MAX_ITEMS-1]
.
charStack.Push(‘S’);
.
. if ( !charStack.IsEmpty( ))
charStack.Pop(letter);
[2]
charStack.Push(‘K’);
[1] while (!charStack.IsEmpty( ))
items [ 0 ] charStack.Pop(letter);
15
letter ‘V’ Tracing Client Code
char letter = ‘V’;
Private data: StackType charStack;
top -1 charStack.Push(letter);
charStack.Push(‘C’);
[MAX_ITEMS-1]
.
charStack.Push(‘S’);
.
. if ( !charStack.IsEmpty( ))
charStack.Pop(letter);
[2]
charStack.Push(‘K’);
[1] while (!charStack.IsEmpty( ))
items [ 0 ] charStack.Pop(letter);
16
letter ‘V’ Tracing Client Code
char letter = ‘V’;
Private data:
StackType charStack;
top 0 charStack.Push(letter);
charStack.Push(‘C’);
[MAX_ITEMS-1]
.
charStack.Push(‘S’);
.
. if ( !charStack.IsEmpty( ))
charStack.Pop(letter);
[2]
charStack.Push(‘K’);
[1] while (!charStack.IsEmpty( ))
items [ 0 ] ‘V’ charStack.Pop(letter);
17
letter ‘V’ Tracing Client Code
char letter = ‘V’;
Private data:
StackType charStack;
top 1 charStack.Push(letter);
charStack.Push(‘C’);
[MAX_ITEMS-1]
.
charStack.Push(‘S’);
.
. if ( !charStack.IsEmpty( ))
charStack.Pop(letter);
[2]
charStack.Push(‘K’);
[1] ‘C’ while (!charStack.IsEmpty( ))
items [ 0 ] ‘V’ charStack.Pop(letter);
18
letter ‘V’ Tracing Client Code
char letter = ‘V’;
Private data:
StackType charStack;
top 2 charStack.Push(letter);
charStack.Push(‘C’);
[MAX_ITEMS-1]
.
charStack.Push(‘S’);
.
. if ( !charStack.IsEmpty( ))
charStack.Pop(letter);
[2] ‘S’
charStack.Push(‘K’);
[1] ‘C’ while (!charStack.IsEmpty( ))
items [ 0 ] ‘V’ charStack.Pop(letter);
19
letter ‘V’ Tracing Client Code
char letter = ‘V’;
Private data:
StackType charStack;
top 2 charStack.Push(letter);
charStack.Push(‘C’);
[MAX_ITEMS-1]
.
charStack.Push(‘S’);
.
. if ( !charStack.IsEmpty( ))
charStack.Pop(letter);
[2] ‘S’
charStack.Push(‘K’);
[1] ‘C’ while (!charStack.IsEmpty( ))
items [ 0 ] ‘V’ charStack.Pop(letter);
20
letter ‘S’ Tracing Client Code
char letter = ‘V’;
Private data:
StackType charStack;
top 1 charStack.Push(letter);
charStack.Push(‘C’);
[MAX_ITEMS-1]
.
charStack.Push(‘S’);
.
. if ( !charStack.IsEmpty( ))
charStack.Pop(letter);
[2] ‘S’
charStack.Push(‘K’);
[1] ‘C’ while (!charStack.IsEmpty( ))
items [ 0 ] ‘V’ charStack.Pop(letter);
21
letter ‘S’ Tracing Client Code
char letter = ‘V’;
Private data:
StackType charStack;
top 2 charStack.Push(letter);
charStack.Push(‘C’);
[MAX_ITEMS-1]
.
charStack.Push(‘S’);
.
. if ( !charStack.IsEmpty( ))
charStack.Pop(letter);
[2] ‘K’
charStack.Push(‘K’);
[1] ‘C’ while (!charStack.IsEmpty( ))
items [ 0 ] ‘V’ charStack.Pop(letter);
22
letter ‘S’ Tracing Client Code
char letter = ‘V’;
Private data:
StackType charStack;
top 2 charStack.Push(letter);
charStack.Push(‘C’);
[MAX_ITEMS-1]
.
charStack.Push(‘S’);
.
. if ( !charStack.IsEmpty( ))
charStack.Pop(letter);
[2] ‘K’
charStack.Push(‘K’);
[1] ‘C’ while (!charStack.IsEmpty( ))
items [ 0 ] ‘V’ charStack.Pop(letter);
23
letter ‘K’ Tracing Client Code
char letter = ‘V’;
Private data:
StackType charStack;
top 1 charStack.Push(letter);
charStack.Push(‘C’);
[MAX_ITEMS-1]
.
charStack.Push(‘S’);
.
. if ( !charStack.IsEmpty( ))
charStack.Pop(letter);
[2] ‘K’
charStack.Push(‘K’);
[1] ‘C’ while (!charStack.IsEmpty( ))
items [ 0 ] ‘V’ charStack.Pop(letter);
24
letter ‘K’ Tracing Client Code
char letter = ‘V’;
Private data:
StackType charStack;
top 1 charStack.Push(letter);
charStack.Push(‘C’);
[MAX_ITEMS-1]
.
charStack.Push(‘S’);
.
. if ( !charStack.IsEmpty( ))
charStack.Pop(letter);
[2] ‘K’
charStack.Push(‘K’);
[1] ‘C’ while (!charStack.IsEmpty( ))
items [ 0 ] ‘V’ charStack.Pop(letter);
25
letter ‘C’ Tracing Client Code
char letter = ‘V’;
Private data:
StackType charStack;
top 0 charStack.Push(letter);
charStack.Push(‘C’);
[MAX_ITEMS-1]
.
charStack.Push(‘S’);
.
. if ( !charStack.IsEmpty( ))
charStack.Pop(letter);
[2] ‘K’
charStack.Push(‘K’);
[1] ‘C’ while (!charStack.IsEmpty( ))
items [ 0 ] ‘V’ charStack.Pop(letter);
26
letter ‘C’ Tracing Client Code
char letter = ‘V’;
Private data:
StackType charStack;
top 0 charStack.Push(letter);
charStack.Push(‘C’);
[MAX_ITEMS-1]
.
charStack.Push(‘S’);
.
. if ( !charStack.IsEmpty( ))
charStack.Pop(letter);
[2] ‘K’
charStack.Push(‘K’);
[1] ‘C’ while (!charStack.IsEmpty( ))
items [ 0 ] ‘V’ charStack.Pop(letter);
27
letter ‘V’ Tracing Client Code
char letter = ‘V’;
Private data:
StackType charStack;
top -1 charStack.Push(letter);
charStack.Push(‘C’);
[MAX_ITEMS-1]
.
charStack.Push(‘S’);
.
. if ( !charStack.IsEmpty( ))
charStack.Pop(letter);
[2] ‘K’
charStack.Push(‘K’);
[1] ‘C’ while (!charStack.IsEmpty( ))
items [ 0 ] ‘V’ charStack.Pop(letter);
28
letter ‘V’ End of Trace
char letter = ‘V’;
Private data:
StackType charStack;
top -1 charStack.Push(letter);
charStack.Push(‘C’);
[MAX_ITEMS-1]
.
charStack.Push(‘S’);
.
. if ( !charStack.IsEmpty( ))
charStack.Pop(letter);
[2] ‘K’
charStack.Push(‘K’);
[1] ‘C’ while (!charStack.IsEmpty( ))
items [ 0 ] ‘V’ charStack.Pop(letter);
29
What is a Queue?
Logical (or ADT) level: A queue is an
ordered group of homogeneous items
(elements), in which new elements are
added at one end (the rear), and
elements are removed from the other
end (the front).
A queue is a FIFO “first in, first out”
structure.
30
Queue ADT Operations
MakeEmpty -- Sets queue to an empty state.
IsEmpty -- Determines whether the queue is currently
empty.
IsFull -- Determines whether the queue is currently
full.
Enqueue (ItemType newItem) -- Adds newItem to the
rear of the queue.
Dequeue (ItemType& item) -- Removes the item at the
front of the queue and returns it in item.
31
ADT Queue Operations
Transformers
MakeEmpty change state
Enqueue
Dequeue
Observers
IsEmpty observe state
IsFull
32
Class Interface Diagram
QueType class
Private data:
QueType front
MakeEmpty rear
[MAX_ITEMS-1]
IsEmpty .
.
.
IsFull
[2]
Enqueue [1]
Dequeue items [0]
33
//--------------------------------------------------------
// CLASS DEFINITION FOR CIRCULAR QUEUE
//--------------------------------------------------------
#include "ItemType.h" // for ItemType
class QueType {
public:
QueType( );
bool IsFull( ) const;
bool IsEmpty( ) const;
void Enqueue( ItemType item );
void Dequeue( ItemType& item );
private:
int front;
int rear;
ItemType items[MAX_ITEMS];
};
34
//--------------------------------------------------------
// CLASS DEFINITION FOR CIRCULAR QUEUE cont’d
//--------------------------------------------------------
QueType::QueType( )
{
front = MAX_ITEMS - 1;
rear = MAX_ITEMS - 1;
}
bool QueType::IsEmpty( )
{
return ( rear == front )
}
35
//----------------------------------------------------
// CLASS DEFINITION FOR CIRCULAR QUEUE cont’d
//----------------------------------------------------
bool QueType::IsFull( )
{ // WRAP AROUND
return ( (rear + 1) % MAX_ITEMS == front )
}
void QueType::Enque(ItemType newItem)
{
rear = (rear+1) % MAX_ITEMS;
items[rear]= newItem;
}
36
//----------------------------------------------------
// CLASS DEFINITION FOR CIRCULAR QUEUE cont’d
//----------------------------------------------------
void QueType::Deque(ItemType& item)
{
front = (front+1)%MAX_ITEMS;
item = items[front];
}
37
//--------------------------------------------------------
// CLASS DEFINITION FOR CIRCULAR QUEUE
//--------------------------------------------------------
#include "ItemType.h" // for ItemType
class QueType {
public:
QueType( );
bool IsFull( ) const;
bool IsEmpty( ) const;
void Enqueue( ItemType item );
void Dequeue( ItemType& item );
private:
int front;
int rear;
int count;
ItemType items[MAX_ITEMS];
};
38
//--------------------------------------------------------
// CLASS DEFINITION FOR CIRCULAR QUEUE cont’d
//--------------------------------------------------------
QueType::QueType( )
{
front = MAX_ITEMS - 1;
rear = MAX_ITEMS - 1;
count = 0;
}
bool QueType::IsEmpty( )
{
return ( count == 0)
}
39
//----------------------------------------------------
// CLASS DEFINITION FOR CIRCULAR QUEUE cont’d
//----------------------------------------------------
bool QueType::IsFull( )
{
return ( count == MAX_ITEMS )
}
void QueType::Enque(ItemType newItem)
{
rear = (rear+1) % MAX_ITEMS;
items[rear]= newItem;
count++;
}
40
//----------------------------------------------------
// CLASS DEFINITION FOR CIRCULAR QUEUE cont’d
//----------------------------------------------------
void QueType::Deque(ItemType& item)
{
front = (front+1)%MAX_ITEMS;
item = items[front];
count--;
}
41