c++ - LinkedList using initializer_list -


so, i'm trying initialize linkedlist class using initializer_list.

template<typename t> sortedlist<t>::sortedlist(initializer_list<t> e){     head_= new node<t>(*e.begin());      long intcheck = 0;     t old;      (auto x : e){         if(intcheck > 0){             node<t>* curr = new node<t>(old);             if(head_ == curr){                 head_->next_ = new node<t>(x);             }             curr->next_ = new node<t>(x);         }          old = x;         intcheck = 1;     }   } 

i seg fault when trying print head_->next_ (nothing wrong print function)

i'm assuming want sortedlist sorted. if so, accomplish goal. bails out if initializer_list empty, still leaves object in rational state.

template<typename t> sortedlist<t>::sortedlist(initializer_list<t> e) : head_{nullptr} {     if (e.size() == 0)         return;     auto = e.begin();     (head_ = new node<t>(*it); != e.end(); ++it) {         node<t> *n = new node<t>(*it);         node<t> *curr;         (curr = head_; curr->next_ && curr->next_->data_ < *it; curr = curr->next_)              continue;         if (*it < curr->data_) {             n->next_ = curr;             head_ = n;         } else {             n->next_ = curr->next_;             curr->next_ = n;         }     } } 

for completeness, here's destructor used test:

template<typename t> sortedlist<t>::~sortedlist() {     while (head_->next_) {         node<t> *t = head_->next_;         head_->next_ = t->next_;         delete t;     }     delete head_;     } 

Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -