< previous page page_1116 next page >

Page 1116
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
struct NodeType
{
    int     number;
    NodePtr link;
};
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
and the function prototype
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
void Exchange( /* in */ NodePtr head,
               /* in */ int     key  );
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
implement the Exchange function. The function searches a linked list for the value given by key and exchanges it with the number preceding it in the list. If key is the first value in the list or if key is not found, then no exchange occurs.
10. Using the type declarations given in Exercise 9, write a function that reorganizes the items in a linked list so that the last item is first, the second to last is second, and so forth. (Hint: Use a temporary list.)
Programming Problems
1. In the Solitaire program in this chapter, all insertions into a linked list are made using the function InsertTop, and all the deletions are made using RemoveTop. In some cases, this is inefficient. For example, function CardDeck::Recreate takes the cards from onTable and moves them one by one to the deck. Then the cards on discardPile are moved one by one to the deck. It would be more efficient simply to concatenate (join) the deck list and the onTable list, then concatenate the resulting list and the discardPile list.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
To the CardPile class, add a member function whose specification is the following:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
void Concat( /* inout */ CardPile& otherList );
    // Postcondition:
    //     This list and otherList are concatenated (the front
    //     of otherList is joined to the rear of this list)
    //  && otherList is empty
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Implement and test the Concat member function. Use it in the CardDeck::Recreate function of the Solitaire program to make the program more efficient.
2. In Chapter 15, the BirthdayCalls program uses a DateType class to process address book information found in a file friendFile. Entries in friendFile are in the form
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
John Arbuthnot
(493) 3842938
1/12/1970

Mary Smith
(123) 1234567
10/12/1960

 
< previous page page_1116 next page >