answersLogoWhite

0

#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

10y ago

What else can I help you with?

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++.


Delimiter Matching in c plus plus using stack?

Delimiter matching in C++ can be efficiently implemented using a stack data structure. As you traverse a string containing various delimiters (like parentheses, brackets, and braces), you push opening delimiters onto the stack. When you encounter a closing delimiter, you check if it matches the top of the stack; if it does, you pop the stack. If there's a mismatch or if the stack is empty when a closing delimiter is found, the input is not properly matched. This method ensures that every opening delimiter has a corresponding and correctly ordered closing delimiter.


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. }

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); }


Delimiter Matching in c plus plus using stack?

Delimiter matching in C++ can be efficiently implemented using a stack data structure. As you traverse a string containing various delimiters (like parentheses, brackets, and braces), you push opening delimiters onto the stack. When you encounter a closing delimiter, you check if it matches the top of the stack; if it does, you pop the stack. If there's a mismatch or if the stack is empty when a closing delimiter is found, the input is not properly matched. This method ensures that every opening delimiter has a corresponding and correctly ordered closing delimiter.


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.