Data Structure Queue, Essays (high school) of Computer science

Queue Implementation of Fixed Array

Typology: Essays (high school)

2017/2018

Uploaded on 03/04/2022

joshua13
joshua13 🇬🇧

11 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
24/10/2018
1
Data Structure:
Queue
LECTURE 3
Queue Overview
Queue ADT
Basic operations of queue
Enqueuing, dequeuing etc.
Implementation of queue
Array
Linked list
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Data Structure Queue and more Essays (high school) Computer science in PDF only on Docsity!

Data Structure:

Queue

LECTURE 3

Queue Overview

 Queue ADT

 Basic operations of queue

 Enqueuing, dequeuing etc.

 Implementation of queue

 Array  Linked list

Queue ADT

 Like a stack, a queue is also a list. However, with a queue,

insertion is done at one end, while deletion is performed at

the other end.

 Accessing the elements of queues follows a First In, First Out

(FIFO) order.

 Like customers standing in a check-out line in a store, the first customer in is the first customer served.

The Queue ADT

 Another form of restricted list

 Insertion is done at one end, whereas deletion is performed at the other end

 Basic operations:

 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

 First-in First-out (FIFO) list

Queue Implementation of Array

 There are several different algorithms to implement

Enqueue and Dequeue

 Naïve way

 When enqueuing, the front index is always fixed and the rear index moves forward in the array.

front

rear

Enqueue(3)

front

rear

Enqueue(6)

front

rear

Enqueue(9)

Queue Implementation of Array

 Naïve way

 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!!!)

Dequeue()

front

rear

Dequeue() Dequeue()

front

rear

rear = -

front

Queue Implementation of Array

 Better way

 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)

The problem here is that the rear index cannot move beyond the

last element in the array.

Implementation using Circular Array

 Using a circular array

 When an element moves past the end of a circular array, it wraps

around to the beginning, e.g.

 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; }

Enqueue()

Operation

Dequeue()

Operation

Clear() Operation

Size() Operation

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/