Programming in C and Data Structures Luis Joyanes
Programming in C and Data Structures Luis Joyanes
INTRODUCTION
Welcome to Programming in C, 2nd edition. The preface of the first edition opened with the question-
Why a book about C at the beginning of the centuryXXIWe indeed wrote the book in the year 2000 and
was published in the year 2001. On this occasion, four years later, we have written the new edition in the
2004 and 2005, and it will be published in 2005. What has changed since then in the world of programming?
What? Really, a little and a lot. It remains the most used language for learning fundamentals and
programming techniques both in the university and in technological institutes and training centers
Professional training. C++ continues to reign in the learning of object-oriented programming and Java with
C# has become the premier language in the professional programming world, engineering-
software development, web engineering, telecommunications, and in general, the software industry.
C is an ideal language for learning computer programming. C is a very compact language.
since its syntax is simple and easy to learn for writing real applications. It is also a language
very powerful since it is widely used in programming at all levels, from device drivers
sensors and components of operating systems to large-scale applications. There are C compilers.
for any programming environment and operating systems, such as Windows, Unix, Linux, Mac,
etc., so that when you have learned C you will be able to program in any
text and current environment. You will also observe that C is an excellent basis for continuing your training in pro-
object-oriented programming with C++ and then migrate to Java or C# depending on the professional environment
to develop..
All university degrees in Sciences and Engineering, as well as vocational training studies
they require a basic course in algorithms and programming with a powerful and professional language but
it should be simple and easy to use. C is ideal for learning to program directly the algorithmic techniques.
or programming, or in parallel with subjects such as Introduction, Fundamentals, or Method.
Logic of Programming when using an algorithmic language or a structured programming language
C remains the most widely used and recommended universal language in university curricula.
and training centers around the world. Organizations such as ACM, IEEE, professional colleges, continue
recommending the need for in-depth knowledge of techniques and programming languages
structured with the objective of "accommodating" the student's training to the conception, design and construction
creation of algorithms and data structures. The deep knowledge of algorithms combined with reliable techniques-
Rigorous and efficient programming techniques prepare the student or self-taught individual for high performance.
in programming and to take on the challenges of object-oriented programming in an initial phase and the téc-
techniques and methods inherent to software engineering in another more advanced phase.
XXmmProgramming in C: Methodology, Algorithms, and Data Structures
the official website of the book. However, McGraw-Hill's authorization allows the reader to
this second edition may include all the appendices from the first edition, as well as some new ones.
on the official Internet address of the book (http://www.mhe.es/joyanes) where you can see all
they and download to their computer those they wish and need for their learning and training
actual or future.
It will also be included on the official book page in addition to the appendices, teaching material com-
supplementary material from the authors with standards, rules, exercises and solved problems, class notes, bibliography
updated graphics, reference websites in programming, etc. that gradually aid learning
involvement of the reader in their professional career as a programmer or in their studies of computer science or
computational systems, or in the different engineering and scientific areas.
The book is predominantly educational for formal teaching of computer programming, but
it does not presume any prior knowledge of programming, so it can also be used by
self-taught readers with or without training in computer science or computational sciences.
To use this book and get the most out of it, a computer with a...
C compiler, a library installed so that the examples from the book can be run, and an editor of
text to prepare your source code files. There are numerous C compilers on the market and
also numerous shareware versions (free of charge) available on the Internet. Ideally, it should be
give a compiler that is compatible with the standard version of C of the American National Standards Ins-
tute (ANSI) which is the version used in writing this book. Most of the current compile-
22mmProgramming in C: Methodology, algorithms, and data structure
available C++ libraries, either commercial or public domain, support C, so this may be
a highly recommended option.
You can use any text editor, such as Notepad or Vi, to create your files.
source grams, although it will be much better to use a specific editor for editing C code, like those that
they usually come with integrated development environments either for Windows or for Linux. However,
should not use a word processor, such as Microsoft Word, since word processors usually
text or commercial text treatment, embed or 'embed' format codes in the text that
they will not be understood by their compiler.
In any case, if you are taking a formal course, the best method to study this book is to follow
the advice of your teacher and professor both for your theoretical training and for your practical training. If
you are self-taught or study independently, the recommendation then will be to compile, execute-
and debug your programs, both those proposed in the book and those you design, at mid-
as I read the book, trying to understand the logic of the algorithm and the syntax of the language in each
exercise that I did.
Chapter 1. Introduction to computers and programming languages. Explains and describes the
fundamental concepts of computing and programming languages. The knowledge-
Complete knowledge of the chapter's content is not an essential requirement for understanding.
to your learning of the remaining chapters although it is fundamental in your training of pro-
Programming. Under these circumstances, the reader/student can choose to study it all at once.
or adjust learning throughout their entire programming course. It is always counted
also with the professor and teacher who will be able to guide the student in their teaching progression. For
readers who have not taken any introductory course on computers or information
We recommend reading it before moving on to the next chapter and then rereading it.
at the time and in the manner that they themselves consider. This chapter has been updated total-
I lied about the first edition, and both the fundamental concepts and
traditional generics like the latest technological innovations that most affect the world
of programming.
Chapter 2. Methodology of programming and software development. The reader is introduced to the
Fundamental concepts of algorithms and their representation tools. Likewise, it is dis-
they write about the classical types of programming with special emphasis on structured programming
C language support. A brief introduction to the software lifecycle and to the
formal methods of program verification aims for the reader to mentally prepare themselves from the
beginning of their training in programming on the importance of these topics in the development
the software is modifiable and efficient.
Chapter 3. The C language. Basic elements.Introduces the structure and the main components.
speak of a C program. Teach the meanings of the fundamental elements of
all programs, such as data, constants, variables, and the basic input operations
from/exit.
Chapter 4. Operators and Expressions. It describes the use of arithmetic and relational operators.
logical operations for the manipulation of operations and expressions in C. Operations are also studied
special radars and type conversions, along with rules of priority and associativity
the operators in mathematical expressions and operations.
Chapter 5. Selection structures: statementsyfi Introduce the selection statements
basic and fundamental in any program. The use of compound statements is examined.
or blocks as well as the use of conditional operators and evaluation of logical expressions.
Chapter 6. Control structures: loops. The concept of loop or cycle is learned and the way to continue
to control the execution of a program through statementsfor, while, do-while. Tam-
The concept of nesting loops and empty loops is well explained; examples are provided.
useful tools for the efficient design of loops.
Chapter 7. Functions. Examines functions in C, an important part of programming. It is described
the concept of structured programming - a method of program design that emphasizes on
the top-down approach to problem solving through the decomposition of a pro-
big problem in lower-level problems that are implemented with functions.
Chapter 8. Recursion. Recursion is the property of a function or expression to call itself.
It is one of the most important techniques in algorithm construction. For this reason
a whole chapter is dedicated to the learning of recursive functions. In the first edition-
this concept was studied within the chapter on functions.
Chapter 9. Arrays (lists and tables). Explains a simple yet powerful method of data storage.
You learn how to group similar data in arrays or "arrays" (lists and tables) numerical
Chapter 10. Sorting and Searching Algorithms. It teaches the methods to sort lists and tables, thus
how to search for data in lists and tables. The simplest classical algorithms are studied
and efficient for both sorting and searching.
XXIVmmProgramming in C: Methodology, algorithms, and data structure
Chapter 11 Structures and joints. Basic concepts of structures, joints, and lists are described.
actions: declaration, definition, initiation, use, and size. The fundamental operations of
access to structures, arrays of structures and nested structures are also analyzed in this
chapter. In the chapter, it is practically shown how to use structures and unions to
meet the needs of the program; the differences between structures and unions are explained
yes, as well as the use of the reserved wordtypedef.
Chapter 12. Pointers. It presents one of the most powerful and efficient features of
C language, pointers. This chapter provides a detailed explanation of pointers.
pointer arrays, string pointers, pointer arithmetic, constant pointers, pointer-
ros as function arguments, pointers to functions and to structures. In a practical way-
tico learns how to use pointers to pointers and how they can be used with arrays
pointers to manipulate strings, which will be studied in depth in chapter 14.
Chapter 13. Dynamic memory allocation. It describes the dynamic management of memory and the functions
associated actions for those tasks:alloc(),free(),calloc()andrealloc().Pro-
they provide operational rules for those functions and rules for memory allocation.
Chapter 14. Strings. The concept of a string is described as well as the relationships between pointers.
arrays and strings in C. Basic concepts of string manipulation are introduced along with
basic operations such as length, concatenation, comparison, conversion, and search
of characters and strings. The most notable functions of the library are described.string.h
Chapter 15. File Input and Output. The concept of stream is studied and the different methods
two file opening methods, along with the concepts of binary files and functions for
random access. It shows practically how C uses streams, examining the flow-
predefined options and the practical mode of working with the screen, the printer, and the keyboard.
Chapter 16. Data organization in a file. The classical concepts of records and organization
Files are studied in the chapter. Two types of special files such as the seq.
indexed lists and with hash addressing for a specific study reason. Lastly
Sorting methods for files are analyzed both externally and by direct merging.
Chapter 17 Data types (ADT/Objects). Object-oriented programming is the most
important after the structured paradigm. The role of abstraction, modularity and
The abstract data types are analyzed in this chapter. The specification and ...
implementation of abstract data types in C as the first level of objects.
Chapter 18. Linked Lists. A linked list is a data structure that maintains a collection
of elements, but the number of them is not known in advance or varies in a wide range.
The linked list consists of elements that contain a value and a pointer. The chapter-
it describes the theoretical foundations, types of lists, and operations that can be performed on
the linked list.
Chapter 19. Stacks and queues. The most commonly used data structures from the point of view of abstraction
The implementation involves stacks and queues. Their structure, design, and manipulation of the algorithms.
the most basic are explained in the chapter.
Chapter 20 Trees. Non-linear and dynamic data structures are widely used in programs.
Trees are one of the most well-known structures in algorithms and programming.
since they are the foundation for advanced programming techniques.
Prologue to the 2nd editionmm25
APPENDICES
Compilation of C programs on Windows
Bibliography and programming resources