Stacks

Inside This blog.
Definition
Operations
Implementation in c++
Implementation in Python
Implementation in Javascript

Definition

stack is a linear data type.

Operations

  1. push()

  2. pop()

  3. isEmpty()

  4. isFull()

  5. peek()

  6. size()

Implementation in C++

// Basic class implememtation in c++ for only int datatype.
#include <iostream>
#define max 10
using namespace std;

class stack{
    private:
     int arr[max]; 
     int top =-1;

    public:
     bool isEmpty(){
         if(top == -1){
             return true;
         }
         else{
             return false;
         }
     };

     bool isFull(){
         if(top == max-1){
             return true;
         }
         else{
             return false;
         }
     };

     void push(int value){
         if(isFull()){
             cout<<"\"stack is full\""<<endl;
         }
         else{
             top++;
             arr[top] = value;
         }
     };

     void pop(){
         if(isEmpty()){
             cout<<"\"stack is empty\""<<endl;
         }
         else{
             top--;
         }
     };

     int peek(){
         if(isEmpty()){
             return 0;
         }

         return arr[top];
     };

     int size(){
         return top+1;
     };
};

int main() {
    stack s;
    s.push(1);
    s.pop();
    cout<<s.peek()<<endl;
    cout<<s.size()<<endl;
    return 0;
};

Note: This is for a basic understanding of stacks in C++. As there are many situations where we need to make a stack for floats or strings we use the template concept in C++. This is shown in the code below.

// With class Template.
#include <iostream>
#define max 10
using namespace std;

template <class T>
class stack{
    private:
     T arr[max]; 
     int top =-1;

    public:
     bool isEmpty(){
         if(top == -1){
             return true;
         }
         else{
             return false;
         }
     };

     bool isFull(){
         if(top == max-1){
             return true;
         }
         else{
             return false;
         }
     };

     void push(T value){
         if(isFull()){
             cout<<"\"stack is full\""<<endl;
         }
         else{
             top++;
             arr[top] = value;
         }
     };

     void pop(){
         if(isEmpty()){
             cout<<"\"stack is empty\""<<endl;
         }
         else{
             top--;
         }
     };

     T peek(){
         if(isEmpty()){
             return 0;
         }

         return arr[top];
     };

     int size(){
         return top+1;
     };
};

int main() {
    stack<char> s;
    stack<float> s1;
    stack<double> s2;
    stack<string> s3;
    s3.push("learn");
    cout<<s3.peek()<<endl;
    s2.push(22.222);
    cout<<s2.peek()<<endl;
    s1.push(22.333);
    cout<<s1.peek()<<endl;
    s.push('r');
    cout<<s.peek()<<endl;
    return 0;
};

Implementation in python

class stack:
    def __init__(self, size=0):#default size 0
        self.size = size #only is stack is fixed size
        self.stack=[]

    def isEmpty(self):
        return len(self.stack) == 0

    def isFull(self):
        return len(self.stack) == self.size

    def push(self, item):
        if self.isFull():
            print("stack is full")
        else:
            self.stack.append(item);#add item to last of list

    def pop(self):
        if self.isEmpty():
            print("stack is empty")
        else:
            self.stack.pop() #delete last element of list
            print(self.stack)

    def length(self):
        return len(self.stack) # length of stack

    def peek(self):
        return self.stack[-1] #return last element of list

p1 = stack(3)#size of stack is 3
p1.push(2)
p1.push(33)
p1.push(50)
p1.push(30)
p1.push(55)
print(p1.length());
print(p1.peek());

Implementation in Javascript

class stack  {
    constructor(){
    this.arr =[]
    }

    isEmpty(){
        if(this.arr.length === 0){
            return true
        }
        else {
            return false
        }
    }

    push(value){
        this.arr.push(value);

    }

    pop(){
        if(this.isEmpty()){
            console.log("stack is empty")
        }
        else{
        this.arr.pop();
        }
    }

    peek(){
        let lastElement=this.arr.length-1;
        return this.arr[lastElement];
    }

    size(){
        return this.arr.length
    }
}

const s1 = new stack;
const s2 = new stack;
s1.push(11);
s1.pop();
console.log(s1.size());
console.log(s1.peek());
20
Subscribe to my newsletter

Read articles from Heisenberg Pandey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Heisenberg Pandey
Heisenberg Pandey