Thread: writing a test_mylist.cpp

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    4

    Red face writing a test_mylist.cpp

    For this i have to write a program that tests the correctness of the implementation.

    This code is mylist.h
    I am new at programming and was wondering on how to get started...
    i have to write a mylist.cpp file and test its implimentation.
    Code:
    /* mylist.h  -- a doubly-linked list */
    
    #include <algorithm>
    using namespace std;
    
    // forward declaration of classes defined in this header
    template <class T> class mylist;
    template <class T> class mylink;
    template <class T> class mylist_iterator;
    
    template <class T> 
    class mylist
    {
    public:
       typedef T value_type;
       typedef mylist_iterator<T> iterator;
    
       // constructors
       mylist(); // default constructor
       mylist(const mylist<T> & list); // copy constructor
    
       ~mylist(); // destructor
    
       // operations
       bool empty() const;
       int size() const;
       T & back();
       T & front();
       void push_front(const T & x);
       void push_back(const T & x);
       void pop_front();
       void pop_back();
       iterator begin();
       iterator end();
       void sort();  // sort list in O(n log n) -- EXTRA CREDIT
       void insert(iterator pos, const T & x); // insert x before pos
       void erase(iterator pos);
       void erase(iterator first, iterator last);
    
    protected:
       mylink<T> * first_link;
       mylink<T> * last_link;
       unsigned int my_size;
    };
    
    template <class T> 
    class mylink 
    {
    private:
       mylink(const T & x);
    
       T x;
       mylink<T> * next_link;
       mylink<T> * prev_link;
    
       friend class mylist<T>;
       friend class mylist_iterator<T>;
    };
    
    template <class T> class mylist_iterator 
    {
    public:
       typedef mylist_iterator<T> iterator;
    
       // constructor
       mylist_iterator(mylink<T> * current_link);
    
       // iterator protocol
       T & operator * (); 
       void operator = (const iterator & rhs);
       bool operator == (const iterator & rhs) const;
       bool operator != (const iterator & rhs) const;
       iterator & operator ++ (); // preincrement, ex. ++it
       iterator operator ++ (int); // postincrement, ex. it++
       iterator & operator -- (); // predecrement
       iterator operator -- (int); // postdecrement
    
    protected:
       mylink<T> * current_link;
       friend class mylist<T>;
    };
    Last edited by Takuhari; 06-11-2007 at 09:54 PM.

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    4
    any ideas?

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Takuhari View Post
    any ideas?
    Yes - show your attemp and ask specific question if you fail in some place.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    4
    Quote Originally Posted by vart View Post
    Yes - show your attemp and ask specific question if you fail in some place.
    im stukk here....
    i waz implimenting part of the code...
    tell me if im on the right track.... im getting all kindz of errors...

    Code:
    #include <mylist.h>
    using namespace std;
    
    
    int main(){
    	bool empty(){
    		return my_size == 0;
    	}
    
    	int size(){
    		return my_size;
    	}
    
    	T & back(){
    		return last_link->x;
    	}
    
    	T & front(){
    		return first_link->x;
    	}
    
    	void push_front(const T & x){
    		new_item->next_link=first_link;
    		first_link->prev_link=new_item;
    		first_link=new_item;
    		new_item->prev_link=0;
    	}
    	
    	void push_back(const T & x){
    		new_item->prev_link=last_link;
    		last_link->next_link=new_item;
    		last_link=new_item;
    		new_item->next_link=0;
    	}
    	void pop_front(){
    		first_link->next_link=first_link;
    		first_link->prev_link=0;
    	}
    
    	void pop_back(){
    		last_link->prev_link=last_link;
    		last_link->next_link=0;
    	}
    
    
    
    	return 0;
    }

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Contrary to many students' wishes, writing arbitrary and sometimes random code in relatively equally arbitrary and random places does not generally produce the desired results.

    When your compiler spits out lots of errors at you, it is indeed trying to get your attention.

    Don't implement functions inside main() or any other function.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Your testprogram should really test the class interface

    So

    1. Create the program that just creates (and then destroys) the list of ints - test if this works
    2. Add several items to the list
    3. Print list contents
    4. remove arbitrary item
    5. Print list contents
    6. Remove first item
    7. Remotre last item

    Repeate the above for list of std::strings
    Repeate the above for list of vectors

    Use two lists simulteneously

    Pass the list as a parameter to the function by ref and by value

    remove the item from the list inside function and print it inside function and outside function...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Optimize file writing
    By Opariti in forum Windows Programming
    Replies: 7
    Last Post: 10-23-2008, 01:32 PM
  2. Replies: 2
    Last Post: 05-20-2008, 08:57 AM
  3. Very slow file writing of 'fwrite' function in C
    By scho in forum C Programming
    Replies: 6
    Last Post: 08-03-2006, 02:16 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM