Topic: Programming Problem


Author: jeff.price@windmill.com (Jeff Price)
Date: 24 Feb 95 03:02:00 GMT
Raw View
I am rather new to C++ and I am having BIG problems with classes and
structures.  I am currently enrolled in a second semester Computer
Science course.  The following is a portion of a programing assignment
that we have to complete.

If I could get past the classes, member functions, and other various
related items, I could get this program finished, but I am having a
little trouble getting started.

Any information that anyone could provide to would help a great deal.
After the assignment I will put MY CURRENT HEADER file which contains
the class definitions and so on...  If someone could look at it and tell
me what I'm doing right or wrong, I would appreciate it.  I do not
really understand where various functions go in the class.

This is probably an "easy" assignment to most of you, so please do not
laugh. <g>.  I'm not looking for THE answer, just some help.

     Write a well-structured and well-documented C++ program which
     will manipulate linked list of grocery records.  You need to
     create a record structure named GROCERYDATA which will include
     each of the data parts below PLUS a pointer to the next node in
     the link.  These definitions will go in the P5xxxB01.D file.
     Don't forget to declare a GROCERYDATA_PTR typedef.

     Additionally, you will declare
          #define NULL  0
          typedef enum sortchoice {ByType, ByName} SORTBY;
     and the structure typedefs in the class header.  However, do
     not put any of them within the class itself.

     You will have three attributes in the class:

          1.A head pointer to the unsorted list.
          2.A head pointer to a list which will be sorted (and
            resorted) on different keys.
          3.An integer which contains the total number items in the
            linked list.

     You should only need two constructors: the default and copy
     constructors.  Additionally, you will need a destructor.

     You are to write at least the following methods.

          1.A public method which receives as a parameter a
            structure which the user loads with information from
            the input file.  It is the method's responsibility to
            put it into the linked list.  The method will add it at
            the end of the unsorted list and increase the number of
            items by one.
          2.A public overloaded operator << which will print
            GROCERYDATA records in tabular form with reasonable
            column headings FROM THE UNSORTED LINKED LIST.  This
            operator prints the entire linked list.  (It will
            require the friend keyword to gain access to the
            private head pointer.)  Use the same spacing as appears
            in the input file.  It should print a blank line before
            and after printing the linked list.  If the head
            pointer for this list is empty, then print EMPTY LIST.
          3.A public operator < which will print the GROCERYDATA
            records as in the previous operator EXCEPT FROM THE
            SORTED LINKED LIST.
          4.A public overloaded operator  =  which will allow one
            object to be assigned another object.  Its
            implementation will require use of the copy constructor
            which will create a new copy with its own dynamically
            allocated linked list.
          5.A public method to sort the array of records into
            ascending order using one of two keys: the specific
            food type field or the brand name.  If the head pointer
            of the sorted list is not null (i.e. there is a list)
            the method must first delete the current list (freeing
            the memory for each node), then copy the original list
            into it (creating a new list dynamically), then sorting
            that list using a bubble sort.  NOTE: When doing the
            swap you need to keep track of all the pointers and set
            them properly.
          6.A public method which receives the specific food type
            (string) of the item to be deleted which returns TRUE
            if successfully found and deleted, FALSE otherwise.
            This method will call the private search method listed
            below to find the item.
          7.A private method which uses a recursive, sequential
            search algorithm to locate a record in the original
            unsorted linked list, given the specific data type.  It
            receives the specific data type as a parameter.
            Return, as its function value, the pointer to the node
            of the record if it is found, or NULL if it is not
            found.
          8.A private method which will accept as a parameter
            either of the head pointers and will delete and free
            the memory for each node in the list.  This method will
            be used by method 4 above as well as by the destructor.



There's more, but this is all I need.  Here's my code thus far:

     #ifndef GROCERY_H
     #define GROCERY_H

     #define NULL 0

     typedef enum sortchoice {ByType, ByName} SORTBY;

     typedef struct LIST* GROCERYDATA_PTR;

     typedef struct GROCERYDATA {
      char szGenFoodClass[19];
      char szSpecFoodType[19];
             char szBrandName[19];
             char szAisleLoc[1];
      char cLeftOrRight;
           char cVertPos;
      char cHorzPos;
      char szInformation[14];
             GROCERYDATA_PTR pNext;
     }GROCERYDATA;

     //----- FORWARD REFRENCES ----------------------
     class ostream;

     //----- CLASS GROCERY --------------------------
     //---------- GROCERY LIST DATA ABSTRACTION -----
     class grocery {
     public:
 //Constructors
 grocery ();
 grocery (const GROCERYDATA &);

 //Destructor
 -grocery ();

 //Accessor functions
 bool AddToList (GROCERYDATA &);
 void SortList (GROCERYDATA &);
 bool DeleteData (char *);

 //Overloads
 ostream & operator << (ostream &, const grocery &);
 void operator = (const grocery &);

 //Assignments
 ostream & operator < (ostream &, const grocery &);

     private:
 //Data areas
 GROCERYDATA_PTR pSortedHead=NULL;
 GROCERYDATA_PTR pUnSortedHead=NULL;
 int iRecordCount;

 grocery & SearchList (char *);
 void DeleteList (GROCERYDATA_PTR);
};
#endif



     1> What do the default and copy constructors do... aren't those
          functions defined by overloading the = ?  What do you set the
          default to?

That's all I can think of.  For any of you still reading this, I would
really appreciate your help.  Even if I can't turn it in on time.

---
* CmpQwk #UNREG* UNREGISTERED EVALUATION COPY