Schultz Composition: Northeast Compositors Cover Design: Night & Day Design Manufacturing Buyer: Therese Bräuer Printing and Binding: Courier Stoughton Cover Printing: Courier Stoughton
Trang 1Data Structures
Structures
J O N E S A N D B A R T L E T T C O M P U T E R S C I E N C E
A Laboratory Course in
James Robergé Stefan Brandle David Whittington
Second Edition
Trang 2Data Structures
Data Structures
S e c o n d E d i t i o n
A Laboratory Course in
James Robergé
Illinois Institute of Technology
Stefan Brandle
Taylor University
David Whittington
Taylor University
Trang 3Copyright © 2003 by Jones and Bartlett Publishers, Inc.
Cover image © Douglas E Walker / Masterfile
All rights reserved No part of the material protected by this copyright notice may be reproduced or utilized
in any form, electronic or mechanical, including photocopying, recording, or any information storage or retrieval system, without written permission from the copyright owner
Library of Congress Cataloging-in-Publication Data
Robergé, Jim.
A laboratory course in C++ data structures / James Robergé, Stefan Brandle, David Whittington.
p cm.
ISBN 0-7637-1976-5
1 C++ (Computer program language) I Brandle, Stefan II Whittington, David III.
Title.
QA76.73.C153R58 2003
005.13’3—dc21
2002044401 Editor-in-Chief, College: J Michael Stranz
Production Manager: Amy Rose
Associate Editor: Theresa DiDonato
Associate Production Editor: Karen C Ferreira
Production Assistant: Jenny L McIsaac
Senior Marketing Manager: Nathan J Schultz
Composition: Northeast Compositors
Cover Design: Night & Day Design
Manufacturing Buyer: Therese Bräuer
Printing and Binding: Courier Stoughton
Cover Printing: Courier Stoughton
Printed in the United States of America
07 06 05 04 03 10 9 8 7 6 5 4 3 2 1
World Headquarters
Jones and Bartlett Publishers
40 Tall Pine Drive
Sudbury, MA 01776
978-443-5000
info@jbpub.com
www.jbpub.com
Jones and Bartlett Publishers Canada
2406 Nikanna Road Mississauga, ON L5C 2W6 CANADA
Jones and Bartlett Publishers International
Barb House, Barb Mews London W6 7PA UK
Trang 4To my son Edward, who lets me see a world of wonder through his eyes And to my wife, Ruby, who creates that world.
—James Robergé
To Christina, Anna, and Esther: my queen and little princesses.
—Stefan Brandle
In memory of my kitty Sweetpea.
—David Whittington
Trang 6• int * ptr Just for completeness, ptris a pointer to integer Both
the data pointed at and where the pointer points can be modified
It is good software engineering practice to give a function only as much access to
data as it absolutely needs The results are more complicated-looking code, but it can
save tons of time that otherwise would be spent debugging to determine how and
where something was getting magically changed So if a function uses an array but
doesn’t need to change the data, it would be best passed as const type * ptr, or
even const type * const ptr
Pointer math: Pointers can be incremented ( ++) and decremented ( ), integers
can be added or subtracted from pointers (+, +=, —, -=) or one pointer may be
subtracted from another The general rule to remember is that the arithmetic is always
done in terms of the size of the data type pointed at For example, ptr += 2means
“change what we are pointing at by 2 * sizeof the data type”; if the datatype is 8 bytes,
then ptr now points 2 * 8 = 16bytes higher ptr2 — ptr1gives you the difference
between the two pointers as a difference between the array index value of ptr2 and
ptr1, so if ptr2points to array[3]and ptr1points to array[1], ptr2 — ptr1 =
2 (since the ptr2 points to an element two elements away from ptr1) Pointer math
only makes sense in the context of an array
C-Strings: C-strings are just pointers to an array of characters The string end is
calculated by starting at item 0 in the array and moving up through the indexed
characters until a character with the value ‘\0’ is found; that is the end of the string
Note: There is a big difference between an empty string (the first item in the array has
the value ‘\0’) and a null string (the pointer has the value NULL, or (char *) 0) A
NULL pointer by our definition doesn’t point at any data
Pointers | 411