answersLogoWhite

0


Best Answer

#include<iostream>

#include<stack>

#include<cassert>

int main ()

{

std::stack<unsigned> s;

assert (s.empty()==true);

s.push (42);

assert (s.empty()==false);

s.pop ();

assert (s.empty()==true);

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you determine when a stack is empty in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How you use stack in c plus plus classes?

It would be easier to manipulate the stack in assembly language rather than C++.


You must write a program in C that uses a stack to check a file for unbalanced parenthesis any suggestions?

Parse the character stream from the beginning of the sequence to the end, counting characters as you go. Every time you encounter an opening parenthesis, push the current character count onto the stack. Every time you encounter a closing parenthesis, ensure the stack is not empty before popping the top value from the stack. You can discard the value -- you've found a matching pair. If you attempt to pop from an empty stack, then you have a closing parenthesis at the current character position that has no matching opening parenthesis. If you reach the end of the test and the stack is not empty, you have at least one opening parenthesis without a matching closing parenthesis. Pop the character positions off the stack to determine where the opening parenthesis are.


Representation of stack data structure in c plus plus?

Stack is an abstract data type that allows you to input and output data in a way that the first data which was placed in the stack will be the last one to get out. We use physical examples of stack in our daily lives such as the stack of dishes or stack of coins where you only add or remove objects from the top of the stack. You can see the implementation in c++ in related links, below.


Write a c plus plus function to reverse the contents of a stack using two additional stacks?

The following function template will reverse any stack of type T: template&lt;typename T&gt; void reverse_stack (sd::stack&lt;T&gt;&amp; A) // Pass by reference! { std::stack&lt;T&gt; B, C; // Two additional stacks (initially empty). while (!A.empty()) { B.push (A.top()); A.pop(); } while (!B.empty()) { C.push (B.top()); B.pop(); } while (!C.empty()) { A.push (C.top()); C.pop(); } // A is now in reverse order. } A more efficient method is to pop the stack onto a queue. template&lt;typename T&gt; void reverse_stack_optimised (sd::stack&lt;T&gt;&amp; A) // pass by reference! { std::queue&lt;T&gt; B; while (!A.empty()) { B.push_back (A.top()); A.pop(); } while (!B.empty()) { A.push (B.front()); B.pop(); } // A is now in reverse order. }


Infix to postfix C?

Infix Expression :Any expression in the standard form like "2*3-4/5" is an Infix(Inorder) expression.Postfix Expression :The Postfix(Postorder) form of the above expression is "23*45/-".Infix to Postfix Conversion :In normal algebra we use the infix notation like a+b*c. The corresponding postfix notation is abc*+. The algorithm for the conversion is as follows :Scan the Infix string from left to right.Initialise an empty stack.If the scannned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack. If the scanned character is an Operand and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character.Repeat this step till all the characters are scanned.(After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.Return the Postfix string.Example :Let us see how the above algorithm will be imlemented using an example.Infix String : a+b*c-dInitially the Stack is empty and our Postfix string has no characters. Now, the first character scanned is 'a'. 'a' is added to the Postfix string. The next character scanned is '+'. It being an operator, it is pushed to the stack.StackPostfix StringNext character scanned is 'b' which will be placed in the Postfix string. Next character is '*' which is an operator. Now, the top element of the stack is '+' which has lower precedence than '*', so '*' will be pushed to the stack.StackPostfix StringThe next character is 'c' which is placed in the Postfix string. Next character scanned is '-'. The topmost character in the stack is '*' which has a higher precedence than '-'. Thus '*' will be popped out from the stack and added to the Postfix string. Even now the stack is not empty. Now the topmost element of the stack is '+' which has equal priority to '-'. So pop the '+' from the stack and add it to the Postfix string. The '-' will be pushed to the stack.StackPostfix StringNext character is 'd' which is added to Postfix string. Now all characters have been scanned so we must pop the remaining elements from the stack and add it to the Postfix string. At this stage we have only a '-' in the stack. It is popped out and added to the Postfix string. So, after all characters are scanned, this is how the stack and Postfix string will be :StackPostfix StringEnd result :Infix String : a+b*c-dPostfix String : abc*+d-

Related questions

What is mapping exception in c plus plus?

There is no such exception in C++. It's probably a 3rd party or user-defined exception. Examine the call-stack to determine where the exception was caught -- that should help you determine where the exception was thrown.


How you use stack in c plus plus classes?

It would be easier to manipulate the stack in assembly language rather than C++.


'write a simple program for stack operation in c plus plus?

void push(int y) { if(top&gt;stackSize) { cout&lt;&lt;"stack full"&lt;&lt;endl; return; } else { top++; stack[top]=y; } } int pop() { int a; if(top&lt;=0) { cout&lt;&lt;"stack is empty"&lt;&lt;endl; return 0; } else { a=stack[top]; top--; } return(a); }


Why you will declare top as -1 for stacks to be empty in c program?

You would do this if you implement a stack using an array. Using a zero-based index to keep track of the top of the stack (the end of the array) means we must use the value -1 to indicate an empty stack. This is because an array of n elements will have indices 0 through n-1, where index n-1 represents the element at top of the stack. An empty stack has 0 elements, therefore the top of the stack is represented by index -1.


Represent a stack in c plus plus?

Use a vector with a base class type. Any objects derived from the base class can be pushed and popped from the vector just as you would from a stack.


You must write a program in C that uses a stack to check a file for unbalanced parenthesis any suggestions?

Parse the character stream from the beginning of the sequence to the end, counting characters as you go. Every time you encounter an opening parenthesis, push the current character count onto the stack. Every time you encounter a closing parenthesis, ensure the stack is not empty before popping the top value from the stack. You can discard the value -- you've found a matching pair. If you attempt to pop from an empty stack, then you have a closing parenthesis at the current character position that has no matching opening parenthesis. If you reach the end of the test and the stack is not empty, you have at least one opening parenthesis without a matching closing parenthesis. Pop the character positions off the stack to determine where the opening parenthesis are.


Representation of stack data structure in c plus plus?

Stack is an abstract data type that allows you to input and output data in a way that the first data which was placed in the stack will be the last one to get out. We use physical examples of stack in our daily lives such as the stack of dishes or stack of coins where you only add or remove objects from the top of the stack. You can see the implementation in c++ in related links, below.


What is pop in C plus plus?

"Pop" allows you to remove items off of the stack. The stack is an area in memory that contains the information of a program, such as function names and instructions, the values of variables, etc. The opposite of pop is "push". This allows you to add items to the stack.


In c plus plus what is meant by stack?

The stack is a region of memory organized as a first-in-last-out (LIFO) structure which stores return information, parameters, and local variables. Since it is a LIFO structure, it is nested, i.e. "stacked", similar to how a stack of papers on your desk would be stacked, and if you could only deal with the top-most paper on the stack. At the language level, C, C++, JAVA, etc., you generally do not even think about the implementation of the stack - it is just there - and it does what it needs in terms of return state, registers, parameters, and automatic or temporary variables.


Write a c plus plus function to reverse the contents of a stack using two additional stacks?

The following function template will reverse any stack of type T: template&lt;typename T&gt; void reverse_stack (sd::stack&lt;T&gt;&amp; A) // Pass by reference! { std::stack&lt;T&gt; B, C; // Two additional stacks (initially empty). while (!A.empty()) { B.push (A.top()); A.pop(); } while (!B.empty()) { C.push (B.top()); B.pop(); } while (!C.empty()) { A.push (C.top()); C.pop(); } // A is now in reverse order. } A more efficient method is to pop the stack onto a queue. template&lt;typename T&gt; void reverse_stack_optimised (sd::stack&lt;T&gt;&amp; A) // pass by reference! { std::queue&lt;T&gt; B; while (!A.empty()) { B.push_back (A.top()); A.pop(); } while (!B.empty()) { A.push (B.front()); B.pop(); } // A is now in reverse order. }


How do you refresh stack using library functions in C programming?

What do you mean by stack-refreshing? Anyway, there are no stack handling functions in the standard C library.


Infix to postfix C?

Infix Expression :Any expression in the standard form like "2*3-4/5" is an Infix(Inorder) expression.Postfix Expression :The Postfix(Postorder) form of the above expression is "23*45/-".Infix to Postfix Conversion :In normal algebra we use the infix notation like a+b*c. The corresponding postfix notation is abc*+. The algorithm for the conversion is as follows :Scan the Infix string from left to right.Initialise an empty stack.If the scannned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack. If the scanned character is an Operand and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character.Repeat this step till all the characters are scanned.(After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.Return the Postfix string.Example :Let us see how the above algorithm will be imlemented using an example.Infix String : a+b*c-dInitially the Stack is empty and our Postfix string has no characters. Now, the first character scanned is 'a'. 'a' is added to the Postfix string. The next character scanned is '+'. It being an operator, it is pushed to the stack.StackPostfix StringNext character scanned is 'b' which will be placed in the Postfix string. Next character is '*' which is an operator. Now, the top element of the stack is '+' which has lower precedence than '*', so '*' will be pushed to the stack.StackPostfix StringThe next character is 'c' which is placed in the Postfix string. Next character scanned is '-'. The topmost character in the stack is '*' which has a higher precedence than '-'. Thus '*' will be popped out from the stack and added to the Postfix string. Even now the stack is not empty. Now the topmost element of the stack is '+' which has equal priority to '-'. So pop the '+' from the stack and add it to the Postfix string. The '-' will be pushed to the stack.StackPostfix StringNext character is 'd' which is added to Postfix string. Now all characters have been scanned so we must pop the remaining elements from the stack and add it to the Postfix string. At this stage we have only a '-' in the stack. It is popped out and added to the Postfix string. So, after all characters are scanned, this is how the stack and Postfix string will be :StackPostfix StringEnd result :Infix String : a+b*c-dPostfix String : abc*+d-