C# OOP: Stack

Search item of 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.

//find an item in the Stack

public ListNode<T> find(T tar)
{
  ListNode<T> t;
  t = pfirst;
  bool f = false;
  while (t != null)
  {
   if (t.val.ToString().CompareTo(tar.ToString()) == 0) { f = true; break;    }
   t = t.next;
}

  if (f != false) return t;
  else return null;
}




Comments




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.