Sie sind auf Seite 1von 12

Dr.

Frank Schindler is a Professor of Applied Informatics at


the Faculty of Informatics of the Pan-European University in
Bratislava. His education includes M.Sc. (1978), Dr. Nat. Sci.
(1980) in Mathematics from Charles University, Prague, Ph.D.
(1983) in Mathematics from the University of Utah, Salt Lake
City, Utah and Dr. Habil. (1994) in Informatics from Slovak Tech-
nical University, Bratislava. Besides that he has taught at the
University of Maryland University College, University of Miami, University of Utah,
Slovak Technical University and worked as software engineer for IBM Kingston,
New York, Prime Computer Inc., Framingham, Massachusetts and Diagonal Sys-
FRANK SCHINDLER
tems AG, Zürich, Switzerland. Among his research interests are: computer grap-
hics, geometrical modelling, object-oriented programming and computer secu-
rity.

DATA STRUCTURES
AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS

ISBN 978-80-7478-568-9

9 788074 785689
Názov projektu:

MEDZINÁRODNOU SPOLUPRÁCOU KU KVALITE VZDELÁVANIA


PEVŠ

Kód ITMS: NFP26140230012

dopytovo - orientovaný projekt

Moderné vzdelávanie pre vedomostnú spoločnosť


Projekt je spolufinancovaný zo zdrojov EÚ
FRANK SCHINDLER

Data structures
and algorithms

2014
Example of citation:
SCHINDLER, Frank. Data structures and algorithms. 1st ed. Prague: Wolters Kluwer,
2014. ISBN 978-80-7478-568-9.

This publication has been published with the financial support of European Union
within Operational Programme Education as a result of the project under the name
“Quality Education at the Pan-European University with International Cooperation”.
This publication reflects the views only of the author, and the European Union cannot be
held responsible for any use which may be made of the information contained therein.

Author Prof. RNDr. Frank Schindler, Ph.D.


Language Corrections SKRIVANEK SLOVENSKO, s.r.o.

Reviewers Juraj Štefanovič, Ph.D.


Assoc. Prof. Jaroslav Fogel, Ph.D.

First edition

© Copyright Pan-European University Slovakia, 2014


© Copyright Frank Schindler, 2014

ISBN 978-80-7478-568-9
CONTENtS

1 INTRODUCTION.......................................................................................... 7

2 TRENDS IN CONTROL STRUCTURES...................................................... 9


2.1 Abstraction in Control Structures.............................................................. 9
2.2 Parameter Passing...................................................................................... 10
2.2.1 One-Way Flow into the Function.................................................... 11
2.2.2 One-Way Flow out of the Function................................................. 11
2.2.3 Two-Way Flow into and out of the Function................................... 11

3 ABSTRACT DATA TYPES.............................................................................. 15


3.1 Data Abstraction via Classes...................................................................... 15
3.2 Classes in C++........................................................................................... 15
3.3 Class Specification and Implementation.................................................... 16
3.4 Modules and Information Hiding ............................................................. 18

4 SPECIFICATION OF DATA STRUCTURES................................................ 24


4.1 Data Types and Data Structures................................................................. 24
4.2 Hierarchy of Data Structures..................................................................... 24
4.3 Lists........................................................................................................... 24
4.4 Stacks........................................................................................................ 25
4.5 Queues...................................................................................................... 26
4.6 Binary Trees............................................................................................... 28
4.7 General Trees............................................................................................. 29
4.8 Graphs....................................................................................................... 30

5 DESIGN AND IMPLEMENTATION OF DATA STRUCTURES................. 32


5.1 Vector Implementation of a Stack.............................................................. 32
5.2 Stack as a Dynamic List............................................................................. 33
5.3 Circular Queue Representation via Vectors................................................ 35
5.4 Queue Representation via Linked Lists...................................................... 37
5.5 Binary Tree Representation via Linked Lists............................................... 39
5.5.1 Binary Tree Traversals...................................................................... 41
5.6 Binary Tree Representation via Vectors....................................................... 42
5.7 General Tree Representation via Linked Lists............................................. 43
5.8 Graph Representation via Linked Lists....................................................... 45
5.9 A Short Overview of Other Data Structures.............................................. 46
5.9.1 Arrays.............................................................................................. 46
5.9.2 Records........................................................................................... 47
5.9.3 Sets.................................................................................................. 47
5.9.4 Summary of Data Structures............................................................ 48

6 SEARCHING AND SORTING BIG DATA.................................................... 49


6.1 Program Speed and Efficiency.................................................................... 49
6.2 Big-O Notation......................................................................................... 50
6.3 Linear Lookup........................................................................................... 54
6.4 Binary Search............................................................................................. 54
6.5 Hash Table Lookup................................................................................... 57
6.6 N2 Sorting................................................................................................. 59
6.6.1 Straight Selection Sort..................................................................... 59
6.6.2 Bubble Sort .................................................................................... 60
6.6.3 Quick Sort ..................................................................................... 61
6.7 Heap Sort ................................................................................................. 64

7 SECURITY IN PROGRAMMING................................................................. 67
7.1 Introduction.............................................................................................. 68
7.2 Information Hiding................................................................................... 69
7.3 Defensive Programming............................................................................. 69
7.4 Assuming the Impossible........................................................................... 70
7.5 Programming Examples............................................................................. 71
7.5.1 Buffer Overflows............................................................................. 71
7.5.2 Missing Initialization of Variables.................................................... 72
7.5.3 Cryptic Code................................................................................... 72
7.5.4 Program Ignoring Error Messages.................................................... 72
7.5.5 Missing Null-Condition Restrictions............................................... 73
7.5.6 Dangling Pointers............................................................................ 73
7.5.7 Assertions........................................................................................ 74
7.5.8 Work with Library Functions.......................................................... 74
7.5.9 Paradoxes – Testing the Impossible (Variant of “Y2K Bug”)............. 75
7.6 Concluding Remarks................................................................................. 75

BIBLIOGRAPHY.................................................................................................. 77
1 INTRODUCTION
Life today: it’s much more complicated that it was a thousand years ago. It is
a well-known fact that beyond some limit the human mind is unable to cope
with too much complexity. Therefore there is an urgent need to use some sort of
a simple tool, let us call it an abstraction, to separate the essentials of an object or
an idea from its details which overwhelm us in the given moment of time. With
the concept of abstraction we focus on the “what”, not the “how”.
A simple example of this could be the postal delivery system. For an unbiased
viewer, it simply delivers letters and packages from senders to recipients. We
should not care so much about details on how this is done. Another interesting
example comes from programming itself.
We often use identifiers to denote variables, constants, functions and any other
complicated data type or object. It is just another form of a very useful abstrac-
tion. A well-chosen variable name can hide the details how the data values are
stored in memory of the computer and instead it stresses what is being stored
there.
Top-down design may also be looked at as an abstraction. At the top-most level
the problem solution may be imagined as the most abstract. Each next level con-
tains more and more details, in such a way that it should stay not too complex
at any specific level.
The functional abstraction is separating the function‘s behaviour from its imple-
mentation. By invoking a built-in function in the expression
sqrt (7*a + b)

the programmer should only know the function’s specification in the form of
a textual description of what the function sqrt does. Usage of such a function
8 CHAPTER 1

