Question

Q4. When creating stacks what is the meant by the top value ? How is it’s...

Q4. When creating stacks what is the meant by the top value ? How is it’s initialization
performed? Which value is it initialized to and why?

Homework Answers

Answer #1

Stack is a data structure which stores data in it in Last In First Out (LIFO) order. When creating stacks, top value indicates the top-most element of the stack, i.e, the element which is inserted at last or the most recently inserted element in the stack. All the insertion and deletion operations of stack is done from the top of the last element that is inserted.

In C++, peek() and stack.top() function is used to access the top of the stack. Suppose in starting, no element is there in the stack. At that time, top value of the stack will be below zero or -1.

Now suppose we push an element in this stack.

stack<int> mystack;

    mystack.push(8);

Now the top value will automatically get initialized as 8.

If you push another element in this stack, say  mystack.push(2);, now the top value will get initialized as 2.

Lets assume that now you want to change the top value of the stack without pushing any other element in the stack. In this case, you can use stack.top() function as shown below.

mystack.top()=5;

Now instead of 2, top value will get initialized as 5.

If you still have any doubt/query regarding the answer then let me know in comment. If it helps, kindly give an upVote to this answer.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
a) What is meant by the term "banking union"? What was the rationale for creating a...
a) What is meant by the term "banking union"? What was the rationale for creating a banking union in Europe? b) “Ireland followed international best practice by setting up NAMA, as asset management agency to run down the bad assets of the Irish banks. Releasing bad assets from bank balance sheets is instrumental in the path to recovery.” Dirk Schoenmaker (2015). Using the stylised bank’s balance sheet presented in Q4, discuss how setting up an asset management agency like NAMA...
How would you define the difference between "sensation" and "perception"?What is meant by "top down" processing?...
How would you define the difference between "sensation" and "perception"?What is meant by "top down" processing? Can you apply this concept to your own studies: are there classes/subjects in which you learn and study the material from the "bottom up" and others where it could be defined as "top down"? On the topic of sensory adaptation: how would our experience of the world be different if we did not experience sensory adaptation? How could we view this process as one...
What is meant by the contention that Gestalt analysis proceeds from the top down rather than...
What is meant by the contention that Gestalt analysis proceeds from the top down rather than from the bottom up? How is the law of Prägnanz involved in perceiving the world in a top down manner?
What is meant by "top down" processing? Can you apply this concept to your own studies:...
What is meant by "top down" processing? Can you apply this concept to your own studies: are there classes/subjects in which you learn and study the material from the "bottom up" and others where it could be defined as "top down"? Color blindness: is it an issue with sensation or with perception? (or both?) Explain your answer. On the topic of sensory adaptation: how would our experience of the world be different if we did not experience sensory adaptation? How...
explain what is meant by a p-value and how it is used in hypothesis tests flr...
explain what is meant by a p-value and how it is used in hypothesis tests flr fhe population mwan
Discuss how the net asset value (NAV) is determined and what is meant by market-to-market.
Discuss how the net asset value (NAV) is determined and what is meant by market-to-market.
What is meant by operational definition, when is it necessary, and why is it necessary? Provide...
What is meant by operational definition, when is it necessary, and why is it necessary? Provide examples to support your main points.
Frankfurt parliament, who and what is it but also when, what’s important and why it’s significant...
Frankfurt parliament, who and what is it but also when, what’s important and why it’s significant to European history
Explain the time value of money concept. What is meant by the effective interest rate. How...
Explain the time value of money concept. What is meant by the effective interest rate. How are time value of money concepts applied to accounting applications in determining the present value of expected cash flows and in valuing bonds?
Explain the time value of money concept. What is meant by the effective interest rate. How...
Explain the time value of money concept. What is meant by the effective interest rate. How are time value of money concepts applied to accounting applications in determining the present value of expected cash flows and in valuing bonds?
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT