Skip to content

Conversation

@c252
Copy link
Member

@c252 c252 commented Aug 9, 2018

I implemented a stack and queue in c, James told me it was okay even though the section isn't open for code contribution.

@leios
Copy link
Member

leios commented Aug 9, 2018

I'll be updating this section this weekend (hopefully), so we'll hold off on merging this until that is done.

@c252
Copy link
Member Author

c252 commented Aug 9, 2018 via email

Copy link
Contributor

@zsparal zsparal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is how I'd implement stacks and queues. Using arrays as backing stores is the right idea but the implementation is a bit problematic:

  1. They are not reusable since they're using global variables
  2. They're using a fixed size with no error handling when the available space runs out

I think the respective implementations should use:

  1. A dynamically growing array for stacks
  2. A dynamically growing circular buffer for queues.

These have the following advantages:

  1. They're still cache-friendly, contiguous data structures
  2. Even though they have worse worst-case complexity than the respectivelinked list implementations, the amortized insertion/deletion complexity is still O(1)
  3. It gracefully handles more than 256 elements
  4. Encapsulating the necessary data in a struct means that one can create multiple queues/stacks
@Gathros Gathros added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Aug 9, 2018
@c252
Copy link
Member Author

c252 commented Aug 9, 2018

I will put in some error handling for the size. One problem is that I don't think you can have dynamically sized arrays in c. I think my queue implementation could also be more efficient though.

(should I close this PR, commit the changes and then make a new one)

@Gathros
Copy link
Contributor

Gathros commented Aug 9, 2018

No, you can just commit to the same branch and the PR updates. Also you use malloc and resize it with realloc to make a dynamically sized array in c.

@zsparal
Copy link
Contributor

zsparal commented Aug 9, 2018

Of course you can have dynamically sized arrays in C, it wouldn't be a very useful language otherwise :) You just need to do it by hand with malloc

c252 added 2 commits August 9, 2018 08:52
program was much worse than I realized
@c252 c252 closed this Aug 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)

4 participants