Pages

Sunday, July 20, 2025

Stack using Linked List – Partial (University Exam Topic)

 #include <iostream>

using namespace std;


struct Node {

    int data;

    Node* next;

};


class Stack {

    Node* top;


public:

    Stack() { top = NULL; }


    void push(int x) {

        Node* temp = new Node();

        temp->data = x;

        temp->next = top;

        top = temp;

    }


    void pop() {

        if (top == NULL) {

            cout << "Stack Underflow\n";

            return;

        }

        Node* temp = top;

        top = top->next;

        delete temp;

    }


    int peek() {

        if (top == NULL) return -1;

        return top->data;

    }


    bool isEmpty() {

        return top == NULL;

    }

};


No comments:

Post a Comment

3917. Count Indices With Opposite Parity (Brute Force) O(n2) + Optimized Solution O(n) + tips LEETCODE WEEKLY 500

  class Solution {     public int [] countOppositeParity ( int [] nums ) {          // approach 1 - checks every pair         int n = n...