Skip to main content
Spelling and grammar
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

Stack Linked list based implementaionimplementation in C++

This is my second implementation of the Stack in c++C++. I already implemented it as an array-based here.

I need a review for it to improve it and improve my coding skill. I also will butput this implementation on my GitHub account

//======================================================
// Author      : Omar_Hafez
// Created     : 27 July 2022 (Wednesday)  11:02:46 AM
//======================================================

#include <iostream>

enum InsertStatus { FailedStackEmpty = -1, FailedStackFull = -2, OK = 0 };

template <class T>
class Stack {
   private:
    int size = 0;

   public:
    struct Node {
        T value;
        Node* next = nullptr;
    };

    Node* top;

    Stack() { top = nullptr; }

    Stack(int MAX_SIZE) {
        //  this constructor is to keep the consistency with the array based implementation
        top = nullptr;
    }

    bool isEmpty() { return !top; }

    bool isFull() { return 0; }

    int getSize() const { return size; }

    InsertStatus push(T const& t) {
        Node* node = new Node;
        node->value = t;
        node->next = top;
        top = node;
        size++;
        return OK;
    }

    InsertStatus pop() {
        if (isEmpty()) return FailedStackEmpty;
        Node* tmp_ptr = top;
        top = top->next;
        delete tmp_ptr;
        size--;
        return OK;
    }

    T stackTop() { return top->value; }

    void clearStack() {
        for (Node* tmp_ptr; top; tmp_ptr = top) {
            top = top->next;
            delete tmp_ptr;
        }
        size = 0;
    }
};

Stack Linked list based implementaion in C++

This is my second implementation of the Stack in c++. I already implemented it as an array-based here

I need a review for it to improve it and improve my coding skill. I also will but this implementation on my GitHub account

//======================================================
// Author      : Omar_Hafez
// Created     : 27 July 2022 (Wednesday)  11:02:46 AM
//======================================================

#include <iostream>

enum InsertStatus { FailedStackEmpty = -1, FailedStackFull = -2, OK = 0 };

template <class T>
class Stack {
   private:
    int size = 0;

   public:
    struct Node {
        T value;
        Node* next = nullptr;
    };

    Node* top;

    Stack() { top = nullptr; }

    Stack(int MAX_SIZE) {
        //  this constructor is to keep the consistency with the array based implementation
        top = nullptr;
    }

    bool isEmpty() { return !top; }

    bool isFull() { return 0; }

    int getSize() const { return size; }

    InsertStatus push(T const& t) {
        Node* node = new Node;
        node->value = t;
        node->next = top;
        top = node;
        size++;
        return OK;
    }

    InsertStatus pop() {
        if (isEmpty()) return FailedStackEmpty;
        Node* tmp_ptr = top;
        top = top->next;
        delete tmp_ptr;
        size--;
        return OK;
    }

    T stackTop() { return top->value; }

    void clearStack() {
        for (Node* tmp_ptr; top; tmp_ptr = top) {
            top = top->next;
            delete tmp_ptr;
        }
        size = 0;
    }
};

Stack Linked list based implementation in C++

This is my second implementation of the Stack in C++. I already implemented it as an array-based here.

I need a review for it to improve it and improve my coding skill. I also will put this implementation on my GitHub account

//======================================================
// Author      : Omar_Hafez
// Created     : 27 July 2022 (Wednesday)  11:02:46 AM
//======================================================

#include <iostream>

enum InsertStatus { FailedStackEmpty = -1, FailedStackFull = -2, OK = 0 };

template <class T>
class Stack {
   private:
    int size = 0;

   public:
    struct Node {
        T value;
        Node* next = nullptr;
    };

    Node* top;

    Stack() { top = nullptr; }

    Stack(int MAX_SIZE) {
        //  this constructor is to keep the consistency with the array based implementation
        top = nullptr;
    }

    bool isEmpty() { return !top; }

    bool isFull() { return 0; }

    int getSize() const { return size; }

    InsertStatus push(T const& t) {
        Node* node = new Node;
        node->value = t;
        node->next = top;
        top = node;
        size++;
        return OK;
    }

    InsertStatus pop() {
        if (isEmpty()) return FailedStackEmpty;
        Node* tmp_ptr = top;
        top = top->next;
        delete tmp_ptr;
        size--;
        return OK;
    }

    T stackTop() { return top->value; }

