how would you do it?
with only the functions i've provided?
I agree O(2n) is not desirable. Should I just pop the front until it's empty? (I was being lazy...), sorry for the bad example on the destructor.
how would you do it?
with only the functions i've provided?
I agree O(2n) is not desirable. Should I just pop the front until it's empty? (I was being lazy...), sorry for the bad example on the destructor.
Last edited by dudeomanodude; 03-12-2008 at 01:37 PM.
Ubuntu Desktop
GCC/G++
Geany (for quick projects)
Anjuta (for larger things)
This is probably more kosher then:
Code:#include <iostream> class linklist{ public: linklist() : head(0) {}; ~linklist(); void push_back(int d); void pop_front(); friend void display(linklist& m_list); private: struct node{ node(int d) : data(d), next(0) {}; int data; node* next; }; node* head; }; linklist::~linklist(){ while(head != 0){ pop_front(); } delete head; } void linklist::push_back(int d){ if(head != 0){ node *curr = head; while(curr->next != 0){ curr = curr->next; } curr->next = new node(d); } else{ head = new node(d); } } void linklist::pop_front(){ if(head != 0){ node *curr = head; if(head->next == 0){ // Only one element in list delete curr; head = 0; } else{ head = curr->next; delete curr; } } } void display(linklist& m_list){ linklist::node *curr = m_list.head; while(curr != 0){ std::cout << curr->data << "\n"; curr = curr->next; } } /* MAIN: */ int main(){ linklist m_list; for(int i = 0; i < 10; i++) m_list.push_back(i + 1); display(m_list); return 0; }
Last edited by dudeomanodude; 03-12-2008 at 01:59 PM. Reason: readability
Ubuntu Desktop
GCC/G++
Geany (for quick projects)
Anjuta (for larger things)
Now you're talking!
This would do it:
Code:linklist::~linklist() { while(head != 0) pop_front(); }
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Thanks dudeomanodude it's nice code
There's one thing I don't like about the list implementation, the fact that I need to declare display() as a friend function. What I'd rather do is make an iterator class within list that outsiders can use to traverse the list.
So, we can add the iterator class to the public interface of list:
Then instead of needing to hard code friend functions into the header we can write our own functions and use iterators to gain access to the next->Code:public: class iterator{ public: iterator( node* c = 0 ) : curr( c ) {}; int operator*( ) { return curr->data; }; bool operator!=( const iterator& i ) { return i.curr != curr; }; bool operator==( const iterator& i ) { return i.curr == curr; }; iterator& operator++( ) { curr = curr->next; return *this; }; private: node* curr; }; const iterator begin() const{ return iterator(head); };
Code:void iter_display(linklist& m_list){ for(linklist::iterator it = m_list.begin(); it != 0; ++it){ std::cout << *it << "\n"; } }
Ubuntu Desktop
GCC/G++
Geany (for quick projects)
Anjuta (for larger things)