Тёмный

JavaScript Data Structures - 10 - Queue Implementation Optimised 

Codevolution
Подписаться 670 тыс.
Просмотров 29 тыс.
50% 1

⚡️ Code with me on Replit - join.replit.com...
⚡️ View and edit the source code on Replit - bit.ly/3umsOHU
📘 Courses - learn.codevolu...
💖 Support UPI - support.codevo...
💖 Support Paypal - www.paypal.me/...
💾 Github - github.com/gop...
📱 Follow Codevolution
Twitter - / codevolutionweb
Facebook - / codevolutionweb
📫 Business - codevolution.business@gmail.com
Queue Implementation Optimised
JavaScript Data Structures
Data Structures in JavaScript

Опубликовано:

 

4 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 26   
@awaisfiaz8186
@awaisfiaz8186 3 месяца назад
Excellent
@sumitkumarsharma4004
@sumitkumarsharma4004 2 года назад
There is small bug on this implementation. I have written a piece of code which will give inappropriate result. queue.enqueue("data1") queue.dequeue() queue.dequeue() qeue.enqueue("data1"); queue.dequeue(); To Solve this issue just need to apply conition in dequeue method : const item = this.items[this.front]; if(!item) return // this is the line to correct the bugs delete this.items[this.front]; this.front++; return item;
@naveenrajukopparthi3046
@naveenrajukopparthi3046 Год назад
That's Nice, Thank you.
@Ayoubmajid-uu9yv
@Ayoubmajid-uu9yv 7 месяцев назад
tr@Winter_Wyvern1 try this : dequeue() { if (this.isEmpty()) { console.log("you should add elements first then dequeue"); } if (this.front == this.rear) { this.front = this.rear = -1; this.arrQueue.length = 0; } else { return this.arrQueue[this.front++]; } }
@timetosleep8055
@timetosleep8055 6 месяцев назад
you will get undefined if the first item in the list is the number 0. Use the isEmpty function instead.
@omobrain1707
@omobrain1707 Год назад
thak you
@premsingh6967
@premsingh6967 Год назад
@kristijanlazarev
@kristijanlazarev Год назад
Lovely
@amitbairagya2496
@amitbairagya2496 2 года назад
Does JavaScript have any in-building queue data structure, like cpp has
@zeeshanahmadkhan810
@zeeshanahmadkhan810 2 года назад
There are methods like push pop shift unshift in JavaScript which can be used to make arrays behave like queues and stacks
@alvincodes
@alvincodes Год назад
No. You have to use build your queue
@alvincodes
@alvincodes Год назад
@Dragon sure.
@jamesblock8384
@jamesblock8384 Год назад
Is it a problem that the head and tail never reset? Obviously it would defeat the purpose if you had to update every key value pair at enqueue or dequeue. That begin said, depending on your use case it might not be a bad idea to have a private reset method that resets the head and tail to 0 if the queue ever completely empties.
@vincentcaruso5494
@vincentcaruso5494 7 месяцев назад
Yes it could be a problem - if the pointers "left" or "right" become larger than the largest possible 64-bit number allowed in JS (1.7976931348623157e+308) they will become represented as infinity, so this implementation would stop working at that point
@mahmodsamir5110
@mahmodsamir5110 6 месяцев назад
you are right, good thinking and that will make it looks like an array with better time complexity
@rs4267
@rs4267 Год назад
Size isnt equal to tail - head +1 ??
@luckyoniovosa2148
@luckyoniovosa2148 6 месяцев назад
this is my implementation, not sure if this works for queue class Queue { constructor() { this.items = {}; this.size = 0; } enqueue(element) { this.items[this.size] = element; this.size++; } dequeue() { if (this.size) { const item = this.items[0]; delete this.items[0]; this.size--; return item; } } peek() { return this.items[0]; } isEmpty() { return this.size === 0; } size() { return this.size; } clear() { this.items = {}; this.size = 0; } print() { console.log(this.items); } }
@tbm1895
@tbm1895 4 месяца назад
As javascript treats 0 as false you cannot delete the element at position 0 using the dequeue method.
@vickydhakar3238
@vickydhakar3238 2 года назад
Don't you think so in this way the length of array will be increased?. All the elements from start will be assigned to undefined and size of array will increase by adding new values?
@shawnmichaelalberto7519
@shawnmichaelalberto7519 2 года назад
it is an object not an array. i had the same confusion before.
@naveenrajukopparthi3046
@naveenrajukopparthi3046 Год назад
Yeah, that's why circular queues came!
@mahmodsamir5110
@mahmodsamir5110 6 месяцев назад
what do you mean by the length of array ? where is the array here
@alhassankiwamdeen7252
@alhassankiwamdeen7252 2 года назад
Excerllent
@sandeepbhattacharjee4512
@sandeepbhattacharjee4512 9 месяцев назад
There is no check in dequeue for rear >= front. Hence, if we try to dequeue an empty object, won't it cause error and set the pointers wrongly.
@rdrahuldhiman19
@rdrahuldhiman19 9 месяцев назад
I smell bugs.
Далее
Women’s Celebrations + Men’s 😮‍💨
00:20
Просмотров 3,8 млн
Китайка стучится Домой😂😆
00:18
Linked List Data Structure | JavaScript
29:36
Просмотров 207 тыс.
Dependency Injection, The Best Pattern
13:16
Просмотров 833 тыс.