0

Hey everyone,

I am currently working on a project to create a List ADT that has the following public interface:

+ size( ) : integer
o Returns the number of elements currently stored in the list.
o Postcondition: Returns 0 if empty otherwise returns the number of elements.
+ add( element: ElementType )
o Add an element to the list.
o Precondition: “element” does not already exist in the list. This is to say: no duplication allowed.
o Postcondition: “element” has been added to the end of the list.
o Throws an exception if “element” already exists in the list.
+ remove( element: ElementType ) : ElementType
o Remove this element from the list.
o Precondition: List is not empty.
o Postcondition: If the element is found in the list, it is removed from the list and returned. If the element is not found in the list, “null” is returned.
o Throws an exception if list is empty.
+ removeAll( )
o Remove all elements from the list.
o Postcondition: size( ) returns 0.
+ get( element: ElementType ) : ElementType
o Returns this element.
o Precondition: List is not empty.
o Postcondition: If the element is found in the list, it is returned. If the element is not found in the list, “null” is returned.
o Throws an exception if list is empty.


I want to make my implementation as flexible as possible in terms of the “type” of the elements in the list.Also make use of “typedef” and implement the List ADT using a reference or pointer-based data structure.

And make unit test of the ADT class .

Here is what I got so far and I am not sure if i am doing it right.So i would appreciate it if you help me figure this out.

Thanks,

Attachments
#include "ListOutOfExceptedRange.h"
#include "ListException.h"

// declaration of the class
template <typename T>
#pragma once
class list
{
public:
list();                    
   // default constructor

   bool isEmpty() const;
  // Determines whether a list is empty.
  // Precondition: None.
  // Postcondition: Returns true if the list is empty,
  // otherwise returns false.
  // Throws: None.

   int size() const;
  // Returns the number of elements currently stored in the list.
  // Precondition: None.
  //  Postcondition: Returns 0 if empty otherwise returns the number of elements.
  // Throws: None.

   const T& get(int index) const
     throw(ListOutOfExceptedRange);
  // Returns this element.
  // Precondition: List is not empty.
  // Postcondition: If the element is found in the list, it is returned. If the element is not found in the list, null is returned.
  // Throws an exception if list is empty.

   void add(int index, const T& newItem)
     throw(ListOutOfExceptedRange,ListException);
  // Add an element to the list.
  // Precondition: element does not already exist in the list. This is to say: no duplication allowed.
  // Postcondition: element has been added to the end of the list.
  // Throws an exception if element already exists in the list.

   void remove(int index)
     throw(ListOutOfExceptedRange);
  // Remove this element from the list.
  // Precondition: List is not empty.
  // Postcondition: If the element is found in the list, it is removed from the list and returned. If the element is not found in the list, null is returned.
  // Throws an exception if list is empty.

   void removeAll();
  // Remove all elements from the list.
  // Precondition: None
  // Postcondition: size( ) returns 0.
  // Throws: None
};
#include <stdexcept>
#include <string>
using namespace std;

class ListException: public logic_error
{
public:
   ListException(const string & message = "")
                        : logic_error(message.c_str())
   { }
};  

// end ListException
#include <stdexcept>
#include <string>
using namespace std;

class ListOutOfExceptedRange: public logic_error
{
public:
   ListOutOfExceptedRange(const string & message = "")
                        : logic_error(message.c_str())
   { }
};  

// end ListOutOfExceptedRange
#include "list.h"
#include <iostream>
using namespace std;

int main() 
{					
		list<long> L;
		
		for (int i=5; i>0; i--) {
			L.add(1,i);
			L.add(L.size()/2,i);
		}
		
		for (int i=1; i<=L.size(); i++)
			cout <<i<<"th smallest element: "<<L.get(i)<<endl;
		
		L.remove(3L);
		
		for (int i=1; i<=L.size(); i++)
			cout <<i<<"th smallest element: "<<L.get(i)<<endl;
			
  system("PAUSE");
  return 0;
}
2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by mike_2000_17
0

Your use of exception specifications is well intended, but impractical, especially for a generic class. In Herb Sutter's words, "Don't bother. Even experts don't bother.". The same goes for Boost developers. Here is an in-depth look at this issue. Just don't do it.

Second, whenever making generic types, you should use nested typedefs for just about any type used in your class. This avoids the maintainability nightmare of having to go through your entire code if you suddenly decide to change one of the types.

Finally, if you are going to be diligent and write good comments for each function, you might as well use doxygen tags (they are more descriptive, standard, used to generate automatic documentation of your code, and supported by most IDE for documentation and code highlighting).

With there recommendations, your list.h file could look like this:

#ifndef LIST_H
#define LIST_H

#include "ListOutOfExceptedRange.h"
#include "ListException.h"

//#pragma once //don't use non-standard compiler extensions.

// declaration of the class
template <typename T>
class list
{
public:
   typedef T value_type;
   typedef T& reference;
   typedef const T& const_reference;
   typedef std::size_t size_type;
   typedef std::ptrdiff_t difference_type;

   /// Default constructor.
   list();
   
   /**
    * Determines whether a list is empty.
    * \pre None.
    * \post Returns true if the list is empty,
    *       otherwise returns false.
    * \return True if the list is empty, false otherwise.
    * \throw None.
    */
   bool isEmpty() const;

   /**
    * Returns the number of elements currently stored in the list.
    * \pre None.
    * \post Returns 0 if empty otherwise returns the number of elements.
    * \return Returns 0 if empty otherwise returns the number of elements.
    * \throw None.
    */
   size_type size() const;

   /** Returns the element at an index.
    * \pre List is not empty.
    * \post If the element is found in the list, it is returned. If the element is           
    *       not found in the list, null is returned.
    * \param index the index of the element.
    * \return the element at the given index.
    * \throw ListOutOfExceptedRange If list is empty.
    */
   const_reference get(size_type index) const;

   /**
    * Add an element to the list.
    * \pre The element does not already exist in the list. 
           This is to say: no duplication allowed.
    * \post The element has been added to the end of the list.
    * \param index the index where to add the element.
    * \param newItem the value of the new element to add to the list.
    * \throw ListOutOfExceptedRange if the list is empty.
    * \throw ListException if element already exists in the list.
    */
   void add(size_type index, const_reference newItem);

  /**
   * Remove this element from the list.
   * \pre List is not empty.
   * \post If the element is found in the list, it is removed from the list 
   *       and returned. If the element is not found in the list, null is returned.
   * \param index the index of the element to be removed.
   * \throw ListOutOfExceptedRange if list is empty (or index is out-of-range).
   */
   void remove(size_type index);
  
  /**
   * Remove all elements from the list.
   * \pre None
   * \post size() returns 0 and empty() return true.
   * \throw None
   */
   void removeAll();
};

#endif

Edited by mike_2000_17: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.