does not require any knowledge of its implementation details. One functional
abstraction can be layered at the top of another functional abstraction. A func-
tion FuncA can invoke FuncB that in turn may call further functions. This is
a quite typical case for the top-down design of larger software programs.
The abstraction approach should be applied to data as well. Data abstraction sep-
arates properties of a data type from details of its implementation. The data type
implementation involves a concrete data representation for the abstract values,
along with implementation of all its allowable operations in terms of program-
ming instructions in a chosen programming language. Data representation is
often done in multiple layers of abstraction. Each of them is more abstract than
the layer below it. More importantly the user of such a data abstraction only
needs to know its defining properties, not its implementation details. This con-
siderably reduces data complexity and the user is shielded from possible future
changes in the data implementation.
Recognition of the importance of data abstraction becomes fundamental in
modern software design. In our book, we will explore the use of abstraction
during program design, coding, testing and any modification process. The use
of abstraction techniques in this textbook does not prove all of its benefits right
away. However it may drastically simplify the big jump from writing small com-
puter programs into huge software projects. Without these abstraction tools we
would not be able to master such complex software programs that we are now
using in our everyday life. The book is concluded by a final chapter that relates
to safe programming techniques, that includes pinpointing typical programming
errors that C++ programmers make when they write programs and large software
projects.
BIBLIOGRAPHY
Arulanandham, J. J. – Calude, C. S. – Dinneen, M. J.: A fast natural algo-
rithm for searching. In: Theoretical Computer Science, 2004, Vol. 320, No. 1, p. 3-13.
ISSN 0304-3975
Bishop, M. – Frincke, D.: Teaching Robust Programming. In: IEEE Security and
Privacy, 2004, Vol. 2, No. 2, p. 54-57.
Blaha, M.: A Copper Bullet for Software Quality Management. In: Computer, 2004,
Vol. 37, No. 2, p. 21-25.
Booch, G. – Maksimchuk, R. A. – Engel, M. W. – Zouny, B. J. –
­Conallen, J. – Houstin, K. A.: Object-Oriented Analysis and Design with
Applications. 3rd Ed. MA, Reading : Addison-Wesley, 2007.
Carrano, F. M. – Henry, T.: Data Abstraction and Problem Solving with C++: Walls
and Mirrors. New Jersey : Prentice Hall, 2013. ISBN 0132923726
Choppy, C. – Bidoit M.: Recent Trends in Data Type Specification: 8th Workshop on
Specification of Abstract Data Types joint with the 3rd COMPASS Workshop. Heidel-
berg : Springer Verlag, 1993, 360 p. ISBN 540563792
Cormen, T. – Lieserson, C. – Rivest, R.:  Introduction to Algorithms. Cam-
bridge, Mass. : MIT Press, 1993, 1048 p. ISBN 0-262-03141-8
Cunto, W. – Gascon, J.:  Improving Time and Space Efficiency in Generalized
Binary Search Trees. In: Acta Informatica, 1987, Vol. 24, No. 5, p. 583-594.
DALE, N.: C++ Plus Data Structures. Jones and Barlett Learning, 2013, 804 p. ISBN
1-449-64675-1
Dijkstra, E.: Go to statement considered harmful. In: Communications of the ACM,
1968, Vol. 11, No. 3, p. 147–148.
Dobosiewicz, W.: Optimal Binary Search Trees. In: International Journal of Com-
puter Mathematics, 1986, Vol. 19, No. 2, p. 135-151.
78 BIBLIOGRAPHY

Ehring, H. – Orejas, F.: Recent Trends in Data Type Specification: 9th Workshop on
Specification of Abstract Data Types Joint with the 4th COMPASS Workshop. Heidel-
berg : Springer Verlag, 1994, 364 p. ISBN 3540578676
Esakov, J. – Weiss, T.: Data Structures – An Advanced Approach using C. New Jersey :
Prentice Hall, 1989, 372 p. ISBN 0-13-198847-6
Ferguson, N. – Schneier, B.: Practical Cryptography. Indianapolis, Indiana :
Wiley Publishing, 2003, 410 p. ISBN 0-471-22357-3
Goldberg, D.: What every computer scientist should know about floating-point
arithmetic. In: ACM Computing Surveys, 1991, Vol. 23, No. 1, p. 5-48. ISSN 0360-0300
Goodrich, M. T. – Tamassia, R. – Mount, D. M.: Data Structures and Algorithms
in C++. New Jersey : John Wiley and Sohn, 2011, 736 p. ISBN 978-0-470-38327-8
Headington, M. R. – Riley, D. D.: Data Abstraction and Structures using C++.
Lexington, Massachusetts : D. C. Heath and Company, 1994, 661 p. ISBN 0-669-
-29220-6
Horowitz, E. – Sahni, S. – Rajasekaran, S.: Computer Algorithms C++. New
York : Computer Science Press, 1997, 777 p. ISBN 0716783169
Josuttis, N. M.: The C++ Standard Library: A Tutorial and Reference. MA, Reading :
Addison-Wesley, 1999, 799 p. ISBN 0-201-37926-0
Klemens, B.: Modeling with Data: Tools and Techniques for Scientific Computing. New
Jersey : Princeton University Press, 2008, 472 p. ISBN 9780691133140
Knuth, D. E.: The Art of Computer Programming. Volume 3: Searching and Sorting.
2nd Ed. MA, Reading : Addison-Wesley, 1998, 800 p. ISBN 0201896850
Kochan, S. G.: Programming in C. 3rd Ed. Indianapolis : Sams Publishing, 2004,
576 p. ISBN 0672331411
Koffman, E. B. – Wolfgang, P. A. T.: Objects, Abstraction, Data Structures and
Design: Using C++. New Jersey : John Wiley and Sohn, 2006, 832 p. ISBN 0-471-
-46755-3
Main, M.: Data Structures and Other Objects using C++. New Jersey : Prentice Hall,
2010, 848 p. ISBN 0-132-12948-5
Main, M. – Savitch, W.: Data Structures and other Objects using C++. New Jersey :
Prentice Hall, 2011. ISBN 0132129485
Malik, D. S.: Data Structures Using C++. Boston, MA : Course Technology, 2009,
912 p. ISBN 0-324-78201-2
BIBLIOGRAPHY 79

