Queue & Stack Assignments: 1. Describe the two basic operations on a stack. 2. S
ID: 3554465 • Letter: Q
Question
Queue & Stack Assignments:
1. Describe the two basic operations on a stack.
2. Suppose that stack is an object of type stackType<int>. What is the difference between
stack.top and stack.top - 1?
3. Suppose that stack is an object of type stackType<double> and the value of stack.top is 5.
What is the index of the top element of the stack?
4. Suppose that stack is an object of type stackType<string> and the value of stack.top - 1 is 2.
How many elements are in the stack?
5. Consider the following statements:
stackType <int> stack;
int num 1, num2;
What is output by the following segment of code?
stack.push(12);
stack.push(6);
num1 = stack.top( ) + 3;
stack.push(num1 + 5);
num2 = stack.top( ) ;
stack.push(num1 + num2);
num2 = stack.top( ) ;
stack.pop( ) ;
stack.push(15 ) ;
num1=stack.top( ) ;
stack.pop( ) ;
stack.push(15) ;
num1=stack.top( ) ;
stack.pop();
while (!stack.isEmptyStack())
{
cout<<stack.top()<<
Explanation / Answer
1.
two basic operations on a stack are
Push i.e inserting element to a stack
and pop : removing element from a stack
2.
stack.top() gives A reference to the top element in the stack
stack.top()-1 will give top element and subtract 1 from it
3.
4 th index
4.
3 elements
5.
stack:
stack.push(12) 12
stack.push(6); 12,6
stack.push(num1 + 5) 12,6,14
stack.push(num1 + num2) 12,6,14,23
pop 12,6,14
push 15 12,6,14,15
//num1=15
pop 12,6,14
output:
14 6 12
15
23
6.
num=31
stack.push(num+3)
element in stack:34
num=47
cout<<stack.top()<<
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.