r/ProgrammerHumor • • 11d ago

Meme queueIsAlwaysLong

Post image
244 Upvotes

48 comments sorted by

View all comments

Show parent comment

8

u/RedCrafter_LP 11d ago

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

2

u/pencilUserWho 11d 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 10d ago

Why would you want to work with a linked list?

1

u/pencilUserWho 10d ago

To implement a stack

1

u/braaaaaaainworms 10d 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 10d ago

That way it can overflow.

1

u/braaaaaaainworms 10d ago

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

1

u/Nightmoon26 10d 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 10d ago

Good points

1

u/d3matt 6d ago

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

1

u/EatingSolidBricks 6d ago

No. It is

T* data

int head

int tail