stack Algorithm

The stack using a doubly linked list (DLL) algorithm is a dynamic data structure that stores a collection of elements in a linear fashion, where each element has a reference to the element before and after it. This stack allows for the efficient management of elements by performing insertions and deletions at the beginning or end of the list without affecting the rest of the elements. A commonly used analogy to describe a stack is a stack of plates, where you can only add or remove a plate from the top of the stack. This behavior is known as Last-In, First-Out (LIFO), which means that the most recently added element is always the first one to be removed. Implementing a stack using a doubly linked list offers several advantages over other data structures like arrays. First, it allows for dynamic resizing, which means that the stack can grow or shrink in size as elements are added or removed. This makes it more memory-efficient, as the stack only uses the exact amount of memory needed to store its elements. Second, the time complexity for inserting and deleting elements in a DLL-based stack is O(1), which makes these operations quite fast. However, this comes at the cost of increased complexity in managing the pointers for the previous and next elements in the list, which can make the implementation more challenging than using an array-based stack.
#include <iostream>
#include <assert.h>
#include "stack.h"

using namespace std;

/* Default constructor*/
template <class Type>
stack<Type>::stack()
{
    stackTop = NULL;
    size = 0;
}

/* Destructor */
template <class Type>
stack<Type>::~stack()
{
}

/* Display for testing */
template <class Type>
void stack<Type>::display()
{
    node<Type> *current = stackTop;
    cout << "Top --> ";
    while(current != NULL) {
        cout<<current->data<< "  ";
        current = current -> next;
    }
    cout <<endl;
    cout << "Size of stack: " << size << endl;
}

/* Determine whether the stack is empty */
template <class Type>
bool stack<Type>::isEmptyStack()
{
    return (stackTop == NULL);
}

/* Clear stack */
template <class Type>
void stack<Type>::clear()
{
    stackTop = NULL;
}

/* Add new item to the stack */
template <class Type>
void stack<Type>::push(Type item)
{
    node<Type> *newNode;
    newNode = new node<Type>;
    newNode->data = item;
    newNode->next = stackTop;
    stackTop = newNode;
    size++;
}

/* Return the top element of the stack */
template <class Type>
Type stack<Type>::top()
{
    assert(stackTop != NULL);
    return stackTop->data;
}

/* Remove the top element of the stack */
template <class Type>
void stack<Type>::pop()
{
    node<Type> *temp;
    if(!isEmptyStack()) {
        temp = stackTop;
        stackTop = stackTop->next;
        delete temp;
        size--;
    } else {
        cout << "Stack is empty !" << endl;
    }
}

/* Operator "=" */
template <class Type>
stack<Type> stack<Type>::operator=(stack<Type> & otherStack)
{
    node<Type> *newNode, *current, *last;

    if (stackTop != NULL) /* If stack is no empty, make it empty */
        stackTop = NULL;
    if (otherStack.stackTop == NULL)
        stackTop = NULL;
    else {
        current = otherStack.stackTop;
        stackTop = new node<Type>;
        stackTop->data = current->data;
        stackTop->next = NULL;
        last = stackTop;
        current = current ->next;
        /* Copy the remaining stack */
        while (current != NULL)
        {
            newNode = new node<Type>;
            newNode->data = current->data;
            newNode->next = NULL;
            last->next = newNode;
            last = newNode;
            current = current->next;
        }
    }
    size = otherStack.size;
    return *this;
}

LANGUAGE:

DARK MODE: