Stack And Queues

Run Settings
LanguageJavaScript
Language Version
Run Command
// STACK AND QUEUES BOTH ARE THE LINEAR DATA STRUCTURE. // Stack : LIFO (Last In First Out) this is the data structure where last item will get out first from the stack. So suppose if you have 5 items and // then added sixth item now if you trying to remove the item then can only remove the item which is on top of list in this case sixth // item. // In stack we only have few operation that we can do like pop() => To remove item from top of the list. O(1) // push() => To add item in top of the list. O(1) // peek() => To get top item from list.O(1) // We can also have lookup operation in this because if we want but most of the time we won't need that. O(n) // Queues : FIFO (First in First Out) this is the data structure where which ever item added first will be removed first. Now its like you are // standing in line for tickets and there who are standing before you are going to get tickets before you and who are standing after you // are going to get tickets after you and same goes with zarodha order suppose if you have given order the 10 other user have given order // on same price then whoever if first going to get order completed first. // See internaly even if you talk about JRE (javascript run time enviroment) then in js engine we are using stack data structure in callstack to handle //. execution context and also for callback queue we are using queues data structure to handle process. So even this two data structure looks simple // but this can be used in many different scenarios. // Oprations that we can perform in queues : 1. lookup : O(n) 2.peek() : O(1) to get first time in queues 3. enque : O(1) add item at last in queue // 4. dequeue : O(1) remove first item from queue. O(1) // NOW THIS IS SOMETHING IMPORTANT THAT TO CREATE STACK WE ARE GOING TO USE ARRAY BECAUSE WE ONLY HAVE TO PLAY WITH LAST ITEM AND THOSE OPERATION // IN ARRAY ARE NOT EXPENSIVE. ITS O(1). BUT WHEN WE TALK ABOUT QUEUE WE NEED TO REMOVE ITEM FROM FIRST POSITION NOW IF WE USE ARRAY IN THIS THEN // EACH TIME YOU REMOVE FIRST ITEM THEN YOU HAVE TO SHIFT OTHER ITEMS ALSO AS ARRAY ARE INDEXED BASED. SO RATHER THEN USING ARRAY FOR QUEUE WE WILL // BE USING LINKED LIST BECAUSE IN QUEUES WE NEED TO PLAY WITH BOTH FIRST AND LAST ITEM AND IN LINKED LIST WE HAVE HEAD AND TAIL MANAGED WHICH IS // FIRST AND LAST ITEM ACTUALLY.
Editor Settings
Theme
Key bindings
Full width
Lines