C++ exercises and solutions: search item in Stack


Search item in Stack

Step 6: Searching for an item in the Stack

Another important operation of the Stack is searching for an item. To search for a specific item in the Stack is a sequential process. The comparison starts from the beginning of the list until the target item is found or until the end of the list is reached. Therefore, the firs matched item is returned.

//Searching for an item in the Stack

ListElem *find(int tar){
ListElem *t=pfirst;
int f = 0;
while (t != NULL)
if (t->data==tar) { f = 1; break; }
t = t->next;

if (f != 0) return t;
else return NULL;


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.