stack c++
#include <bits/stdc++.h>
stack<int> stk;
stk.push(5);
int ans = stk.top(5); // ans =5
stk.pop();//removes 5
stack c++
#include <bits/stdc++.h>
stack<int> stk;
stk.push(5);
int ans = stk.top(5); // ans =5
stk.pop();//removes 5
stack implementation using class in c++
#include<iostream>
using namespace std;
#define Size 5
class Stack
{
private:
int Array[Size];
int top;
public:
Stack()
{
top = -1;
}
void Push(int x)
{
if (top == Size - 1)
{
cout << "Error, stack overFlow!" << endl;
return;
}
Array[++top] = x;
}
void Pop()
{
if (top == -1)
{
cout << "Error, stack is Empty!" << endl;
return;
}
top--;
}
int Top()
{
return Array[top];
}
bool IsEmpty()
{
if (top == -1)
return 1;
return 0;
}
void print()
{
cout << "Stack: ";
for (int i = 0; i <= top; i++)
{
cout << Array[i] << " ";
}
cout << "\n";
}
};
int main()
{
Stack s;
s.Push(1);
s.Push(2);
s.print();
return 0;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us