Document Details

EnergySavingSydneyOperaHouse

Uploaded by EnergySavingSydneyOperaHouse

German Jordanian University

Tags

queue implementation data structures algorithms computer science

Summary

This lecture covers queue implementation in detail, including the motivation behind queues, operations like enqueue and dequeue, and implementations using both arrays and linked lists. It explains the concept of a queue as a FIFO data structure and its real-world applications.

Full Transcript

Queues CS223: Data Structures Queue Queue: A list with the restriction that insertions are done at one end and deletions are done at the other. First-In, First-Out ("FIFO”) Elements are stored in order of insertion but don't have indexes....

Queues CS223: Data Structures Queue Queue: A list with the restriction that insertions are done at one end and deletions are done at the other. First-In, First-Out ("FIFO”) Elements are stored in order of insertion but don't have indexes. Client can only add to the end of the queue, and can only examine/remove the front of the queue. Basic queue operations: add (enqueue): Add an element to the back. remove (dequeue): Remove the front element. peek: Examine the element at the front. Queues - Motivation Operating systems: queue of print jobs to send to the printer queue of programs / processes to be run queue of network data packets to send Programming: modeling a line of customers or clients storing a queue of computations to be performed in order Real world examples: people on an escalator or waiting in a line cars at a gas station (or on an assembly line) The Queue Operations A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. Front Rear The Queue Operations New people must enter the queue at the rear. It is usually called an enqueue operation. New Person Front Rear The Queue Operations When an item is taken from the queue, it always comes from the front. It is usually called a dequeue operation. Front Rear Queue – Basic Operations add(value)/ enqueue(value) places a given value at the back of queue remove()/dequeue() removes a value from the front of queue and returns it. peek() returns the front value from queue without removing it; returns null if queue is empty isFull() returns true if queue is full isEmpty() returns true if queue has no elements 7 Queue Implementation 1.Using Array 2. Using Linked List Queue Implementation using an Array A queue can be implemented with an array, as shown here. For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear).... 4 8 6 An array of integers to implement a We don't care what's in queue of integers this part of the array. Queue Implementation using an Array The easiest implementation also keeps track of the 3 size number of items in the queue and the index of the first element (at the front of the queue), the last element (at the front 0 rear).... 2 rear 4 8 6 A Dequeue Operation When an element leaves the queue, “size” is 2 size decremented, and “first” changes, too. 1 front... 2 rear 4 8 6 An Enqueue Operation When an element enters the queue, “size” is 3 size incremented, and “last” changes, too. 1 front... 3 rear 8 6 2 Queue Implementation using Array //MAXSIZE is array size const int MAXSIZE = 10; int queue [MAXSIZE]; //Initialize front and rear to -1 int front = -1, rear = -1; bool queueIsEmpty() bool queueIsFull() { { if(front < 0) if(rear == MAXSIZE – 1) return true; return true; else else return false; return false; } } Queue Implementation using Array: Peek operation int peek() { return queue[front]; } Queue Implementation using Array: enQueue Operation void enqueue(int data) { if(!queueIsFull()) { if(front == -1) { front = 0; rear = 0; } else rear = rear + 1; queue[rear] = data; } } Queue Implementation using Array: deQueue operation int dequeue() { int data = queue[front]; if(front == rear) { rear = -1; front = -1; } else front = front + 1; return data; } At the End of the Array There is special behaviour at the end of the array. For 3 size example, suppose we want to add a new element to this queue, where the last index is : 3 front 5 rear 2 6 1 At the End of the Array Neat trick: use a circular array to insert and remove items from a 4 size queue in constant time. The idea of a circular array is that the end of the array “wraps around” 3 front to the start of the array. rear 5 0 1 4 0 rear 4 6 1 2 4 2 6 1 3 2 front rear front Circular view of arrays. Queue Implementation using “Circular” Array: enQueue Operation void enqueue(int data) { if(!queueIsFull()) { if(front == -1) { front = 0; rear = 0; } else rear = (rear + 1) % MAXSIZE; queue[rear] = data; } } Queue Implementation using “Circular” Array: deQueue Operation int dequeue() { int data = queue[front]; If(front == rear) { rear = -1; front = -1; } else front = (front + 1) % MAXSIZE; return data; } Queue Implementation using “Circular” Array: IsFull Operation bool queueIsFull() { if((rear+1) % MAXSIZE == front) return true; else return false; } Note: Using “circular” array, you can use the same implementation of the “isEmpty” and “peek” operations used with the “regular” array Queue Implementation using an Array Easy to implement size 3 But it has a limited capacity with a fixed array Special behaviour is needed when the rear reaches the end of front the array. 0 [ 0 ]... 2 rear 4 8 6 Queue Implementation 1.Using Array 2. Using Linked List Queue Implementation using a Linked List struct Node { int data; From a linked list implementation struct Node *next; }; Node *front=NULL; New pointers for Queue Node *rear=NULL; implementation Queue Implementation using a Linked List A queue can also be implemented using a linked list with both a head and a tail pointer. 13 10 15 7 null front rear Queue Implementation using Linked List: enQueue Operation void enqueue(int data) { Node* newNode = new Node; newNode->data = data; newNode->next = NULL; if(front == NULL) { front = newNode; rear = newNode; } else { rear->next = newNode; rear = newNode; } } Queue Implementation using Linked List: deQueue Operation int deQueue() { Node * temp = front; int x = front->data;// or int x = temp -> data; front = front -> next; if(front == NULL) rear = NULL; delete temp; return x; } Queue Implementation using Linked List: peek Operation int peek() { return front->data; } Queue: Circular Array vs. Linked List Circular Array Linked List May waste unneeded Always just enough space space or run out of space But more space per Space per element element excellent Operations very simple / Operations very simple / fast fast

Use Quizgecko on...
Browser
Browser