Ticker

6/recent/ticker-posts

Queue Part - Data Structure | Learn More CSE

 Queues -Data Structure

Introduction

  • Like Stack, the queues is also an abstract data type.
  • As the name suggest, in queues elements are inserted at one end while deletion takes place at the other end.
  • Queues are open at both ends, unlike stacks that are open only one end(the top).
Let us consider a queue at a counter:




  • Here, the person who comes first in the queue is served first with the ticket while the new seekers of tickets are added back in the line. 
  • This order is known as ​First In First Out (FIFO)​. 
  • In programming terminology, the operation to add an item to the queue is called "enqueue", whereas removing an item from the queue is known as "dequeue".

Working of A Queue :

Queue operations work as follows: 

1. Two pointers called ​FRONT ​and ​REAR ​are used to keep track of the first and last elements in the queue. 

2. When initializing the queue, we set the value of FRONT and REAR to -1. 

3. On ​enqueuing ​an element, we increase the value of the REAR index and place the new element in the position pointed to by REAR. 

4. On ​dequeuing ​an element, we return the value pointed to by FRONT and increase the FRONT index. 

5. Before enqueuing, we check if the queue is already full. 

6. Before dequeuing, we check if the queue is already empty. 

7. When enqueuing the first element, we set the value of FRONT to 0. 8. When dequeuing the last element, we reset the values of FRONT and REAR to -1.


Implementation of A Queue Using Array

A Queue contains majorly these five functions that we will be implementing: 

Main Queue Operations 

• ​enqueue(int data): Inserts an element at the end of the queue 

​int dequeue(): Removes and returns the element at the front of the queue 

Auxiliary Queue Operations 

​int front(): Returns the element at the front without removing it 

• ​int size(): Returns the number of elements stored in the queue 

• ​int IsEmpty(): Indicates whether no elements are stored in the queue or not Now, let’s  implement these functions in Python. Follow up the code along with the comments below:







Post a Comment

0 Comments