|
|
 |
|
|
|
|
cin >> ssNumber;
while (cin)
{
ptr = new PersonNode;
ptr->ssNum = ssNumber;
ptr->next = head;
head = ptr;
cin >> ssNumber;
}
ptr = head;
while (ptr != NULL)
{
cout << ptr->ssNum;
ptr = ptr->next;
} |
|
|
|
|
|
|
|
|
Programming Warm-Up Exercises |
|
|
|
|
|
|
|
|
1. To the OrdList class of this chapter, add a value-returning member function named Length that counts and returns the number of nodes in a list. |
|
|
|
|
|
|
|
|
2. To the OrdList class, add a value-returning member function named Sum that returns the sum of all the data values in a list. |
|
|
|
|
|
|
|
|
3. To the OrdList class, add a Boolean member function named IsPresent that searches a list for a particular value (passed as a parameter) and returns TRUE if the value is found. |
|
|
|
|
|
|
|
|
4. To the OrdList class, add a Boolean member function named Equal that compares two class objects and returns TRUE if the two lists they represent are identical. |
|
|
|
|
|
|
|
|
5. Rewrite the InsertTop and Insert functions of the OrdList class so that the program prints an error message and halts if the new operator fails to obtain space on the free store. (To halt the program, use the exit function supplied by the C++ standard library.) Be sure to revise the function postconditions for the new versions. |
|
|
|
|
|
|
|
|
6. Redo Exercise 5 as follows. Instead of halting the program if the new operation fails, each function should return (as a parameter) a Boolean flag indicating that the insertion failed. |
|
|
|
|
|
|
|
|
7. The OrdList class provides a copy-constructor but not a deep copy operation. Add a CopyFrom function to the OrdList class that performs a deep copy of one class object to another. |
|
|
|
|
|
|
|
|
8. To avoid special handling of empty linked lists for insertion and deletion routines, some programmers prefer to keep a dummy node permanently in the list. (The list is considered empty if it contains only the dummy node.) Rewrite the OrdList class to use a dummy node whose component value is equal to a constant named DUMMY. Do not keep any unnecessary code. (Hint: The first element of the list follows the dummy node.) |
|
|
|
|
|
|
|
|
9. Given the declarations |
|
|
|
 |
|
|
|
|
struct NodeType;
typedef NodeType* NodePtr; |
|
|
|
|
|