Understanding Stack implementation problem statement

谁说我不能喝 提交于 2019-12-24 13:49:52

问题


I need to compute the peek mid element also the problem statement for implementing this method is as follows:-

*returns object which has the middle value among the all objects without removing it from the stack. *returns the object which has the value of following order (size()/2)+1 *e.g. *When the stack has the following values (1, 2, 5, 4, 2, 6) *this method returns 4 and doesn't remove the object.

so my query is:-

should i consider the middle element in terms of position i.e. after sorting the elements of the stack the mid element is obtained as mid = stack[size()/2+1] or should i consider it in terms of value i.e. mid= max+min/2

as in above problem both the situations are correct( in my point of view) i.e.

stack[size()/2+1]=stack[6/2+1]=4

and max+min/2=6+1/2=3.5 and rounding off will be equal to 4

kindly help me understanding the problem statement


回答1:


A stack is a data structure, and as such in the most generic case should be able to store any data type. The fact that you are dealing with ints is just a simplification for your assignment. Data structure wise, it makes sense that you consider the middle element, and not perform any computation on element values (that is too specific for a data structure).

It seems like what you want is the ((n/2) + 1) th element, therefore the element at index (n/2) in this example.



来源:https://stackoverflow.com/questions/7143327/understanding-stack-implementation-problem-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!