Thread: What is wrong in this??i am tired!

  1. #16
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    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)

  2. #17
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    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)

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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"

  4. #19
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Thanks dudeomanodude it's nice code

  5. #20
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    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:
    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); };
    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:
    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)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM