Stack template using linked list












3












$begingroup$


I'm working on a stack implementation using a linked list, but I have a strong feeling that I overcomplicated my solution. I would appreciate it if you review this code and give me any suggestions on code and style.



#pragma once
#include <functional>
#include <iostream>
#include <type_traits>
#include <stdexcept>
template <typename T>
class StackList
{
private:
class Node
{
T data;
Node * next;
Node(Node * next, const T & data) :next(next), data(data) {};
friend class StackList<T>;
};
int size_ = 0;
Node * head_ = nullptr;
Node * tail_ = nullptr;
void AddToTail(T& data);
public:
StackList() = default;
StackList(StackList & other);
StackList(StackList && other);
StackList & operator=(StackList & other);
StackList & operator=(StackList && other);
~StackList() { EmptyList(); }

void EmptyList();
void push(const T & data);
T pop();
const T& operator(int count) const;
T& operator(int count) { return const_cast<T &>(static_cast<const StackList &>(*this).operator(count)); };
int size() { return size_; }
void Traverse(std::function<void(T&)> lamda) const;
void Traverse(std::function<void(T&)> lamda){ (static_cast<const StackList &>(*this).Traverse(lamda)); }

template <typename T>
friend std::ostream & operator<<(std::ostream & os, StackList<T> & stack);
};

template <typename T>
void StackList<T>::AddToTail(T& data)
{
if (head_ == nullptr)
head_ = tail_ = new Node(nullptr, data);
else
{
tail_->next = new Node(nullptr, data);
tail_ = tail_->next;
}
}

template <typename T>
StackList<T>::StackList(StackList & other)
{
std::function<void(T&)> lamda = [&](T& data) {this->AddToTail(data); this->size_++; };
other.Traverse(lamda);
}

template <typename T>
StackList<T>::StackList(StackList && other) : head_(other.head_), tail_(other.tail_), size_(other.size_)
{
other.head_ = 0;
other.tail_ = 0;
other.size_ = 0;
}

template <typename T>
StackList<T> & StackList<T>::operator=(StackList<T> & other)
{
if (this != &other)
{
if (other.size_ == 0)
EmptyList();
else
{
if (size_ >= other.size_)
{
Node * current = head_;
std::function<void(T&)> lamda = [&](T& data) {current->data = data; tail_ = current; current = current->next; };
other.Traverse(lamda);
while (current != nullptr)
{
Node * save = current->next;
delete current;
current = save;
}
}
else
{
Node * current = other.head_;
std::function<void(T&)> lamda = [&](T& data) {data = current->data; current = current->next; };
Traverse(lamda);
while (current != nullptr)
{
AddToTail(current->data);
current = current->next;
}
}
tail_->next = nullptr;
size_ = other.size_;
}
}
return *this;
}

template <typename T>
StackList<T> & StackList<T>::operator=(StackList<T> && other)
{
if (this != &other)
{
head_ = other.head_;
tail_ = other.tail_;
size_ = other.size_;
other.head_ = 0;
other.tail_ = 0;
other.size_ = 0;
}
return *this;
}

template <typename T>
const T& StackList<T>::operator(int count) const
{
if (count > size_ - 1 && count < 0)
throw std::invalid_argument("Out of range index!");
Node * search = head_;
for (int i = 0; i < count; i++)
search = search->next;
return search->data;
}

template <typename T>
void StackList<T>::EmptyList()
{
while (head_ != nullptr)
pop();
}

template <typename T>
void StackList<T>::push(const T & data)
{
head_ = new Node(head_, data);
if (tail_ == nullptr)
tail_ = head_;
size_++;
}

template <typename T>
T StackList<T>::pop()
{
if (size_ > 0)
{
T retval = head_->data;
Node * temp = head_->next;
delete head_;
head_ = temp;
size_--;
if (size_ == 0)
head_ = tail_ = nullptr;
return retval;
}
else
{
throw std::invalid_argument("Pop of empty list");
}

}
template <typename T>
void StackList<T>::Traverse(std::function<void(T&)> lamda) const
{
Node * cur = head_;
while (cur != nullptr)
{
lamda(cur->data);
cur = cur->next;
}
}

