, SCHAUM’S OUTLINE OF
THEORY AND PROBLEMS
of
PROGRAMMING
WITH C++
Second Edition
·
JOHN R. HUBBARD, Ph.D.
Professor of Mathematics and Computer Science
University of Richmond
·
SCHAUM’S OUTLINE SERIES
McGRAW-HILL
New York San Francisco Washington, D.C. Auckland Bogota′ Caracas
Lisbon London Madrid Mexico City Milan Montreal
New Delhi San Juan Singapore Sydney Tokyo Toronto
TeamLRN
,McGraw-Hill
abc
Copyright © 2000, 1996 by the McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as permitted
under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in
a database or retrieval system, without the prior written permission of the publisher.
0-07-136811-6
The material in this eBook also appears in the print version of this title: ISBN 0-07-135346-1.
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we
use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such
designations appear in this book, they have been printed with initial caps.
McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training pro-
grams. For more information, please contact George Hoare, Special Sales, at or (212) 904-4069.
TERMS OF USE
This is a copyrighted work and The McGraw-Hill Companies, Inc. (ÒMcGraw-HillÓ) and its licensors reserve all rights in and to t he work. Use of
this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work,
you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate,
sell, publish or sublicense the work or any part of it without McGraw-HillÕ s prior consent. You may use the work for your own noncommercial
and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these
terms.
THE WORK IS PROVIDED A Ò S ISÓ. McGRA W-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE
ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY
INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM
ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR
FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work
will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you
or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has
no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be
liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even
if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether
such claim or cause arises in contract, tort or otherwise.
DOI: 10.1036/0071368116
, Preface
Like all Schaum’s Outline Series books, this volume is intended to be used primarily for self
study, preferably in conjunction with a regular course in C++ programming language or
computer science. However, it is also well-suited for use in independent study or as a reference.
The book includes over 200 examples and solved problems. The author firmly believes that
the principles of data structures can be learned from a well-constructed collection of examples
with complete explanations. This book is designed to provide that support.
C++ was created by Bjarne Stroustrup in the early 1980s. Based upon C and Simula, it is
now one of the most popular languages for object-oriented programming. The language was
standardized in 1998 by the American National Standards Institute (ANSI) and the International
Standards Organization (ISO). This new ANSI/ISO Standard includes the powerful Standard
Template Library (STL). This book conforms to these standards.
Although most people who undertake to learn C++ have already had some previous
programming experience, this book assumes none. It approaches C++ as one’s first programming
language. Therefore, those who have had previous experience may need only skim the first few
chapters.
C++ is a difficult language for at least two reasons. It inherits from the C language an
economy of expression that novices often find cryptic. And as an object-oriented language, its
widespread use of classes and templates presents a formidable challenge to those who have not
thought in those terms before. It is the intent of this book to provide the assistance necessary for
first-time programmers to overcome these obstacles.
Source code for all the examples and problems in this book, including the Supplementary
Problems, may be downloaded from these websites http://projectEuclid.net/schaums ,
http://www.richmond.edu/~hubbard/schaums, http://hubbards.org/schaums, or
http://jhubbard.net/schaums . Any corrections or addenda for the book will also be
available at these sites.
I wish to thank all my friends, colleagues, students, and the McGraw-Hill staff who have
helped me with the critical review of this manuscript, including John Aliano, Arthur Biderman,
Francis Minhthang Bui, Al Dawson, Peter Dailey, Mohammed El-Beltagy, Gary Galvez, Libbie
Geiger, Sergei Gorlatch, Chris Hanes, John B. Hubbard, Raana Jeelani, Dick Palas, Blake Puhak,
Arni Sigurjonsson, Andrew Somers, Joe Troncale, Maureen Walker, Stefan Wentzig, and Nat
Withers. Their editorial advice and debugging skills are gratefully appreciated.
Special thanks to my wife and colleague, Anita H. Hubbard, for her advice, encouragement,
and creative ideas for this book. Many of the original problems used here are hers.
JOHN R. HUBBARD
Richmond, Virginia
iii
Copyright 2000 The McGraw-Hill Companies, Inc. Click Here for Terms of Use.
TeamLRN
THEORY AND PROBLEMS
of
PROGRAMMING
WITH C++
Second Edition
·
JOHN R. HUBBARD, Ph.D.
Professor of Mathematics and Computer Science
University of Richmond
·
SCHAUM’S OUTLINE SERIES
McGRAW-HILL
New York San Francisco Washington, D.C. Auckland Bogota′ Caracas
Lisbon London Madrid Mexico City Milan Montreal
New Delhi San Juan Singapore Sydney Tokyo Toronto
TeamLRN
,McGraw-Hill
abc
Copyright © 2000, 1996 by the McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as permitted
under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in
a database or retrieval system, without the prior written permission of the publisher.
0-07-136811-6
The material in this eBook also appears in the print version of this title: ISBN 0-07-135346-1.
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we
use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such
designations appear in this book, they have been printed with initial caps.
McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training pro-
grams. For more information, please contact George Hoare, Special Sales, at or (212) 904-4069.
TERMS OF USE
This is a copyrighted work and The McGraw-Hill Companies, Inc. (ÒMcGraw-HillÓ) and its licensors reserve all rights in and to t he work. Use of
this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work,
you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate,
sell, publish or sublicense the work or any part of it without McGraw-HillÕ s prior consent. You may use the work for your own noncommercial
and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these
terms.
THE WORK IS PROVIDED A Ò S ISÓ. McGRA W-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE
ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY
INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM
ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR
FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work
will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you
or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has
no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be
liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even
if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether
such claim or cause arises in contract, tort or otherwise.
DOI: 10.1036/0071368116
, Preface
Like all Schaum’s Outline Series books, this volume is intended to be used primarily for self
study, preferably in conjunction with a regular course in C++ programming language or
computer science. However, it is also well-suited for use in independent study or as a reference.
The book includes over 200 examples and solved problems. The author firmly believes that
the principles of data structures can be learned from a well-constructed collection of examples
with complete explanations. This book is designed to provide that support.
C++ was created by Bjarne Stroustrup in the early 1980s. Based upon C and Simula, it is
now one of the most popular languages for object-oriented programming. The language was
standardized in 1998 by the American National Standards Institute (ANSI) and the International
Standards Organization (ISO). This new ANSI/ISO Standard includes the powerful Standard
Template Library (STL). This book conforms to these standards.
Although most people who undertake to learn C++ have already had some previous
programming experience, this book assumes none. It approaches C++ as one’s first programming
language. Therefore, those who have had previous experience may need only skim the first few
chapters.
C++ is a difficult language for at least two reasons. It inherits from the C language an
economy of expression that novices often find cryptic. And as an object-oriented language, its
widespread use of classes and templates presents a formidable challenge to those who have not
thought in those terms before. It is the intent of this book to provide the assistance necessary for
first-time programmers to overcome these obstacles.
Source code for all the examples and problems in this book, including the Supplementary
Problems, may be downloaded from these websites http://projectEuclid.net/schaums ,
http://www.richmond.edu/~hubbard/schaums, http://hubbards.org/schaums, or
http://jhubbard.net/schaums . Any corrections or addenda for the book will also be
available at these sites.
I wish to thank all my friends, colleagues, students, and the McGraw-Hill staff who have
helped me with the critical review of this manuscript, including John Aliano, Arthur Biderman,
Francis Minhthang Bui, Al Dawson, Peter Dailey, Mohammed El-Beltagy, Gary Galvez, Libbie
Geiger, Sergei Gorlatch, Chris Hanes, John B. Hubbard, Raana Jeelani, Dick Palas, Blake Puhak,
Arni Sigurjonsson, Andrew Somers, Joe Troncale, Maureen Walker, Stefan Wentzig, and Nat
Withers. Their editorial advice and debugging skills are gratefully appreciated.
Special thanks to my wife and colleague, Anita H. Hubbard, for her advice, encouragement,
and creative ideas for this book. Many of the original problems used here are hers.
JOHN R. HUBBARD
Richmond, Virginia
iii
Copyright 2000 The McGraw-Hill Companies, Inc. Click Here for Terms of Use.
TeamLRN