Linked List implementation in C++ -


i've 3 header(.hpp) files define node, singly linked list , iterator. should build singly linked-list. here's node.

#ifndef node_hpp #define node_hpp  #include <iostream> #include <string>  template <typename t> struct node {   public:     explicit node(const t& value, node* next = 0): value_(value), next_(next){}   private:     t value_;     node* next_;  };  #endif //node_hpp 

here's slist

#ifndef slist_hpp #define slist_hpp  #include "node.hpp" #include "iterator.hpp"  template <typename t> class slist{  public:     class iterator;      slist() :head_(0){ }      bool empty() const{return (head_==0);}      int size() const{         int s=0;         iterator last=end();         for(iterator i= begin(); i!= last; ++i; ++s);         return s;     };      iterator begin() { return iterator(head_); }     iterator end() { return iterator(0); }      void insert(iterator p, const t& t);      void erase(iterator p){         node<t>* tmp = p.node_->next_;         p.node_->value = p.node_->next_.value;         p.node_->next_=p.node_->next_->next_;          delete tmp;         return p;     };  private:     node<t>* head_;  };  #endif //slist_hpp 

and here's iterator class

#ifndef iterator_hpp #define iterator_hpp  #include "slist.hpp" #include "node.hpp"    class iterator{  public:     explicit iterator(node<t>* node=0) :node_(node){ }      t& operator* (){return node_-> value_;}     t* operator->(){return node_->value_;}      iterator& operator++(){         if(node_ != 0) node_ = node_->next_;         return *this;     }      iterator operator++(int){         iterator tmp = *this;         if(node_!= 0) node_ = node_->next_;         return tmp;     }      bool operator==(const iterator& iter) const { return (node_ == iter.node_); }      bool operator!=(const iterator& iter) const { return (node_ != iter.node_); }   private:     node<t>* node_; };  #endif //iterator_hpp 

the problem nothing seems resolved. code doesn't compile because slist can't read values iterator despite iterator being defined inside slist. iterator class displays squiggly on methods return iterator object. can me understand what's going wrong here.

if compiler doesn't tell problems are, start simple , build up. what's simplest iterator can think of? try this:

template <typename t> class iterator{ }; 

so far, good. add complexity little @ time, testing @ every step. can write slist now, or add functionality iterator. you'll notice problem write like

for(iterator i= begin(); i!= last; ++i; ++s); 

when hit snag that, don't add more complexity until fix it.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -