C++ exercises and solutions: Stack data structure


Write a C++ program to create a Stack data structure. This Stack data structure is to store the integer values. Your program should display a menu of choices to operate the Stack data structure. See the sample menu below:
  Operations Menu
1. Add items

2. Delete items

3. Show the number of items

4. Show min and max items

5. Find an item

6. Print all items

7. Exit

Enter your choice:1

In Stack data structure, removing or adding an item  is a LIFO(Last In First Out) process. It allows items to be added to the beginning of list and removed from the beginning of the list. In this solution to the exercise, we will build a Stack data structure to store the integer values. Each item of the Stack contains two parts--data and a link. The data part is to store the integer value and the link part is a pointer to store the address of its next item. The items in the Stack are chained together by links(or pointers). We also have two important links used to point to the first item(pfirst) and point to the last item(plast) that help us in most of operations on the Stack such as adding, deleting an item, traversing through the Stack, finding an item,etc.

C Stack data structure

To keep the solution simple, we divide the solution into different steps--Defining Stack item and function prototypes, Adding item, Counting all items, Removing item, showing minimum and maximum items, searching for an item, printing items on the screen, displaying a menu of choices and the last step is to combine the C code together. You can find their links in the left menu of the page.


Sherif comment


thank you


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.