How to Create a Stack of User-Defined Data Type in C++?

In this article, we will discuss how to create a stack of User-Defined Data Types in C++. But before discussing the creation of a stack, we must know about the stack.

What does std::stack mean?

A data structure called a stack, uses the Last In, First Out (LIFO) method of operation. Only one end of the std::stack can be utilized to add and remove elements.

The std::stack class is an adaptor for containers. Similar data types are stored in container objects, and different sequence containers can be combined to form a stack. If no container is provided, the deque container will be used by default. Data manipulation is not possible with container adapters as they do not allow iterators.

In C++, one way to create a stack of user-defined data types is to utilise the std::stack container adapter and other capabilities offered by the Standard Template Library (STL).

Syntax:

It has the following syntax:

  • stack: The Standard Template Library (STL) in C++ provides the template class stack. It represents a stack data structure in which items are added and deleted from the same end, referred to as the top of the stack, by the Last In, First Out (LIFO) principle.
  • <DataType>: The data type of the elements that will be stored in the stack is indicated by this placeholder. The actual data type we want to use has been replaced with DataType. Some examples are int, double, string, and even user-defined types (classes).
  • NameOfTheStack: The stack variable is referred to by this name. Any valid identifier may be used as the name. Here, the name NameOfTheStack is used as an example.

Stack Operations:

Basic operations supported by a C++ stack include:

  1. Push: An item is added to or pushed into the stack.
  2. Pop: It allows an object on the stack to be removed or pop.
  3. Peek: It returns the top element in the stack without taking it down.
  4. isFull: Determines if a stack is filled.
  5. isEmpty: Determines if a stack is empty.

Example:

Let us take an example to illustrate how to create a stack of user-defined data types in C++.

Output:

The top student name in the stack is: Name of the student is: Jacob, Age of the student is: 28
Students in the stack are:
The name of the student is: Jacob, Age of the student is: 26
The name of the student is: Joseph, Age of the student is: 29
The name of the student is: Johnson, Age of the student is: 27

Explanation:

In this example, this code shows how to manage a set of user-defined objects (Demo objects) in C++ using a stack. It demonstrates how to access the top element, push, pop, and check for empty, among other fundamental stack operations. It further demonstrates how to overload operators such as << for user-defined classes to customise output.

Applications of the stack:

There are several applications of the stack in C++. Some main applications of the stack are as follows:

1. Management of Function Calls

Programming languages heavily use stacks to handle function calls and return addresses.

A function's parameters, local variables, and return address are placed into the stack when it is called and removed from it upon its return.

2. Evaluation of Expression

Arithmetic expressions, including infix, postfix, and prefix expressions, are evaluated using stacks.

Operators and operands are stacked during evaluation, and operations are carried out according to their associativity and precedence.

3. History of browsers

Users may go back and forth across previously seen websites using stacks to manage their browser history.

4. Undo/Redo Features

Stacks are often used in text editors and graphical apps to create undo and redo functions that allow users to undo changes they have made to documents or designs.

5. Managing Memory

In computer systems, stacks are essential for managing memory.

Stack-based data structures, including the call stack and stack frames, are frequently used to implement memory allocation and deallocation.






Latest Courses