






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Queue Implementation of Fixed Array
Typology: Essays (high school)
1 / 11
This page cannot be seen from the preview
Don't miss anything!







Enqueuing, dequeuing etc.
Array Linked list
Queue ADT
Like customers standing in a check-out line in a store, the first customer in is the first customer served.
The Queue ADT
Insertion is done at one end, whereas deletion is performed at the other end
enqueue: insert an element at the rear of the list dequeue: delete the element at the front of the list Size: count the number of elements in the queue. Clear: to clear all the elements
Queue Implementation of Array
When enqueuing, the front index is always fixed and the rear index moves forward in the array.
Queue Implementation of Array
When enqueuing, the front index is always fixed and the rear index moves forward in the array. When dequeuing, the element at the front the queue is removed. Move all the elements after it by one position. (Inefficient!!!)
Queue Implementation of Array
When an item is enqueued, make the rear index move forward. When an item is dequeued, the front index moves by one element towards the back of the queue (thus removing the front item, so no copying to neighboring elements is needed).
XXXXOOOOO (rear) OXXXXOOOO (after 1 dequeue, and 1 enqueue) OOXXXXXOO (after another dequeue, and 2 enqueues) OOOOXXXXX (after 2 more dequeues, and 2 enqueues)
(front)
Implementation using Circular Array
OOOOO7963 4OOOO7963 (after Enqueue(4)) After Enqueue(4), the rear index moves from 3 to 4.
Queue Implementation of Fixed Array
public struct Queue // data structure for Queue
{ public int Front; public int Rear; public int[] items; }
public class QueueStructure
{ Queue queue = new Queue();
public QueueStructure(int value) // use of constructor { queue.items = new int[value]; // creating an array of given size. queue.Front = 0; queue.Rear = -1; }
Queue Class
Attributes of Queue front/rear: front/rear index counter: number of elements in the queue maxSize: capacity of the queue Operations of Queue IsEmpty: return true if queue is empty, return false otherwise IsFull: return true if queue is full, return false otherwise Enqueue: add an element to the rear of queue Dequeue: delete the element at the front of queue Display: print all the data of Queue. Size: Display the number of elements in the queue Clear: Clear all the elements in the Array.
The whole program on this is provided separately on your Moodle
Operations of Queue
public void Enqueue() {
if (queue.Rear == queue.items.Length‐1) { Console.WriteLine("Queue is full and items cannot be added.."); } else { Console.Write("Enter a number to be added in Queue : "); queue.Rear++; queue.items[queue.Rear] = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enqueue done/ Job added to the queue.."); }
Display(); }
public int Dequeue() {if (queue.Front > queue.Rear) Console.WriteLine("Queue is empty"); else Console.WriteLine("The first job in queue taken out:{0}", queue.items[queue.Front]); queue.Front++; Display(); return ‐1; }
public void Clear() { Console.WriteLine("The whole jobs is getting cleared in the Queue"); if (queue.Rear < queue.Front) { Console.WriteLine("Queue isalready Empty"); } else { queue.Front = 0; queue.Rear = ‐1; } Display(); }
public void Size() { int counter= (queue.Rear ‐ queue.Front)+1; Console.WriteLine("The number of jobs in the queue is {0}", counter);
}
Enqueue function of Queue
rear
newNode
The time complexity for Enqueue operation is O(1)
Dequeue
front
front
The whole program on this is provided separately on your Moodle
Important Link for Further studies
Queue using Stacks https://www.geeksforgeeks.org/queue-using-stacks/
Circular Queue https://www.geeksforgeeks.org/circular-queue-set-2-circular-linked-list-implementation/
Some more to practice https://www.geeksforgeeks.org/queue-data-structure/