A stack is an array or list structure of function calls and parameters used in modern computer programming
function Stack() {
let items = [];
function push(element) {
items.push(element);
}
function pop() {
return items.pop();
}
function peek() {
return items[items.length - 1];
}
function isEmpty() {
return items.length === 0;
}
function size() {
return items.length;
}
function clear() {
items = [];
}
return {
clear,
push,
pop,
peek,
isEmpty,
size,
};
}
#include<bits/stdc++.h>
using namespace std;
int Stack[100],n,top,i;
void push(int x) {
if(top<n-1) {
top++;
Stack[top]=x;
} else {
cout<<"Could not insert data, Stack is full.
";
}
}
int pop() {
int data;
if(top>-1) {
data = Stack[top];
top = top - 1;
return data;
} else {
cout<<"Could not retrieve data, Stack is empty.
";
}
}
void display()
{
if(top>=0)
{
cout<<"
The elements in STACK
";
for(i=top; i>=0; i--)
cout<<Stack[i]<<" ";
}
else
{
cout<<"
The STACK is empty";
}
}
int main()
{
cin>>n;
top=-1;
push(10);
push(20);
push(40);
push(10);
cout<<endl<<pop();
display();
return 0;
}