Thank you for the replies. Appreciate it
My understanding was that you can perform only push and pop operation on the stack but today we were asked to write a function that would search for a given element in the stack and return it if it exists. This left me confused
Wouldn't stack be usually implemented when the LIFO order must be strictly followed? Assuming that's the case, if I want to search the stack for a particular element, the best practice would be to pop the contents until the desired element is found and then push them back onto the stack in their original order, correct? And if I do that, wouldn't it be more convenient to store the popped elements in a separate array since having two stacks would get kind of redundant? Or am I missing something here?

Also, is there a data structure generally preferred for implementing stack or is it just a matter of one's preference?
You are getting confused between different data structures mate. Stack is based on Last-In-First-Out concept, i.e the last element pushed is the first element popped. I'll give you a simple analogy.
Consider a courier service where you are assigned to process and acknowledge different documents received. You have a placeholder container at your side where new letters and documents are dumped for you to process. The first letter arrives, then next and next, until the newest letter is always at top. Now in a stack, you will always pick the newest or top-most letter to process. After that, you remove it (pop) and process the next letter on top. So the first letter that had arrived will be processed at last, and all new letters will be on top, getting first priority.
If you want to process the first letter, i.e at the bottom, then you following a "Queue" and not a "Stack". Queue follows the first in first out mechanism. If you want to prioritize the letters by traversing the pile and choosing which letter to process first, then it becomes a
"Priority Queue".
Stack is designed to follow LIFO. As a programmer, you cannot traverse the array, or else it won't be a stack. It will be a priority queue or something else. Most microprocessors are designed towards a stack implementation, i.e newest threads that are pushed into the memory are the first ones to be processed and popped for write back.
Consider the following example of a stack. I've implemented this in JAVA, while you can prefer you own programming language. There are no array traversing code in a stack, else it won't be called as one.
PHP:
//Mechanism of Stack data structure implemented in JAVA
public class newStack {
private int[] testArr ;
private int size;
private int first ;
public newStack(int s) {
size = s;
testArr = new int [size];
first = -1;
}
public void push(int m) {
testArr[++first] = m;
}
public int pop (){
return testArr[first--];
}
public int peek() {
return testArr[first];
}
public boolean isEmpty(){
return (first == -1);
}
public boolean isFull(){
return (first == size - 1);
}
}
PHP:
// Test Code of stack implementation
import java.util.Scanner;
public class StackTest {
public static void main (String args []){
newStack stack = new newStack(10);
Scanner in = new Scanner(System.in);
System.out.println("Enter the desired values max upto arraysize:");
while (!stack.isFull()){
stack.push(in.nextInt());
}
while(!stack.isEmpty()){
int n = stack.pop();
System.out.print(n);
System.out.print(" ");
}
System.out.println("");
}
}
If you are a C++ programmer, you will able to understand this. Test it out on an Eclipse or Netbeans IDE or any java compiler. It will ask you keyboard input and you can understand how a stack and its LIFO mechanism works.