how to implement stack in java code example
Example 1: java stack
Stack<String> stack = new Stack<String>();
stack.push("Hello");
String top = stack.pop();
String peek = stack.peek();
Example 2: stack class in java
import java.util.Stack;
class Main {
public static void main(String[] args) {
Stack<String> animals= new Stack<>();
animals.push("Dog");
animals.push("Horse");
animals.pop();
animals.peek();
}
}
Example 3: stack implementation
typedef struct Nodo{
Elem val;
struct Nodo *next;
} *Stack;
Stack Empty(){return NULL;}
bool IsEmpty(Stack a){return a==NULL;}
Elem Top(Stack a){return a->val;}
Stack Pop(Stack l){return l->next;}
Stack Push(Elem x,Stack res){
Stack nuevo=(Stack)malloc(sizeof(struct Nodo));
nuevo->val=x;
nuevo->next=res;
return nuevo;
}