|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "#### Author: OMKAR PATHAK" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "## What is a Queue?" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "* A queue is a data structure used for storing data (similar to Linked Lists and stacks). In queue, the order in which dula arrives is important.\n", |
| 22 | + "* A queue is an ordered list in which insertions are done at one end (rear) and deletions are done at other end (front). The first element to be inserted is the first one to be deleted. Hence, it is called First in First out (FIFO) or Last in Last out (LILO) list.\n", |
| 23 | + "* For example, our labs have 30 computers networked with a single printer. When students want to print, their print tasks “get in line” with all the other printing tasks that are waiting. The first task in is the next to be completed." |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "### Applications of Queue:" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "metadata": {}, |
| 36 | + "source": [ |
| 37 | + "* When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling.\n", |
| 38 | + "* When data is transferred asynchronously (data not necessarily received at same rate as sent) between two processes. Examples include IO Buffers, pipes, file IO, etc.\n", |
| 39 | + "* Keyboard: As we type, sometimes keystrokes get ahead of the characters that appear on the screen. This is due to the computer doing other work at that moment. The keystrokes are being placed in a queue-like buffer so that they can eventually be displayed on the screen in the proper order." |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": 18, |
| 45 | + "metadata": {}, |
| 46 | + "outputs": [ |
| 47 | + { |
| 48 | + "name": "stdout", |
| 49 | + "output_type": "stream", |
| 50 | + "text": [ |
| 51 | + "0 1 2 3 4 5 6 7 8 9\n", |
| 52 | + "Queue Size: 10\n", |
| 53 | + "0 1 2 3 4 5 6 7 8\n", |
| 54 | + "Queue Size: 9\n" |
| 55 | + ] |
| 56 | + } |
| 57 | + ], |
| 58 | + "source": [ |
| 59 | + "class Queue(object):\n", |
| 60 | + " def __init__(self, limit = 10):\n", |
| 61 | + " self.queue = []\n", |
| 62 | + " self.front = None\n", |
| 63 | + " self.rear = None\n", |
| 64 | + " self.limit = limit\n", |
| 65 | + " self.size = 0\n", |
| 66 | + " \n", |
| 67 | + " def __str__(self):\n", |
| 68 | + " return ' '.join([str(i) for i in self.queue])\n", |
| 69 | + " \n", |
| 70 | + " # to check if queue is empty\n", |
| 71 | + " def isEmpty(self):\n", |
| 72 | + " return self.size <= 0\n", |
| 73 | + " \n", |
| 74 | + " # to add an element from the rear end of the queue\n", |
| 75 | + " def enqueue(self, data):\n", |
| 76 | + " if self.size >= self.limit:\n", |
| 77 | + " return -1 # queue overflow\n", |
| 78 | + " else:\n", |
| 79 | + " self.queue.append(data)\n", |
| 80 | + " \n", |
| 81 | + " # assign the rear as size of the queue and front as 0\n", |
| 82 | + " if self.front is None:\n", |
| 83 | + " self.front = self.rear = 0\n", |
| 84 | + " else:\n", |
| 85 | + " self.rear = self.size\n", |
| 86 | + " \n", |
| 87 | + " self.size += 1\n", |
| 88 | + " \n", |
| 89 | + " # to pop an element from the front end of the queue\n", |
| 90 | + " def dequeue(self):\n", |
| 91 | + " if self.isEmpty():\n", |
| 92 | + " return -1 # queue underflow\n", |
| 93 | + " else:\n", |
| 94 | + " self.queue.pop()\n", |
| 95 | + " self.size -= 1\n", |
| 96 | + " if self.size == 0:\n", |
| 97 | + " self.front = self.rear = 0\n", |
| 98 | + " else:\n", |
| 99 | + " self.rear = self.size - 1\n", |
| 100 | + " \n", |
| 101 | + " def getSize(self):\n", |
| 102 | + " return self.size\n", |
| 103 | + "\n", |
| 104 | + "myQueue = Queue()\n", |
| 105 | + "for i in range(10):\n", |
| 106 | + " myQueue.enqueue(i)\n", |
| 107 | + "print(myQueue)\n", |
| 108 | + "print('Queue Size:',myQueue.getSize())\n", |
| 109 | + "myQueue.dequeue()\n", |
| 110 | + "print(myQueue)\n", |
| 111 | + "print('Queue Size:',myQueue.getSize())" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "markdown", |
| 116 | + "metadata": {}, |
| 117 | + "source": [ |
| 118 | + "### Time Complexities:" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "markdown", |
| 123 | + "metadata": {}, |
| 124 | + "source": [ |
| 125 | + "* enqueue(): O(1)\n", |
| 126 | + "* dequeue(): O(1)\n", |
| 127 | + "* isEmpty(): O(1)\n", |
| 128 | + "* getSize(): O(1)" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "markdown", |
| 133 | + "metadata": {}, |
| 134 | + "source": [ |
| 135 | + "### Limitations:" |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "cell_type": "markdown", |
| 140 | + "metadata": {}, |
| 141 | + "source": [ |
| 142 | + "* The maximum size of the queue must be defined prior to the execution and cannot be changed" |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "markdown", |
| 147 | + "metadata": {}, |
| 148 | + "source": [ |
| 149 | + "### Extensions of Queue:" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "markdown", |
| 154 | + "metadata": {}, |
| 155 | + "source": [ |
| 156 | + "* Deque (Double ended queue): Supports insert and delete at both the ends (front as well as rear) of the queue.\n", |
| 157 | + "* Circular Queue: A queue in which last position is connected back to front\n", |
| 158 | + "* Priority Queue: Elements are added based on their priorities (higher priority elements are popped first)" |
| 159 | + ] |
| 160 | + } |
| 161 | + ], |
| 162 | + "metadata": { |
| 163 | + "kernelspec": { |
| 164 | + "display_name": "Python 3", |
| 165 | + "language": "python", |
| 166 | + "name": "python3" |
| 167 | + }, |
| 168 | + "language_info": { |
| 169 | + "codemirror_mode": { |
| 170 | + "name": "ipython", |
| 171 | + "version": 3 |
| 172 | + }, |
| 173 | + "file_extension": ".py", |
| 174 | + "mimetype": "text/x-python", |
| 175 | + "name": "python", |
| 176 | + "nbconvert_exporter": "python", |
| 177 | + "pygments_lexer": "ipython3", |
| 178 | + "version": "3.5.2" |
| 179 | + } |
| 180 | + }, |
| 181 | + "nbformat": 4, |
| 182 | + "nbformat_minor": 2 |
| 183 | +} |
0 commit comments