You are on page 1of 16

Lecture Queue

By: Saeed Ahmed


Queue Data Structure:
Queue is a linear data structure which operates in First IN First OUT or
Last IN Last Out
 It is named queue as it behaves like a real-world queue, for example - queue(line) of cars
in a single lane,
queue of people waiting at food counter etc.
 Queue is an abstract data type with bounded
(predefined) capacity
 it is a simple data structure that allows
adding and removing elements in a particular order

 The order is FIFO(First In First Out) or LILO (Last In Last Out)


Queue Data Structure:
Queue is a liner data structure which operates in a FIFO(First In First Out) and LILO(Last In
Last Out) pattern
Elements are added at one end(rear end) .This is called Enqueue
Elements are removed from the other end (front/head end). This is called Dequeue
Implementation of Queue Data Structure
Queue can be implemented using an Array, Stack. The easiest way of implementing a queue is
by using an Array.

Initially the head(FRONT) and the tail(REAR) of the queue points at the first index of the
array (starting the index of array from 0). As we add elements to the queue, the tail keeps on
moving ahead, always pointing to the position where the next element will be inserted, while
the head remains at the first index.
Implementation of Queue Data Structure
Implementation of Queue Data Structure
Implementation of Queue Data Structure
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.

In approach [B] we remove the element from head position and then move head to the next
position.

In approach [A] there is an overhead of shifting the elements one position forward every
time we remove the first element.

In approach [B] there is no such overhead, but whenever we move head one position ahead,
after removal of first element, the size on Queue is reduced by one space each time.
Queue Data Structure Operations:
 enqueue(): Elements are added from one end which is called Rare/Back

 dequeue(): Elements are removed from one end which is called Head/Front.

 isEmpty(): Tells if the queue is empty or not.

 Isfull(): Tells if the queue is full or not.

 Count(): Get the number of items in the queue


Some Application Queue Data Structure:
 Queue is used when things but have to be processed in First In First Out order. Like –

 CPU scheduling, Disk Scheduling.


 Handling of interrupts in real-time systems. The interrupts are handled in the same order
as they arrive, First come first served.
 In real life, Call Center phone systems will use Queues, to hold people calling them in
an order, until a service representative is free.
 When data is transferred asynchronously between two processes. Queue is used for
synchronization.
Linked List Data Structure :
 A linked list is a linear data structure, in which the elements are not stored at contiguous
memory locations. The elements in a linked list are linked using pointers(entity that point
to the next element)
 In simple words, a linked list consists of nodes where each node contains a data field and a
reference(link) to the next node in the list.
Linked List Data Structure :
 Linked List contains a link element called first.
 Linked list consist of nodes where each node contains a data field and a reference(link
called next) to the next node in the list.
 Node/Link/Element/Object-
Each node in the linked list consist of two part
 data
 link to the next node

 Next - This points to the next node/element in the linked list(since they are not stored in a
contiguous memory locations)
Linked List Data Structure :

Link field is called next and each link is linked with its next link. Last link carries a link to null
to mark the end of the list.

Note: Head is not a separate node but it is a reference to the first node. If the list is empty, the
head is a null reference.

Linked list is a dynamic data structure. While accessing a particular item, start at the head and
follow the references until you get that data item.
Linked List Data Structure :
Advantages of Linked Lists
 They are a dynamic in nature which allocates the memory when required.
 Insertion and deletion operations can be easily implemented.

Disadvantages of Linked Lists


 The memory is wasted as pointers require extra memory for storage.
 No element can be accessed randomly; it has to access each node sequentially.
 Reverse Traversing is difficult in linked list.
Some Standard Linked List Operations
 Traverse – Iterate through the nodes in the linked list starting from the head node.
 Append – Attach a new node (to the end) of a list
 Prepend – Attach a new node (to the beginning) of the list
 Insert – attach a new node to a specific position on the list
 Delete – Remove/Delink a node from the list
 Count – Returns the no of nodes in linked list
Some Applications of Linked List DS
 Linked Lists can be used to implement Stacks , Queues.
 Linked Lists can also be used to implement Graphs. (Adjacency list representation of
Graph).
 Implementing Hash Tables :- Each Bucket of the hash table can itself be a linked list.
(Open chain hashing).
 Undo functionality in Photoshop or Word . Linked list of states.

You might also like