template <typename T>
std::ostream & operator<<(std::ostream & os, StackList<T> & stack)
{
std::function<void(T&)> lamda = [&](T& data) { os << data << std::endl; };
std::ios_base::fmtflags f(os.flags());
os << "Stack of " << typeid(T).name() << ", size = " << stack.size() << std::endl;
stack.Traverse(lamda);
os.flags(f);
return os;
}









share|improve this question









New contributor




Aleksei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    3












    $begingroup$


    I'm working on a stack implementation using a linked list, but I have a strong feeling that I overcomplicated my solution. I would appreciate it if you review this code and give me any suggestions on code and style.



    #pragma once
    #include <functional>
    #include <iostream>
    #include <type_traits>
    #include <stdexcept>
    template <typename T>
    class StackList
    {
    private:
    class Node
    {
    T data;
    Node * next;
    Node(Node * next, const T & data) :next(next), data(data) {};
    friend class StackList<T>;
    };
    int size_ = 0;
    Node * head_ = nullptr;
    Node * tail_ = nullptr;
    void AddToTail(T& data);
    public:
    StackList() = default;
    StackList(StackList & other);
    StackList(StackList && other);
    StackList & operator=(StackList & other);
    StackList & operator=(StackList && other);
    ~StackList() { EmptyList(); }

    void EmptyList();
    void push(const T & data);
    T pop();
    const T& operator(int count) const;
    T& operator(int count) { return const_cast<T &>(static_cast<const StackList &>(*this).operator(count)); };
    int size() { return size_; }
    void Traverse(std::function<void(T&)> lamda) const;
    void Traverse(std::function<void(T&)> lamda){ (static_cast<const StackList &>(*this).Traverse(lamda)); }

    template <typename T>
    friend std::ostream & operator<<(std::ostream & os, StackList<T> & stack);
    };

    template <typename T>
    void StackList<T>::AddToTail(T& data)
    {
    if (head_ == nullptr)
    head_ = tail_ = new Node(nullptr, data);
    else
    {
    tail_->next = new Node(nullptr, data);
    tail_ = tail_->next;
    }
    }

    template <typename T>
    StackList<T>::StackList(StackList & other)
    {
    std::function<void(T&)> lamda = [&](T& data) {this->AddToTail(data); this->size_++; };
    other.Traverse(lamda);
    }

    template <typename T>
    StackList<T>::StackList(StackList && other) : head_(other.head_), tail_(other.tail_), size_(other.size_)
    {
    other.head_ = 0;
    other.tail_ = 0;
    other.size_ = 0;
    }

    template <typename T>
    StackList<T> & StackList<T>::operator=(StackList<T> & other)
    {
    if (this != &other)
    {
    if (other.size_ == 0)
    EmptyList();
    else
    {
    if (size_ >= other.size_)
    {
    Node * current = head_;
    std::function<void(T&)> lamda = [&](T& data) {current->data = data; tail_ = current; current = current->next; };
    other.Traverse(lamda);
    while (current != nullptr)
    {
    Node * save = current->next;
    delete current;
    current = save;
    }
    }
    else
    {
    Node * current = other.head_;
    std::function<void(T&)> lamda = [&](T& data) {data = current->data; current = current->next; };
    Traverse(lamda);
    while (current != nullptr)
    {
    AddToTail(current->data);
    current = current->next;
    }
    }
    tail_->next = nullptr;
    size_ = other.size_;
    }
    }
    return *this;
    }

    template <typename T>
    StackList<T> & StackList<T>::operator=(StackList<T> && other)
    {
    if (this != &other)
    {
    head_ = other.head_;
    tail_ = other.tail_;
    size_ = other.size_;
    other.head_ = 0;
    other.tail_ = 0;
    other.size_ = 0;
    }
    return *this;
    }

    template <typename T>
    const T& StackList<T>::operator(int count) const
    {
    if (count > size_ - 1 && count < 0)
    throw std::invalid_argument("Out of range index!");
    Node * search = head_;
    for (int i = 0; i < count; i++)
    search = search->next;
    return search->data;
    }

    template <typename T>
    void StackList<T>::EmptyList()
    {
    while (head_ != nullptr)
    pop();
    }

    template <typename T>
    void StackList<T>::push(const T & data)
    {
    head_ = new Node(head_, data);
    if (tail_ == nullptr)
    tail_ = head_;
    size_++;
    }

    template <typename T>
    T StackList<T>::pop()
    {
    if (size_ > 0)
    {
    T retval = head_->data;
    Node * temp = head_->next;
    delete head_;
    head_ = temp;
    size_--;
    if (size_ == 0)
    head_ = tail_ = nullptr;
    return retval;
    }
    else
    {
    throw std::invalid_argument("Pop of empty list");
    }

    }
    template <typename T>
    void StackList<T>::Traverse(std::function<void(T&)> lamda) const
    {
    Node * cur = head_;
    while (cur != nullptr)
    {
    lamda(cur->data);
    cur = cur->next;
    }
    }

    template <typename T>
    std::ostream & operator<<(std::ostream & os, StackList<T> & stack)
    {
    std::function<void(T&)> lamda = [&](T& data) { os << data << std::endl; };
    std::ios_base::fmtflags f(os.flags());
    os << "Stack of " << typeid(T).name() << ", size = " << stack.size() << std::endl;
    stack.Traverse(lamda);
    os.flags(f);
    return os;
    }









    share|improve this question









    New contributor




    Aleksei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      3












      3








      3





      $begingroup$


      I'm working on a stack implementation using a linked list, but I have a strong feeling that I overcomplicated my solution. I would appreciate it if you review this code and give me any suggestions on code and style.



      #pragma once
      #include <functional>
      #include <iostream>
      #include <type_traits>
      #include <stdexcept>
      template <typename T>
      class StackList
      {
      private:
      class Node
      {
      T data;
      Node * next;
      Node(Node * next, const T & data) :next(next), data(data) {};
      friend class StackList<T>;
      };
      int size_ = 0;
      Node * head_ = nullptr;
      Node * tail_ = nullptr;
      void AddToTail(T& data);
      public:
      StackList() = default;
      StackList(StackList & other);
      StackList(StackList && other);
      StackList & operator=(StackList & other);
      StackList & operator=(StackList && other);
      ~StackList() { EmptyList(); }

      void EmptyList();
      void push(const T & data);
      T pop();
      const T& operator(int count) const;
      T& operator(int count) { return const_cast<T &>(static_cast<const StackList &>(*this).operator(count)); };
      int size() { return size_; }
      void Traverse(std::function<void(T&)> lamda) const;
      void Traverse(std::function<void(T&)> lamda){ (static_cast<const StackList &>(*this).Traverse(lamda)); }

      template <typename T>
      friend std::ostream & operator<<(std::ostream & os, StackList<T> & stack);
      };

      template <typename T>
      void StackList<T>::AddToTail(T& data)
      {
      if (head_ == nullptr)
      head_ = tail_ = new Node(nullptr, data);
      else
      {
      tail_->next = new Node(nullptr, data);
      tail_ = tail_->next;
      }
      }

      template <typename T>
      StackList<T>::StackList(StackList & other)
      {
      std::function<void(T&)> lamda = [&](T& data) {this->AddToTail(data); this->size_++; };
      other.Traverse(lamda);
      }

      template <typename T>
      StackList<T>::StackList(StackList && other) : head_(other.head_), tail_(other.tail_), size_(other.size_)
      {
      other.head_ = 0;
      other.tail_ = 0;
      other.size_ = 0;
      }

      template <typename T>
      StackList<T> & StackList<T>::operator=(StackList<T> & other)
      {
      if (this != &other)
      {
      if (other.size_ == 0)
      EmptyList();
      else
      {
      if (size_ >= other.size_)
      {
      Node * current = head_;
      std::function<void(T&)> lamda = [&](T& data) {current->data = data; tail_ = current; current = current->next; };
      other.Traverse(lamda);
      while (current != nullptr)
      {
      Node * save = current->next;
      delete current;
      current = save;
      }
      }
      else
      {
      Node * current = other.head_;
      std::function<void(T&)> lamda = [&](T& data) {data = current->data; current = current->next; };
      Traverse(lamda);
      while (current != nullptr)
      {
      AddToTail(current->data);
      current = current->next;
      }
      }
      tail_->next = nullptr;
      size_ = other.size_;
      }
      }
      return *this;
      }

      template <typename T>
      StackList<T> & StackList<T>::operator=(StackList<T> && other)
      {
      if (this != &other)
      {
      head_ = other.head_;
      tail_ = other.tail_;
      size_ = other.size_;
      other.head_ = 0;
      other.tail_ = 0;
      other.size_ = 0;
      }
      return *this;
      }

      template <typename T>
      const T& StackList<T>::operator(int count) const
      {
      if (count > size_ - 1 && count < 0)
      throw std::invalid_argument("Out of range index!");
      Node * search = head_;
      for (int i = 0; i < count; i++)
      search = search->next;
      return search->data;
      }

      template <typename T>
      void StackList<T>::EmptyList()
      {
      while (head_ != nullptr)
      pop();
      }

      template <typename T>
      void StackList<T>::push(const T & data)
      {
      head_ = new Node(head_, data);
      if (tail_ == nullptr)
      tail_ = head_;
      size_++;
      }

      template <typename T>
      T StackList<T>::pop()
      {
      if (size_ > 0)
      {
      T retval = head_->data;
      Node * temp = head_->next;
      delete head_;
      head_ = temp;
      size_--;
      if (size_ == 0)
      head_ = tail_ = nullptr;
      return retval;
      }
      else
      {
      throw std::invalid_argument("Pop of empty list");
      }

      }
      template <typename T>
      void StackList<T>::Traverse(std::function<void(T&)> lamda) const
      {
      Node * cur = head_;
      while (cur != nullptr)
      {
      lamda(cur->data);
      cur = cur->next;
      }
      }

      template <typename T>
      std::ostream & operator<<(std::ostream & os, StackList<T> & stack)
      {
      std::function<void(T&)> lamda = [&](T& data) { os << data << std::endl; };
      std::ios_base::fmtflags f(os.flags());
      os << "Stack of " << typeid(T).name() << ", size = " << stack.size() << std::endl;
      stack.Traverse(lamda);
      os.flags(f);
      return os;
      }









      share|improve this question









      New contributor




      Aleksei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I'm working on a stack implementation using a linked list, but I have a strong feeling that I overcomplicated my solution. I would appreciate it if you review this code and give me any suggestions on code and style.



      #pragma once
      #include <functional>
      #include <iostream>
      #include <type_traits>
      #include <stdexcept>
      template <typename T>
      class StackList
      {
      private:
      class Node
      {
      T data;
      Node * next;
      Node(Node * next, const T & data) :next(next), data(data) {};
      friend class StackList<T>;
      };
      int size_ = 0;
      Node * head_ = nullptr;
      Node * tail_ = nullptr;
      void AddToTail(T& data);
      public:
      StackList() = default;
      StackList(StackList & other);
      StackList(StackList && other);
      StackList & operator=(StackList & other);
      StackList & operator=(StackList && other);
      ~StackList() { EmptyList(); }

      void EmptyList();
      void push(const T & data);
      T pop();
      const T& operator(int count) const;
      T& operator(int count) { return const_cast<T &>(static_cast<const StackList &>(*this).operator(count)); };
      int size() { return size_; }
      void Traverse(std::function<void(T&)> lamda) const;
      void Traverse(std::function<void(T&)> lamda){ (static_cast<const StackList &>(*this).Traverse(lamda)); }

      template <typename T>
      friend std::ostream & operator<<(std::ostream & os, StackList<T> & stack);
      };

      template <typename T>
      void StackList<T>::AddToTail(T& data)
      {
      if (head_ == nullptr)
      head_ = tail_ = new Node(nullptr, data);
      else
      {
      tail_->next = new Node(nullptr, data);
      tail_ = tail_->next;
      }
      }

      template <typename T>
      StackList<T>::StackList(StackList & other)
      {
      std::function<void(T&)> lamda = [&](T& data) {this->AddToTail(data); this->size_++; };
      other.Traverse(lamda);
      }

      template <typename T>
      StackList<T>::StackList(StackList && other) : head_(other.head_), tail_(other.tail_), size_(other.size_)
      {
      other.head_ = 0;
      other.tail_ = 0;
      other.size_ = 0;
      }

      template <typename T>
      StackList<T> & StackList<T>::operator=(StackList<T> & other)
      {
      if (this != &other)
      {
      if (other.size_ == 0)
      EmptyList();
      else
      {
      if (size_ >= other.size_)
      {
      Node * current = head_;
      std::function<void(T&)> lamda = [&](T& data) {current->data = data; tail_ = current; current = current->next; };
      other.Traverse(lamda);
      while (current != nullptr)
      {
      Node * save = current->next;
      delete current;
      current = save;
      }
      }
      else
      {
      Node * current = other.head_;
      std::function<void(T&)> lamda = [&](T& data) {data = current->data; current = current->next; };
      Traverse(lamda);
      while (current != nullptr)
      {
      AddToTail(current->data);
      current = current->next;
      }
      }
      tail_->next = nullptr;
      size_ = other.size_;
      }
      }
      return *this;
      }

      template <typename T>
      StackList<T> & StackList<T>::operator=(StackList<T> && other)
      {
      if (this != &other)
      {
      head_ = other.head_;
      tail_ = other.tail_;
      size_ = other.size_;
      other.head_ = 0;
      other.tail_ = 0;
      other.size_ = 0;
      }
      return *this;
      }

      template <typename T>
      const T& StackList<T>::operator(int count) const
      {
      if (count > size_ - 1 && count < 0)
      throw std::invalid_argument("Out of range index!");
      Node * search = head_;
      for (int i = 0; i < count; i++)
      search = search->next;
      return search->data;
      }

      template <typename T>
      void StackList<T>::EmptyList()
      {
      while (head_ != nullptr)
      pop();
      }

      template <typename T>
      void StackList<T>::push(const T & data)
      {
      head_ = new Node(head_, data);
      if (tail_ == nullptr)
      tail_ = head_;
      size_++;
      }

      template <typename T>
      T StackList<T>::pop()
      {
      if (size_ > 0)
      {
      T retval = head_->data;
      Node * temp = head_->next;
      delete head_;
      head_ = temp;
      size_--;
      if (size_ == 0)
      head_ = tail_ = nullptr;
      return retval;
      }
      else
      {
      throw std::invalid_argument("Pop of empty list");
      }

      }
      template <typename T>
      void StackList<T>::Traverse(std::function<void(T&)> lamda) const
      {
      Node * cur = head_;
      while (cur != nullptr)
      {
      lamda(cur->data);
      cur = cur->next;
      }
      }

      template <typename T>
      std::ostream & operator<<(std::ostream & os, StackList<T> & stack)
      {
      std::function<void(T&)> lamda = [&](T& data) { os << data << std::endl; };
      std::ios_base::fmtflags f(os.flags());
      os << "Stack of " << typeid(T).name() << ", size = " << stack.size() << std::endl;
      stack.Traverse(lamda);
      os.flags(f);
      return os;
      }






      c++ linked-list stack






      share|improve this question









      New contributor




      Aleksei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Aleksei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 1 hour ago









      200_success

      130k16153417




      130k16153417






      New contributor




      Aleksei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 hours ago









      AlekseiAleksei

      164




      164




      New contributor




      Aleksei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Aleksei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Aleksei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Aleksei is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214733%2fstack-template-using-linked-list%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Aleksei is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Aleksei is a new contributor. Be nice, and check out our Code of Conduct.













          Aleksei is a new contributor. Be nice, and check out our Code of Conduct.












          Aleksei is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214733%2fstack-template-using-linked-list%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to make a Squid Proxy server?

          第一次世界大戦

          Touch on Surface Book