C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ stackIn computer science we go for working on a large variety of programs. Each of them has their own domain and utility. Based on the purpose and environment of the program creation, we have a large number of data structures available to choose from. One of them is 'stack'. Before discussing about this data type let us take a look at its syntax. Syntaxtemplate<class T, class Container = deque<T> > class stack; This data structure works on the LIFO technique, where LIFO stands for Last In First Out. The element which was first inserted will be extracted at the end and so on. There is an element called as 'top' which is the element at the upper most position. All the insertion and deletion operations are made at the top element itself in the stack. Stacks in the application areas are implied as the container adaptors. The containers should have a support for the following list of operations:
Template ParametersT: The argument specifies the type of the element which the container adaptor will be holding. Container: The argument specifies an internal object of container where the elements of the stack are hold. Member TypesGiven below is a list of the stack member types with a short description of the same.
FunctionsWith the help of functions, an object or variable can be played with in the field of programming. Stacks provide a large number of functions that can be used or embedded in the programs. A list of the same is given below:
Example: A simple program to show the use of basic stack functions.#include <iostream> #include <stack> using namespace std; void newstack(stack <int> ss) { stack <int> sg = ss; while (!sg.empty()) { cout << '\t' << sg.top(); sg.pop(); } cout << '\n'; } int main () { stack <int> newst; newst.push(55); newst.push(44); newst.push(33); newst.push(22); newst.push(11); cout << "The stack newst is : "; newstack(newst); cout << "\n newst.size() : " << newst.size(); cout << "\n newst.top() : " << newst.top(); cout << "\n newst.pop() : "; newst.pop(); newstack(newst); return 0; } Output: The stack newst is : 11 22 33 44 55 newst.size() : 5 newst.top() : 11 newst.pop() : 22 33 44 55
Next TopicC++ Stack
|