Stack in C STL with Example - JohnHau/mis GitHub Wiki

What is std::stack? A stack is a data structure that operates based on LIFO (Last In First Out) technique. The std::stack allows elements to be added and removed from one end only.

The std::stack class is a container adapter. Container objects hold data of a similar data type. You can create a stack from various sequence containers. If no container is provided, the deque containe will be used by default. Container adapters don’t support iterators, so it can’t be used to manipulate data.

In this C++ tutorial, you will learn

What is std::stack? Stack Syntax Member Types Operations in Stack Stack Implementation push() and pop() empty(), size(), top() emplace() and swap() Stack in STL Stack Syntax To create a stack, we must include the header file in our code. We then use this syntax to define the std::stack:

template <class Type, class Container = deque > class stack; Type – is the Type of element contained in the std::stack. It can be any valid C++ type or even a user-defined type. Container – is the Type of underlying container object. Member Types Here are stack member types:

value_type- The first template parameter, T. It denotes the element types. container_type- The second template parameter, Container. It denotes the underlying container type. size_type- Unsigned integral type. Operations in Stack A C++ stack supports the following basic operations:

push – It adds/pushes an item into the stack. pop – It removes/pops an item from the stack. peek – Returns the top item of the stack without removing it. isFull – Checks whether a stack is full. isEmpty – Checks whether a stack is empty.

image

image

image

image

image

image

image

Code Explanation:

Include the iostream header file in our code in order to use its functions. Include the stack header file in our code in order to use its functions. Include the std namespace in our program in order to use its classes without calling it. Create the function createStack that we can use to create the stack mystack. The stack will hold a set of integers. The beginning of the body of the createStack function. Create an instance of the mystack datatype and giving it the name ms. Use the while loop and the empty() function to check whether the stack is empty. The start of the body of the while loop. Use the top() function stored at the top of the stack. The \t character will create a new tab. Use the pop() function to delete the element at the top of the stack. End of the body of the while loop. Print a blank line on the console. End of the body of the createStack function. Call the main() function. The program logic should be added within the body of the main() function. The start of the body of function main(). Create a stack object st. Use the push() function to insert the element 32 into the stack. Use the push() function to insert the element 21 into the stack. Use the push() function to insert the element 39 into the stack. Use the push() function to insert the element 89 into the stack. Use the push() function to insert the element 25 into the stack. Print some text on the console. Call the createStack function to execute the above insert operations into the stack. Print the size of the stack on the console alongside other text. Print the element at the top of the stack on the console. Print some text on the console. Delete the element at the top of the stack. It will then return the elements remaining in the stack. Call the createStack function to execute the above operations. The program must return value upon successful completion. End of the body of function main(). emplace() and swap() These are other inbuilt stack functions:

emplace()- constructs then inserts new element to top of stack. swap()- exchanges stack contents with another stack’s contents.

image

image

Code Explanation:

Include the iostream header file in our code to use its functions. Include the stack header file in our code to use its functions. Include the cstdlib header file in our code to use its functions. Include the std namespace in our code to use its classes without calling it. Call the main() function. The program logic will be added within the body of this function. Declare a stack named st1 to store integer values. Declare a stack named st2 to store integer values. Use the emplace() function to insert the integer 12 into the stack named st1. Use the emplace() function to insert the integer 19 into the stack named st1. Use the emplace() function to insert the integer 20 into the stack named st2. Use the emplace() function to insert the integer 23 into the stack named st2. Use the swap() function to swap the contents of the two stacks, st1 and st2. The contents of the stack st1 should be moved to the stack st2. The contents of the stack st2 should be moved to the stack st1. Print some text on the console. Use the while statement and the empty() function to check whether the stack st1 is not empty. Print the contents of the stack st1 on the console. The ” ” adds space between the stack elements when printing them on the console. Execute the pop() function on the stack st1 to remove the top element. End of the body of the while statement. Print some text on the console. The endl is a C++ keyword for end line. It moves the mouse cursor to the next line to begin printing from there. Use the while statement and the empty() function to check whether the stack st2 is not empty. Print the contents of the stack st2 on the console. The ” ” adds space between the stack elements when printing them on the console. Execute the pop() function on the stack st2 to remove the top element. End of the body of the while statement. End of the body of the main() function.

image

image

Code Explanation:

Include the iostream header file in our code to use its functions. Include the stack header file in our code to use its functions. Include the cstdlib header file in our code to use its functions. Include the std namespace in our code to use its classes without calling it. Call the main() function. The program logic should be added within the body of this function. Declare a stack st to store integer data. Add the element 12 to the stack. Add the element 19 to the stack. Add the element 20 to the stack. Print the element at the top of the stack on the console. Print the size of the stack on the console. End of the body of the function main(). Summary: A stack is a data structure that operates based on the LIFO (Last In first Out) technique. The std::stack only allows items to be added and removed from one end. The std::stack class is a container adapter, holding items of a similar data type. A stack can be created from various sequence containers. If you don’t provide a container, the deque container will be used by default. The push() function is for inserting items into the stack. The pop() function is for removing the top item from the step. The empty() function is for checking whether a stack is empty or not.

⚠️ **GitHub.com Fallback** ⚠️