Insert a new element at the end of the queue, which is called enqueue.Introduction to the Queue data structureĪ queue is an ordered list of elements where an element is inserted at the end of the queue and is removed from the front of the queue.Ī queue works based on the first-in, first-out (FIFO) principle, which is different from a stack, which works based on the last-in, first-out (LIFO) principle. Implementation of dequeue() in C programming language −įor a complete Queue program in C programming language, please click here.Summary: in this tutorial, you will learn about the queue data structure and how to implement a JavaScript queue. Step 4 − Increment front pointer to point to the next available data element. Step 3 − If the queue is not empty, access the data where front is pointing. Step 2 − If the queue is empty, produce underflow error and exit. The following steps are taken to perform dequeue operation − Implementation of enqueue() in C programming language −Īccessing data from the queue is a process of two tasks − access the data where front is pointing and remove the data after access. Sometimes, we also check to see if a queue is initialized or not, to handle any unforeseen situations. Step 4 − Add data element to the queue location, where the rear is pointing. Step 3 − If the queue is not full, increment rear pointer to point the next empty space. Step 2 − If the queue is full, produce overflow error and exit. The following steps should be taken to enqueue (insert) data into a queue − Therefore, its operations are comparatively difficult to implement than that of stacks. Queues maintain two data pointers, front and rear. If the value of front is less than MIN or 0, it tells that the queue is not yet initialized, hence empty. If front is less than MIN OR front is greater than rear Implementation of isfull() function in C programming language − In case we maintain the queue in a circular linked-list, the algorithm will differ. Implementation of peek() function in C programming language −Īs we are using single dimension array to implement queue, we just check for the rear pointer to reach at MAXSIZE to determine that the queue is full. The algorithm of peek() function is as follows − This function helps to see the data at the front of the queue. Let's first learn about supportive functions of a queue − peek() In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or storing) data in the queue we take help of rear pointer. Isempty() − Checks if the queue is empty. Peek() − Gets the element at the front of the queue without removing it. Here we shall try to understand the basic operations associated with queues −Įnqueue() − add (store) an item to the queue.ĭequeue() − remove (access) an item from the queue.įew more functions are required to make the above-mentioned queue operation efficient. Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from the memory. For the sake of simplicity, we shall implement queues using one-dimensional array. The following diagram given below tries to explain queue representation as data structure −Īs in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and Structures. Queue RepresentationĪs we now understand that in queue, we access both ends for different reasons. More real-world examples can be seen as queues at the ticket windows and bus-stops. Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.Ī real-world example of queue can be a single-lane one-way road, where the vehicle enters first, exits first. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Unlike stacks, a queue is open at both its ends. Queue is an abstract data structure, somewhat similar to Stacks.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |