Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

sfg

Sal F. Gambino
CS-342

C++ CS 342

Data Structures
&
Queues 1
Lists- QUEUE
sfg
Sal F. Gambino
CS-342

A QUEUE is a list such that entries can only be


inserted at one end (Rear or Back) and removed
at the other end. ( Top or Front)

operations:
•ENQUEUE insertAtBack
•DEQUEUE removeFromFront
•isQueueEmpty
•getTop returns the top data item
•isFull usually used with static arrays

FIFO First In First Out


2
sfg
Lists- QUEUE Sal F. Gambino
CS-342

Implementation:

1. Dynamic: Pointers
Restricted Linked List
2. Static: Arrays
Partially Filled
Circular Arrays
3
Lists- QUEUE
sfg
Sal F. Gambino
Applications: CS-342
1. Recognizing Palindromes (words forward/back, same)
2. Printing queues (spools)
3. Buffering input (keyboard input)
4. Simulation Programs (lines, waiting for traffic lite)
5. Echoing a word (read ch enqueue, write Q)
6. A line (bank teller, drive-in window)
7. A line (busy baker’s shop, take a ticket #)
8. Phone calls to large companies are generally
placed on a Q when all operators are Busy

and many more…..


4
Lists- QUEUE sfg
Sal F. Gambino
Applications: Recognizing Palindromes
CS-342
A palindrome is a string that reads the same
forwards and backwards Convert to uppercase
ex: RADAR, BOB, etc. toUPPER()
ABLE WAS I ERE I SAW ELBA check only alpha
ALGORITHM: isALPHA()
1. Read the characters into a STACK & QUEUE
2. Traverse the STACK & QUEUE and compare
the characters of the stack and queue.
If the characters Do NOT MATCH Increment
a counter.
3. At end, If counter == 0 “string is a Palindrome”
otherwise “string is Not a Palindrome“
5
Lists- QUEUE
sfg
Sal F. Gambino
CS-342

Applications: More Complex applications:


These applications require more than a regular Q

9. Simple topological Sort on a Directed acyclic graph


10. Computer Networks: File Servers users are given
access to files on a first come first served basis
11. Priority Queue vs Regular Queue (later!)
12. Airport ( Take-off Q, Landing Q)
13. Scheduling I/O request on a disk device

and many more ………... 6


Lists- QUEUE
sfg
Sal F. Gambino
CS-342

Implementation:
1. Pointers ………Linked List notes

2. Partially Filled Circular Arrays


•Declare an array and pick the CAPACITY
of the queue (size)
•Use a counter variable to keep track of the
number of items on the queue. (COUNT)
•check for empty-> COUNT == 0
•check for Full----> COUNT == CAPACITY
7
Lists- QUEUE
sfg
Sal F. Gambino
Implementation: CS-342
2. Partially Filled Circular Arrays: continued…

Use two variables to keep track of the Front


and Back of the queue
•Front------------------> first Circular array
•Back-------------------> last (wrap around)
To advance first or last use the following: (-1 + 1) % 4 = 0
( 0 + 1) % 4 = 1
•first = (first + 1) % CAPACITY ( 1 + 1) % 4 = 2
•last = (last + 1) % CAPACITY ( 2 + 1) % 4 = 3
( 3 + 1) % 4 = 0
•getTop--------------------> return queue[first]
•printQueue (start at first, end at last)
8
sfg
Lists- QUEUE, ARRAY insert
Sal F. Gambino
insert:( Partially Filled Circular Arrays: continued…) CS-342
If ( count == 0 )
{ count++; Starting
first = 0; values:
last = 0; first = -1;
cin >> Q[0]; last = -1;
} count = 0;
else if ( count < CAPACITY )
{ last = (last +1) % CAPACITY;
cin >> Q[last];
count++;
}
else
“Q is FULL cannot add” 9
sfg
Lists- QUEUE, ARRAY remove
Sal F. Gambino
remove:( Partially Filled Circular Arrays: continued…) CS-342
If ( count == 0 )
{ “Q is empty cannot remove”
}
else if ( count == 1 )
RESET
{ last = -1;
QUEUE
first = -1;
count = 0;
}
else if ( count <= CAPACITY )
{ first = (first +1) % CAPACITY;
count--;
}
10
INSERT & REMOVE sfg
If ( count == 0 ) If ( count == 0 )
{ count++; first = 0; last = 0; Sal F. Gambino
{ “Q is empty cannot remove”
cin >> Q[0]; } CS-342
} else if ( count == 1 )
else if ( count < CAPACITY ) { last = -1; first = -1; count = 0;
{ last = (last +1) % CAPACITY; }
cin >> Q[last]; else if ( count <= CAPACITY )
count++; { first = (first +1) % CAPACITY;
} count- -;
else “Q is FULL cannot add” }

0 4
COUNT CAPACITY

-1
0 1 2 3 FIRST

-1
11
LAST
INSERT & REMOVE sfg
If ( count == 0 ) If ( count == 0 )
{ count++; first = 0; last = 0; Sal F. Gambino
{ “Q is empty cannot remove”
cin >> Q[0]; } CS-342
} else if ( count == 1 )
else if ( count < CAPACITY ) { last = -1; first = -1; count = 0;
{ last = (last +1) % CAPACITY; }
cin >> Q[last]; else if ( count <= CAPACITY )
count++; { first = (first +1) % CAPACITY;
} count- -;
else “Q is FULL cannot add” }

0 4
COUNT CAPACITY

-1
0 1 2 3 FIRST

-1
12
LAST
INSERT & REMOVE sfg
If ( count == 0 ) If ( count == 0 )
{ count++; first = 0; last = 0; Sal F. Gambino
{ “Q is empty cannot remove”
cin >> Q[0]; } CS-342
} else if ( count == 1 )
else if ( count < CAPACITY ) { last = -1; first = -1; count = 0;
{ last = (last +1) % CAPACITY; }
cin >> Q[last]; else if ( count <= CAPACITY )
count++; { first = (first +1) % CAPACITY;
} count- -;
else “Q is FULL cannot add” }

0 4
COUNT CAPACITY

-1
0 1 2 3 FIRST

-1
13
LAST
INSERT & REMOVE sfg
If ( count == 0 ) If ( count == 0 )
{ count++; first = 0; last = 0; Sal F. Gambino
{ “Q is empty cannot remove”
cin >> Q[0]; } CS-342
} else if ( count == 1 )
else if ( count < CAPACITY ) { last = -1; first = -1; count = 0;
{ last = (last +1) % CAPACITY; }
cin >> Q[last]; else if ( count <= CAPACITY )
count++; { first = (first +1) % CAPACITY;
} count- -;
else “Q is FULL cannot add” }

0 4
COUNT CAPACITY

-1
0 1 2 3 FIRST

-1
14
LAST
#ifndef queue_h DERIVED class sfg
#define queue_h
#include "list.h" Sal F. Gambino
template<class QUEUETYPE> BASE classCS-342
class Queue : private List<QUEUETYPE>
{
Inheritance: class Queue will inherit from class List
Queue can access the PUBLIC & PROTECTED
members NOT the PRIVATE members of List
public:
void enqueue(const QUEUETYPE &d ) { insertAtBack(d); }
int dequeue (QUEUETYPE &d ) { return removeFromFront(d);}
int isQueueEmpty()const { return isEmpty(); }
void printQueue() const { print(); }
}; 15
#endif
#ifndef queue_h Includes: sfg
#define queue_h
List--> ListNode Sal F. Gambino
#include "list.h"
template<class QUEUETYPE> CS-342

class Queue : private List<QUEUETYPE>


{
Queue functions Inherited
The derived class contains List functions
parts of the BASE class
and new ones
public:
void push(const QUEUETYPE &d ) { insertAtBack(d); }
int pop (QUEUETYPE &d ) { return removeFromFront(d);}
int isQueueEmpty()const { return isEmpty(); }
void print Queue() const { print(); }
}; 16
#endif
#include <iostream.h>
sfg
#include ”queue.h" Sal F. Gambino
CS-342
void main()
{ int DQint; Queue<int> intQueue;
for (int i=0; i< 4; i++)
{ intQueue.enqueue(i);
cout << i << ”Enqueued!";
intQueue.printQueue();
}
while ( ! intQueue.isQueueEmpty() )
{ intQueue.dequeue(DQint);
cout << DQint << " Dequeued! \n" ;
intQueue.printQueue();
}} 17
sfg
processing an integer Queue... Sal F. Gambino
CS-342

The list is empty The list is ..0 1 2 3


0 enqueued 0 dequeued
The list is ..0 The list is ..1 2 3
1 enqueued 1 dequeued
The list is ...0 1 The list is ..2 3
2 enqueued 2 dequeued
The list is …0 1 2 The list is ..3
3 enqueued 3 dequeued
The list is ..0 1 2 3 The list is empty..
All nodes destroyed
18

You might also like