    void clearStack() {
        for (Node* tmp_ptr; top; tmp_ptr = top) {
            top = top->next;
            delete tmp_ptr;
        }
        size = 0;
    }
};
edited tags
Link
pacmaninbw
  • 26.2k
  • 13
  • 47
  • 114
added 2111 characters in body
Source Link
Omar_Hafez
  • 389
  • 1
  • 12
//======================================================
// Author      : Omar_Hafez
// Created     : 27 July 2022 (Wednesday)  11:02:46 AM
//======================================================
 

#include <iostream> 

enum InsertStatus { FailedStackEmpty = -1, FailedStackFull = -2, OK = 0 };

template<classtemplate T><class T>
class Stack {
    private:
        int size = 0;
  
   public:
        struct Node {
            T value;
            Node* next = nullptr;
        };

        Node* top;
 

        Stack() {
            top = nullptr;
        }

        Stack(int MAX_SIZE) {
            //  this constructor is to keep the consistency with the array based implementation
            top = nullptr;
        }

        bool isEmpty() {
            return !top;
        }

        bool isFull() {
            return 0;
        }

        int getSize() const {
            return size;
        }

        InsertStatus push(T const& t) {
            NodeNode* *nodenode = new Node;
            node->value = t;
            node->next = top;
            top = node;
            size++;
            return OK;
        }

        InsertStatus pop() {
            if (isEmpty())  return FailedStackEmpty;
            NodeNode* *tmp_ptrtmp_ptr = top;
            top = top->next;
            delete tmp_ptr;
            size--;
            return OK;
        }

        T stackTop() {
            return top->value;
        }

        void clearStack() {
            for (Node* tmp_ptr; top; tmp_ptr = top) {
                top = top->next;
                delete tmp_ptr;
            }
            size = 0;
        }
    
};
//======================================================
// Author      : Omar_Hafez
// Created     : 27 July 2022 (Wednesday)  11:02:46 AM
//======================================================
 

#include <iostream> 

enum InsertStatus {FailedStackEmpty = -1, FailedStackFull = -2, OK = 0};

template<class T> class Stack {
    private:
        int size = 0;
    public:
        struct Node {
            T value;
            Node* next = nullptr;
        };

        Node* top;
 

        Stack() {
            top = nullptr;
        }

        Stack(int MAX_SIZE) {
            //  this constructor is to keep the consistency with the array based implementation
            top = nullptr;
        }

        bool isEmpty() {
            return !top;
        }

        bool isFull() {
            return 0;
        }

        int getSize() const {
            return size;
        }

        InsertStatus push(T const& t) {
            Node *node = new Node;
            node->value = t;
            node->next = top;
            top = node;
            size++;
            return OK;
        }

        InsertStatus pop() {
            if(isEmpty())  return FailedStackEmpty;
            Node *tmp_ptr = top;
            top = top->next;
            delete tmp_ptr;
            size--;
            return OK;
        }

        T stackTop() {
            return top->value;
        }

        void clearStack() {
            for(Node* tmp_ptr; top; tmp_ptr = top) {
                top = top->next;
                delete tmp_ptr;
            }
            size = 0;
        }
    
};
//======================================================
// Author      : Omar_Hafez
// Created     : 27 July 2022 (Wednesday)  11:02:46 AM
//======================================================

#include <iostream>

enum InsertStatus { FailedStackEmpty = -1, FailedStackFull = -2, OK = 0 };

template <class T>
class Stack {
   private:
    int size = 0;
 
   public:
    struct Node {
        T value;
        Node* next = nullptr;
    };

    Node* top;

    Stack() { top = nullptr; }

    Stack(int MAX_SIZE) {
        //  this constructor is to keep the consistency with the array based implementation
        top = nullptr;
    }

    bool isEmpty() { return !top; }

    bool isFull() { return 0; }

    int getSize() const { return size; }

    InsertStatus push(T const& t) {
        Node* node = new Node;
        node->value = t;
        node->next = top;
        top = node;
        size++;
        return OK;
    }

    InsertStatus pop() {
        if (isEmpty()) return FailedStackEmpty;
        Node* tmp_ptr = top;
        top = top->next;
        delete tmp_ptr;
        size--;
        return OK;
    }

    T stackTop() { return top->value; }

    void clearStack() {
        for (Node* tmp_ptr; top; tmp_ptr = top) {
            top = top->next;
            delete tmp_ptr;
        }
        size = 0;
    }
};
Source Link
Omar_Hafez
  • 389
  • 1
  • 12
Loading