JavaScript Program to implement a queue using an array. Linked list allocation – A queue implemented using a linked list can store/organize an unlimited number of elements.In this implementation, the queue can only store a limited number of elements. Sequential allocation – It can be implemented using an array.We can implement a queue in the following two ways. In this way, we can continue the Dequeue Operation one by one till we reset both the pointers at -1, making it an empty queue Enqueue and Dequeue Operations Implemention of Queue Data Structure Increase the Front index by 1, and the Rear stays the same.Return the element that is in the Front position.Add the new element where the Rear is pointing.In an empty stack, we set values of Front and Rear to -1.The rear pointer points to the last element.The front pointer points to the first element of the queue. The front pointer is used to access or Dequeue an element, while the rear pointer points to the new element that is added. isFull() – It checks if the queue is full.īy now you would have understood, that we need two pointers for two different functions in a queue.isEmpty() – It checks if the queue is empty.peek() – This function brings the asked element to the front-end of the queue without removing it.Dequeue() – It refers to removing or accessing an element from the front-end of a queue.Enqueue() – This is the process of adding or storing an element to the rear end(back-end) of a queue.We can perform the following operations on a queue. ( Remember, stack uses the First In Last Out principle) First In First Out Principle Basic Operations of a Queue This is at the center of the working of a queue, known as the First In First Out principle. The person standing first in the queue gets the ticket and leaves the group, while a new person joins at the back end of the queue. Think of a queue at the cinema theatre counter. We insert data at one end of the queue and remove data from the other end. It is an abstract data type, pretty similar to a stack.īut unlike stacks, we can perform operations at both ends of a queue. printQueue: To print the elements present in queue. isEmpty: To check whether an element is present in the queue or not. peek: To get the front element without removing it. dequeue() : This removes the first item from the. dequeue: To remove an element from the front of the queue. Creating a queue enqueue(element(s)) : This adds a new item (or several items) at the back of the queue. A queue is a linear data structure that contains elements in an ordered sequence. To implement a queue data structure we need the following methods: enqueue : To add elements at end of the queue.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |