Stack Using Array
Stack implementation using STL
#include <iostream>
#include <stack>
using namespace std;
int main()
{
// stack container declaration
stack<int> s1;
stack<int> s2;
// pushing elements into first stack
s1.push(1);
s1.push(2);
s1.emplace(3);
s1.push(4);
// pushing elements into 2nd stack
s2.push(3);
s2.emplace(5);
s2.push(7);
s2.push(9);
cout << "Size of the Stack is: " << s1.size() << ", " << s2.size() << endl;
cout << "Top Elements of both stacks are: " << s1.top() << ", " << s2.top()<< endl;
// using swap() function to swap elements of stacks
s1.swap(s2);
cout << "After Swapping both stacks!!" << endl;
// printing the first stack
cout<<"s1 = ";
while (!s1.empty()) {
cout<<s1.top()<<" ";
s1.pop(); // deleting
}
// printing the second stack
cout<<endl<<"s2 = ";
while (!s2.empty()) {
cout<<s2.top()<<" ";
s2.pop(); // deleting
}
return 0;
}
Stack Implementation using arrays
// Stack class.
class Stack {
int *arr;
int qtop;
int size;
public:
Stack(int capacity) {
size = capacity;
arr = new int [size] ;
qtop = -1;
}
void push(int num) {
if(qtop != size-1)
arr[++qtop] = num;
}
int pop() {
if(isEmpty()) {
return -1;
}
int ans = arr[qtop];
qtop--;
return ans;
}
int top() {
if(isEmpty())
return -1;
return arr[qtop];
}
int isEmpty() {
return qtop == -1;
}
int isFull() {
return qtop == size-1;
}
};
Easy to Understand
#include<iostream>
#include<stack>
using namespace std;
class Stack {
//properties
public:
int *arr;
int top;
int size;
// behaviour
Stack(int size) {
this -> size = size;
arr = new int[size];
top = -1;
}
void push( int element) {
if(size - top > 1) {
top++;
arr[top] = element;
}
else{
cout << "Stack OverFlow" << endl;
}
}
void pop() {
if(top >=0 ) {
top--;
}
else{
cout << "Stack UnderFlow" << endl;
}
}
int peek() {
if(top >=0 )
return arr[top];
else
{
cout << "Stack is Empty" << endl;
return -1;
}
}
bool isEmpty() {
if( top == -1) {
return true;
}
else{
return false;
}
}
};
int main() {
Stack st(5);
st.push(22);
st.push(43);
st.push(44);
st.push(22);
st.push(43);
st.push(44);
cout << st.peek() << endl;
st.pop();
cout << st.peek() << endl;
st.pop();
cout << st.peek() << endl;
st.pop();
cout << st.peek() << endl;
if(st.isEmpty()) {
cout << "Stack is Empty mere dost " << endl;
}
else{
cout << "Stack is not Empty mere dost " << endl;
}
/*
//creation of stack using STL
stack<int> s;
//push operation
s.push(2);
s.push(3);
//pop
s.pop();
cout << "Printing top element " << s.top() << endl;
if(s.empty()){
cout << "Stack is empty " << endl;
}
else{
cout << "stack is not empty " << endl;
}
cout << "size of stack is " << s.size() << endl;
*/
return 0;
}
#include<iostream>
using namespace std;
class Stack{
int *arr;
int top;
int size;
public:
Stack(int size) {
this -> size = size;
arr = new int[size];
top = -1;
}
void push(int element) {
if(size - top > 1)
arr[++top] = element;
else
cout << "<< Stack Overflow!! \nBecause you're trying to push <" << element << "> into the stack >>" << endl;
}
void pop() {
if(isEmpty())
cout << "Stack Underflow !" << endl;
else
top--;
}
void peak () {
if(isEmpty())
cout << "Stack is Empty !" << endl;
else
cout << arr[top] << endl;
}
bool isEmpty() {
if(top == -1)
return true;
else
return false;
}
};
int main() {
int size = 5;
Stack s(size);
s.push(100);
// inserting/pushing elements
for (int i = 0; i < size; i++)
s.push(i+1);
// 100,1,2,3,4,5 -> beacuse of 5 else statment will be printed
// printng & popping
cout << endl << "printing & popping elements! " << endl;
while(!s.isEmpty()) {
s.peak(); //
s.pop();
}
s.peak();
s.pop();
}
Last updated
Was this helpful?