c - Sentinel vs. passing around a count -


i have structure in c contains array on perform stack operations.

if stack full, need prevent pushing element past end of array, , return error condition.

is better style include size of stack element of structure, , pass number of elements stack_push() function, or should have sentinel element @ end of stack array?

how going implement stack_push() function?

if requires scanning end of array looking empty slot insert pushed element into, need sentinel value anyway (e.g., null, if array contains pointer elements). note algorithm going o(n).

on other hand, keeping track of number of active elements within array allows algorithm o(1) pushes (and pops). saves trouble of allocating 1 element in array, may significant if it's array of structs.

generally speaking, stack data structures implemented using array , counter.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -