4-ListStackQueue (2)
4-ListStackQueue (2)
Chapter - 17
Introduction
• Data can be organized and processed
sequentially using an array, called a
sequential list
• Problems with an array
– Array size is fixed
– Unsorted array: searching for an item is slow
– Sorted array: insertion and deletion is slow
• Example:
Chapter - 18
Stacks
• Stack: list of homogenous elements
– Addition and deletion occur only at one end,
called the top of the stack
• Example: in a cafeteria, the second tray can be
removed only if first tray has been removed
– Last in first out (LIFO) data structure
• Operations:
– Push: to add an element onto the stack
– Pop: to remove an element from the stack
43
Algorithm for POP operation
– Check if the stack is empty or not.
– If the stack is empty, then print error of
underflow and exit the program.
– If the stack is not empty, then print the
element at the top and decrement the top.
63
Applications of Queue
• Serving requests on a single shared resource,
like a printer, CPU task scheduling etc.
64
Queue Implementation
• Queue can be implemented using an Array, or
Linked List. The easiest way of implementing a
queue is by using an Array.
65
66
• When we remove an element from Queue, we
can follow two possible approaches (mentioned
[A] and [B] in above diagram). In [A] approach,
we remove the element at head position, and
then one by one shift all the other elements in
forward position.
67
• In approach [A] there is an overhead of shifting
the elements one position forward every time we
remove the first element.
77
• In a circular queue, data is not actually removed
from the queue. Only the head pointer is
incremented by one position when dequeue is
executed. As the queue data is only the data
between head and tail, hence the data left
outside is not a part of the queue anymore,
hence removed.
83
• This can be controlled either by checking every
time whether tail or head have reached
the maxSize and then setting the value 0 or, we
have a better way, which is, for a value x if we
divide it by 8, the remainder will never be greater
than 8, it will always be between 0 and 0, which
is exactly what we want.
• So the formula to increment
the head and tail pointers to make them go
round and round over and again will be:
head = (head+1) % maxSize
tail = (tail+1) % maxSize 84
Implementation of Circular Queue
• Initialize the queue, with size of the queue
defined (maxSize), and head and tail pointers.
89
// function dequeue - to remove data from queue
int CircularQueue :: dequeue()
{
int y;
if(isEmpty())
{
cout << "Queue is empty" << endl;
}
else
{
y = a[front];
if(front == rear)
{
// only one element in queue, reset queue after
removal
front = -1;
rear = -1;
}
else
{
front = (front+1) % SIZE;
}
return(y); 90
}
void CircularQueue :: display()
{
/* Function to display status of Circular Queue */
int i;
if(isEmpty())
{
cout << endl << "Empty Queue" << endl;
}
else
{
cout << endl << "Front -> " << front;
cout << endl << "Elements -> ";
for(i = front; i != rear; i= (i+1) % SIZE)
{
cout << a[i] << "\t";
}
cout << a[i];
cout << endl << "Rear -> " << rear;
}
}
91
int CircularQueue :: size()
{
if(rear >= front)
{
return (rear - front) + 1;
}
else
{
return (SIZE - (front - rear) + 1);
}
}
// the main function
int main()
{
CircularQueue cq;
cq.enqueue(10);
cq.enqueue(100);
cq.enqueue(1000);
cout << endl << "Size of queue: " << cq.size();
cout << endl << "Removed element: " << cq.dequeue();
cq.display();
system("pause");
return 0;
} 92