stack C++ code example
Example 1: stack c++
stack<int> s;
s.push(3);
s.push(2);
s.push(5);
cout << s.top();
s.pop();
cout << s.top();
Example 2: stack c++
stack<int> stk;
stk.push(5);
int ans = stk.top(5);
stk.pop();
Example 3: cpp stack
template<class S, const int N> class Stack {
private:
S arr[N];
int top_i;
public:
Stack() : arr(), top_i(-1) {}
void push (S n) {
arr[++top_i] = n;
}
void pop() {
top_i--;
}
S top() {
return arr[top_i];
}
S bottom() {
return arr[0];
}
int size() {
return top_i+1;
}
};
Example 4: stack c++
#include <bits/stdc++.h>
stack<int> stk;
stk.push(5);
int ans = stk.top(5);
stk.pop();
Example 5: 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;
}