r/ProgrammerHumor • • 13d ago

Meme queueIsAlwaysLong

Post image
241 Upvotes

48 comments sorted by

View all comments

Show parent comment

3

u/pencilUserWho 13d ago

Double linked lists are easier.

8

u/RedCrafter_LP 13d ago

Double linked list is the most common implementation of the deque concept. A double linked list is a deque.

2

u/pencilUserWho 13d ago

Yes, I know. I should have said "working with double linked lists (for deque) is easier than working with single linked lists (like for stack or queue)"

Sorry for the confusionĀ 

1

u/braaaaaaainworms 12d ago

Why would you want to work with a linked list?

1

u/pencilUserWho 12d ago

To implement a stack

1

u/braaaaaaainworms 12d ago

You can just... make a struct with "amount of elements in array" and "capacity" ints and a pointer to a memory region, it'll be faster than linked lists, a lot faster

1

u/pencilUserWho 12d ago

That way it can overflow.

1

u/braaaaaaainworms 12d ago

Just check, before pushing, if capacity and len are equal, if yes, use realloc to make the memory region bigger

1

u/Nightmoon26 12d ago

This is how some libraries probably implement it by default. But in cases where memory efficiency is critical, the structure might get arbitrarily large, and/or the size of the of the structure may vary dramatically and non-monotonically over its lifetime, linked lists avoid overallocation and the intermittent performance hitches of reallocate-and-copy operations. Also, so long as you only ever need to do operations to the end, linked lists are simpler to implement and reason about (less chance of off-by one errors or other gotchas, and you don't need to wrap your head around amortization to see the O(1) time complexity for push/pop operations)

That mandatory Algorithms class in computer science programs is in part about being able to tell when you can and should deviate from the default way of doing things

1

u/braaaaaaainworms 12d ago

Good points

1

u/d3matt 8d ago

Not particularly. Most deque implementations use heap allocated arrays for the underlying storage.

1

u/EatingSolidBricks 8d ago

No. It is

T* data

int head

int tail

1

u/EatingSolidBricks 8d ago

🤢 memory indirection