Sunday 22 September 2024

Write a function for Implementation of stack using C++

Method 1 

#include <iostream.h>

#include<conio.h>


#define MAX 100  // Define the maximum size of the stack


class Stack {


   public:

 int top;

    int arr[MAX];

    Stack() {

        top = -1;  // Initialize top of the stack

    }


    // Function to add an element to the stack

    void push(int value) {

        if (top == MAX - 1) {

            cout << "Stack overflow! Cannot push " << value << endl;

        } else {

            arr[++top] = value;

            cout << value << " pushed to stack" << endl;

        }

    }


    // Function to remove an element from the stack

    void pop() {

        if (top == -1) {

            cout << "Stack underflow! Cannot pop" << endl;

        } else {

            cout << arr[top--] << " popped from stack" << endl;

        }

    }


    // Function to get the top element of the stack

    void peek() {

        if (top == -1) {

            cout << "Stack is empty!" << endl;

        } else {

            cout << "Top element is " << arr[top] << endl;

        }

    }


    // Function to check if the stack is empty

    int  isEmpty() {

        return top == -1;

    }


    // Function to check if the stack is full

    int  isFull() {

        return top == MAX - 1;

    }

};


int main() {

    Stack stack;  // Create a stack object


    // Perform stack operations

    stack.push(10);

    stack.push(20);

    stack.push(30);


    stack.peek();  // Peek at the top element


    stack.pop();   // Remove the top element

    stack.pop();


    stack.peek();  // Peek at the new top element


    // Check if the stack is empty or full

    if (stack.isEmpty()) {

        cout << "Stack is empty" << endl;

    } else {

        cout << "Stack is not empty" << endl;

    }


    if (stack.isFull()) {

        cout << "Stack is full" << endl;

    } else {

        cout << "Stack is not full" << endl;

    }

  getch();

    return 0;

}


Method 2


#include <iostream.h>

#include<stdio.h>

#include<conio.h>

//using namespace std;


#define MAX 100


class Stack {

    int top;


public:

    int a[MAX]; // Maximum size of Stack


    Stack() { top = -1; }

    int push(int x);

    int pop();

    int peek();

    int isEmpty();

    int isFull();

};


int Stack::push(int x)

{

    if (isFull()) { // Check for stack overflow using isFull()

cout << "Stack Overflow\n";

//return false;

    }

    else {

a[++top] = x;

cout << x << " pushed into stack\n";

//return true;

    }

}


int Stack::pop()

{

    if (isEmpty()) { // Check for stack underflow

cout << "Stack Underflow\n";

return 0;

    }

    else {

int x = a[top--];

return x;

    }

}


int Stack::peek()

{

    if (isEmpty()) { // Check if the stack is empty

cout << "Stack is Empty\n";

return 0;

    }

    else {

return a[top];

    }

}


int Stack::isEmpty()

{

    return (top < 0);

}


int Stack::isFull()

{

    return (top >= (MAX - 1));

}


int main()

{

clrscr();

    Stack s; 

    s.push(10);

    s.push(20);

    s.push(30);


    cout << s.pop() << " Popped from stack\n";


    // Print top element of stack after popping

    cout << "Top element is: " << s.peek() << endl;


    // Print all elements in stack:

    cout << "Elements present in stack: ";

    while (!s.isEmpty()) {

cout << s.peek() << " ";

s.pop();

    }

    cout << endl;


    // Check if the stack is full

    if (s.isFull()) {

cout << "Stack is full\n";

    } else {

cout << "Stack is not full\n";

    }

    getch();

    return 0;

}