How to Create a Stack of User-Defined Data Type in C++? Last Updated : 11 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, we have a stack data structure that follows the LIFO (Last In First Out) rule and allows operations such as push, pop, and top at one end of the structure. In this article, we will learn how to create a stack of user-defined data types in C++. Example: Input://user defined data typestruct Point { int x, y;};Output:Elements in the Stack of Point:(3, 4)(1, 2)Creating Stack of a User-Defined Datatype in C++The process of creating a std::stack of user-defined data types is similar to creating a stack of built-in data types, but instead of using a built-in data type, we have to use a user-defined data type as template argument. For that, first create a custom class or struct and then use a std::stack::push() to store instances of that type. Syntax to Create Stack of User-Defined Data Type in C++stack<DataType> StackName;Here, DataType is a name of user-defined data typeStackName is a stack of DataType.C++ Program to Use a Stack with a User-Defined Data Type The below program demonstrates how we can create a stack of user-defined data types in C++. C++ // C++ Program to show how to create and use a Stack of a // User-Defined Data Type #include <iostream> #include <stack> using namespace std; // User-defined data type struct Point { int x, y; }; int main() { // Creating a stack of Points stack<Point> stackOfPoints; // Creating Points Point p1 = { 1, 2 }; Point p2 = { 3, 4 }; // Pushing Points into the stack stackOfPoints.push(p1); stackOfPoints.push(p2); // Printing elements from the stack of Points cout << "Elements in the Stack of Points:" << endl; while (!stackOfPoints.empty()) { Point currP = stackOfPoints.top(); stackOfPoints.pop(); cout << "(" << currP.x << ", " << currP.y << ")" << endl; } return 0; } OutputElements in the Stack of Points: (3, 4) (1, 2) Time Complexity: O(N), here n is the total number of elements in the stack. Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Create a Stack of Vectors in C++? B bug8wdqo Follow Improve Article Tags : C++ Programs C++ STL cpp-data-types cpp-stack CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Create Deque of User-Defined Data Type in C++? In C++, deque is a container in which we can insert and delete at both its beginning and its end. It supports almost all kinds of inbuilt data types. In this article, we will learn how to create a deque of user-defined data types in C++. Example Input://user defined data typestruct usd { int a; char 2 min read How to Create a Stack of Deque in C++? In C++, the stack is a container in which new elements are added from one end (top) and removed from that end only whereas a deque (double-ended queue) are sequence container with the feature of expansion and contraction on both ends. In this article, we will learn how to create a stack of deque in 2 min read How to Create Stack of Tuples in C++? In C++, a stack is a container adapter that provides a LIFO (Last In First Out) order of element insertion and deletion which is only possible at the end. A tuple is a container that can store elements of different types. In this article, we will learn how to create a stack of tuples in C++. Example 2 min read How to Create a Stack of Vectors in C++? In C++, a stack of vectors can be created using the Standard Template Library (STL). The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure, and a vector is a dynamic array that can grow and shrink in size. In this article, we will learn how to create a stac 2 min read How to Create a Stack of Stack in C++? In C++, the stack is a container that follows the LIFO (Last In, First Out) order in which the elements are inserted and removed from it. In this article, we will learn how to create a stack of a stack in C++. Example:Input:Elements in stack1= 1, 2, 3, 4Elements in stack2= 5, 6, 7Output:Elements in 2 min read How to Create a Stack of Unordered_Map in C++? In C++, the stack is a container that follows the LIFO(Last In First Out) rule where new elements are added from one end (top) and removed from that end only. An unordered_map is an associative container that stores elements formed by a combination of key-value pairs, where the key should be unique. 2 min read Like