Stacks and Queues
Stacks and Queues
head_ptr
head_ptr
• What advantages does a doubly-linked list offer?
Cursor can move forwards and backwards in list.
Stacks and Queues
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];
};
Stack Implementation (cont.)
StackType::StackType()
{
top = -1;
}
void StackType::MakeEmpty()
{
top = -1;
}
bool StackType::IsEmpty() const
{
return (top == -1);
}
Stack Implementation (cont.)
bool StackType::IsFull() const
{
return (top == MAX_ITEMS-1);
}
template<class ItemType>
bool StackType<ItemType>::IsFull() const
{
return (top == MAX_ITEMS-1);
}
template<class ItemType>
void StackType<ItemType>::Push(ItemType newItem)
{
top++;
items[top] = newItem;
}
Function templates (cont.)
template<class ItemType>
void StackType<ItemType>::Pop(ItemType& item)
{
item = items[top];
top--;
}
Implementing stacks using dynamic
array allocation
template<class ItemType> private:
class StackType { int top;
public: int maxStack;
StackType(int); ItemType *items;
};
~StackType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Push(ItemType);
void Pop(ItemType&);
Implementing stacks using
dynamic array allocation (cont.)
template<class ItemType>
StackType<ItemType>::StackType(int max)
{
maxStack = max;
top = -1;
items = new ItemType[max];
}
template<class ItemType>
StackType<ItemType>::~StackType()
{
delete [ ] items;
}
Example: postfix expressions
• Postfix notation is another way of writing arithmetic
expressions.
while (!Stack.IsEmpty()) { 3 1
Stack.Pop(item);
if (item==oldItem)
2 5
1 3
tempStack.Push(newItem);
Stack
else
tempStack.Push(item);
}
while (!tempStack.IsEmpty()) {
tempStack.Pop(item); 3
Stack.Push(item);
}
oldItem = 2 5
} newItem = 5 1
What is a queue?
• It is an ordered group of homogeneous items of
elements.
• Queues have two ends:
– Elements are added at one end.
– Elements are removed from the other end.
• The element added first is also removed first
(FIFO: First In, First Out).
• Operations
– MakeEmpty
– Boolean IsEmpty
– Boolean IsFull
– Enqueue (ItemType newItem)
– Dequeue (ItemType& item) (serve and retrieve)
Enqueue (ItemType newItem)
• Function: Adds newItem to the rear of the
queue.
• Preconditions: Queue has been initialized
and is not full.
• Postconditions: newItem is at rear of queue.
Dequeue (ItemType& item)
• Function: Removes front item from queue
and returns it in item.
• Preconditions: Queue has been initialized
and is not empty.
• Postconditions: Front element has been
removed from queue and item is a copy of
removed element.
Implementation issues
• Implement the queue as a circular
structure.
• How do we know if a queue is full or
empty?
• Initialization of front and rear.
• Testing for a full or empty queue.
Make front point to the element preceding the front
element in the queue (one memory location will be
wasted).
Initialize front and rear
Queue is empty
now!!
rear == front
Queue Implementation
template<class ItemType> private:
class QueueType { int front;
public: int rear;
QueueType(int); ItemType* items;
QueueType(); int maxQue;
~QueueType(); };
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(ItemType);
void Dequeue(ItemType&);
Queue Implementation (cont.)
template<class ItemType>
QueueType<ItemType>::QueueType(int
max)
{
maxQue = max + 1;
front = maxQue - 1;
rear = maxQue - 1;
items = new ItemType[maxQue];
}
Queue Implementation (cont.)
template<class ItemType>
QueueType<ItemType>::~QueueType()
{
delete [] items;
}
Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::
MakeEmpty()
{
front = maxQue - 1;
rear = maxQue - 1;
}
Queue Implementation (cont.)
template<class ItemType>
bool QueueType<ItemType>::IsEmpty() const
{
return (rear == front);
}
template<class ItemType>
bool QueueType<ItemType>::IsFull() const
{
return ( (rear + 1) % maxQue == front);
}
Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::Enqueue
(ItemType newItem)
{
rear = (rear + 1) % maxQue;
items[rear] = newItem;
}
Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::Dequeue
(ItemType& item)
{
front = (front + 1) % maxQue;
item = items[front];
}
Queue overflow
if(!q.IsFull())
q.Enqueue(item);
Queue underflow
if(!q.IsEmpty())
q.Dequeue(item);
Example: recognizing palindromes
• A palindrome is a string that reads the same
forward and backward.
Able was I ere I saw Elba
• We will read the line of text into both a stack and
a queue.
• Compare the contents of the stack and the queue
character-by-character to see if they would
produce the same string of characters.
Example: recognizing palindromes
Example: recognizing palindromes
#include <iostream.h> cout << "Enter string: " << endl;
#include <ctype.h>
while(cin.peek() != '\\n') {
#include "stack.h"
#include "queue.h“ cin >> ch;
if(isalpha(ch)) {
int main()
{ if(!s.IsFull())
StackType<char> s; s.Push(toupper(ch));
QueType<char> q; if(!q.IsFull())
char ch; q.Enqueue(toupper(ch));
char sItem, qItem; }
}
int mismatches = 0;
Example: recognizing palindromes
while( (!q.IsEmpty()) && (!s.IsEmpty()) ) {
s.Pop(sItem);
q.Dequeue(qItem);
if(sItem != qItem)
++mismatches;
}
if (mismatches == 0)
cout << "That is a palindrome" << endl;
else
cout << That is not a palindrome" << endl;
return 0;
}
Case Study: Simulation