Malik, D. S. – Sen, M. K.: Discrete Mathematical Structures, Theory and Applications.


Boston, MA : Course Technology, 2004, 905 p. ISBN 06192128553
Mead, N. R.: Who is Liable for Insecure Systems? In: Computer, 2004, Vol. 37, No. 7,
p. 27-34.
Mehlhorn, K. – Sanders, P.:  Algorithms and Data Structures. Heidelberg :
Springer Verlag, 2008, 300 p. ISBN 3-642-09682-4
Meyers, S.: Effective C++: 55 Specific Ways to Improve Your Programs and Designs.
3rd Ed. MA, Reading : Addison-Wesley Professional, 2005, 336 p. ISBN 0-321-33487-6
Morin, P.: Open Data Structures in C++. Available at: opendatastructures.org/ods-cpp.
pdf.
Nyhoff, L.: ADTs, Data Structures, and Problem Solving with C++. New Jersey : Pren-
tice Hall, 2005, 1042 p. ISBN 0-131-48758-2
Norman, D. A.: The Design of Everyday Things. New York : Basic Books, 2002, 288 p.
ISBN 9780465067
Oliveira, S. – Stewart, D. E.: Writing Scientific Software: A Guide to Good Style.
Cambridge : Cambridge University Press, 2006, 303 p. ISBN 0-521-67595-2
Palko, V.: Discrete Mathematics in Computer Science. Nitra, Slovakia : ForPress, 2014,
210 p. ISBN 978-80-970719-9.
Pfleger, C. P.: Security in Computing. New Jersey : Prentice-Hall International, 1997.
ISBN 0-13-185794-0
Schindler, F.: Coping with Security in Programming. In: Acta Polytechnica Hunga-
rica, 2006, Vol. 3, No. 2, p. 65-72. ISSN 1785-8860
Ullman, L. M. – Liyanage, M.: C Programming. San Francisco : Peachpit Press,
2004, 390 p. ISBN 0-321-28763-0
FRANK SCHINDLER

DATA STRUCTURES
AND ALGORITHMS

Editorial officeWolters Kluwer s. r. o.


Mlynské nivy 48, 821 09 Bratislava
Cover design by Christian
www.christianstudio.sk

Published by Wolters Kluwer, a. s.,


U Nákladového nádraží 6, 130 00 Prague 3,
Czech Republic,
in 2014 as its 1 744th publication.

First edition

80 pages. Format: A5. Impression: 200 copies.


Typography by Forma, s. r. o., Bratislava.
Printed by Tlačiareň P+M, Slovakia.

www.wkcr.cz, e-mail: knihy@wkcr.cz


tel.: +420 246 040 405, +420 246 040 444, fax: +420 246 040 401

ISBN 978-80-7478-568-9

Das könnte Ihnen auch gefallen