

It follows the FIFO (First In First Out) order to store the elements, which means the element that is inserted first will come out first.Ģ. It follows the LIFO (Last In First Out) order to store the elements, which means the element that is inserted last will come out first.ġ. Difference between Stack and Queue Data Structuresġ. However, there are other types of queue that give the facility to insert and delete from both ends of the queue. The time complexity for insertion and deletion in the queue is O(1). Therefore, an element is inserted into a queue using the rear pointer, whereas the element is deleted using the front pointer.


The rear points to the last element, whereas the front points to the first element that is inserted into the queue. There are two pointers in a queue, known as the rear and front. However, insertion into a queue is called enqueue, whereas deletion from a queue is called dequeue. Similar to Stacks, they are also used to perform insertions, and deletions, find the peak element, and check whether the queue is empty or not. Therefore, it follows the FIFO order to process the elements. It can also only store similar kinds of elements in the queue.Īn example of a queue can be a line or a queue in a bank in which the person who comes first will be served first. However, the queue data structure follows a FIFO (First In First Out) order to store and retrieve elements from the memory. What Is a Queue Data Structure?Ī queue is also a non-primitive, linear data structure that stores elements in a sequential manner. You can refer to the article Stacks in Data Structure for more information about stacks. Insertion and deletion in the stack have a time complexity of O(1). It has a single pointer called top which points to the topmost element of the stack and insertion as well as deletion is performed by manipulating the top pointer only. However, the insertion and deletion from the stack could be done from one end only. The insertion in a stack is called a push, whereas the deletion in a stack is called a pop. There are multiple operations that are performed on the stack elements like insertion, deletion, finding the peek element, and determining whether the stack is empty or not. Therefore, it follows the Last In First Out order. However, the topmost book, which would have been kept at last, is the one that will be removed first from the pile. You can imagine how the books would be placed on top of each other. However, the stack can only be used to store similar kinds of elements, that is, those that have the same data types.Īn example of a stack data structure could be a pile of books that are kept on a table. Stack and Queue are non-primitive, linear data structures.Ī stack is a non-primitive, linear data structure that stores the elements in a LIFO (Last In First Out) order.We'll take a deepr look at the difference between stack and queue. Stack follows the LIFO (Last In First Out) order to store the elements whereas the Queue follows the FIFO (First In First Out) order to store the elements in the memory. Both of them are linear data structures, which means they store and retrieve the elements in a sequencial manner from the structure when required. There are many data structures like arrays, linked lists, and trees that are used to organise the data in a particular manner.Ī stack and a queue are two other data structures that are used to organise data.
