Understanding Stack implementation problem statement
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
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.
精彩评论