0% found this document useful (0 votes)
2 views182 pages

Data Structures

The document is a comprehensive overview of data structures, detailing their importance in organizing and storing data efficiently for algorithms. It outlines various data structures such as stacks, queues, linked lists, and trees, and discusses their applications in programming, particularly using the C language. The book is structured into eight chapters, covering foundational concepts, functions, pointers, and algorithm analysis.

Uploaded by

muhmmedkare6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views182 pages

Data Structures

The document is a comprehensive overview of data structures, detailing their importance in organizing and storing data efficiently for algorithms. It outlines various data structures such as stacks, queues, linked lists, and trees, and discusses their applications in programming, particularly using the C language. The book is structured into eight chapters, covering foundational concepts, functions, pointers, and algorithm analysis.

Uploaded by

muhmmedkare6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‫ﻤﻌ‬

‫ﻬﺪ‬
‫ﺍﻟ‬
‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Data

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮ‬ ‫ﻮﻟ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
Structures

‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
Computer Science
-1

‫ﺩﺍ‬
2nd Year

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

Prof Dr. Mohamed Samy Rady


42
-2

Professor of Accounting and Auditing


8

Dean of Higher Institute of Management and Information Technology


1
1-
-1
25

Dr. Mona Mowafy


20

Information System Lecturer


Higher Institute of Management and Information Technology

1
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Introduction

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
A data structure is defined as a group of data elements used for organizing

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
and storing data. In order to be effective, data has to be organized in a

‫ﻤﻌ‬
‫ﻠﻮ‬
manner that adds to the efficiency of an algorithm, and data structures

‫ﻣﺎ‬
‫ﺕ‬
such as stacks, queues, linked lists, heaps, and trees provide different

‫ﺑﻜ‬
‫ﻔﺮ‬
capabilities to organize data.

‫ﺍﻟ‬
‫ﺸﻴ‬
While developing a program or an application, many developers find

-‫ﺦ‬
themselves more interested in the type of algorithm used rather than the
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
type of data structure implemented. However, the choice of data structure
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
used for a particular algorithm is always of the utmost importance. Each
18

‫ﻰ‬
1-

‫ﻟﻺ‬
data structure has its own unique properties and is constructed to suit
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

various kinds of applications. Some of them are highly specialized to carry

‫ﻭ‬
20

‫ﺗﻜ‬
out specific tasks. Specific data structures are essential components of

‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
many efficient algorithms, and make possible the management of large
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
amounts of data, such as large databases and Internet indexing services. C,
‫ﻠﻮ‬
‫ﻣﺎ‬

as we all know, is the most popular programming language and is


‫ﺕ‬
‫ﺑﻜ‬

widespread among all the computer architectures. Therefore, it is not only


‫ﻔﺮ‬
‫ﺍﻟ‬

logical but also fundamentally essential to start the introduction and


‫ﺸﻴ‬
-‫ﺦ‬

implementation of various data structures through C. The course of data


76

structures is typically taught in the second or third semester of most


42
-2

engineering colleges and across most engineering disciplines in India. The


8

aim of this course is to help students master the design and applications of
1
1-
-1

various data structures and use them in writing effective programs. The
25

objective of this book is to introduce the concepts of data structures and


20

apply these concepts in problem solving. The book provides a thorough


and comprehensive coverage of the fundamentals of data structures. This
book is organized into 8 chapters.

2
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
- Chapter 1, Introduction: provides a detailed explanation of data

‫ﺩﺍ‬
‫ﺭﺓ‬
structure concept and difference between the concepts of data

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
structures and algorithms.

‫ﻮ‬
‫ﺟﻴﺎ‬
- Chapter 2, Functions: This chapter describe functions through

‫ﺍﻟ‬
‫ﻤﻌ‬
explaining how to define and call functions, how to send data to the

‫ﻠﻮ‬
‫ﻣﺎ‬
function, how to return data from function.

‫ﺕ‬
‫ﺑﻜ‬
- Chapter 3, Pointers: The purpose of this chapter is to provide an

‫ﻔﺮ‬
‫ﺍﻟ‬
introduction to pointers and their use to the beginners.

‫ﺸﻴ‬
-‫ﺦ‬
- Chapter 4, Arrays and Linked Lists: provides a detailed

76

‫ﻤﻌ‬
explanation of arrays and Linked Lists concepts that includes the
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
main difference between them, the advantages and disadvantaged of

‫ﻌﺎﻟ‬
18

‫ﻰ‬
each of them and the operations that can be performed on them are
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
also explained.

‫ﺭﺓ‬
25

‫ﻭ‬
20

- Chapter 5, Stacks: focuses on the concept of last-in, first-out

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
(LIFO) data structure called stacks. The chapter also shows the

‫ﺟﻴ‬
‫ﺎﺍ‬
practical implementation of these data structures using arrays as
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

well as linked lists. It also shows how stacks can be used for the
‫ﻣﺎ‬
‫ﺕ‬

evaluation of arithmetic expressions.


‫ﺑﻜ‬
‫ﻔﺮ‬

- Chapter 6, Queues: deals with the concept of first-in, first-out


‫ﺍﻟ‬
‫ﺸﻴ‬

(FIFO) data structure called queues. The chapter also provides the
-‫ﺦ‬

real-world applications of queues.


76
42

- Chapter 7, Trees focuses on binary trees, and the operations that


-2

can be performed on the tree are also explained.


18
1-

- Chapter 8, Algorithm Analysis: In this chapter, we will discuss


-1
25

the different techniques of searching and sorting arrays of numbers


20

or characters.

I’m glad that this book is finally finished and hopes that my students will
find it useful for learning data structure fundamentals.

3
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Chapter 1 : Introduction

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
1.1 Introduction 5

‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
1.2 Algorithms as opposed to programs 6

‫ﻮ‬
‫ﺟﻴﺎ‬
1.3 Fundamental questions about algorithms 7

‫ﺍﻟ‬
1.4 Data structures, abstract data types 8

‫ﻤﻌ‬
1.5 Data Types 9

‫ﻠﻮ‬
‫ﻣﺎ‬
1.6 Abstraction 9

‫ﺕ‬
1.7 Abstract Data Types (ADTs) 10

‫ﺑﻜ‬
‫ﻔﺮ‬
1.8 Data Structures 11

‫ﺍﻟ‬
1.9 Classification of data structures 12

‫ﺸﻴ‬
1.10 Operations on Data Structures 13

-‫ﺦ‬
Chapter 2 : Functions
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
2.1 Modular Programming 16
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
2.2 Defining and Calling Functions 17
18

‫ﻰ‬
2.3 Function Prototypes 25
1-

‫ﻟﻺ‬
2.4 Sending Data into a Function 27
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

2.5 The Return Statement 35

‫ﻭ‬
20

2.6 Returning a Value from a Function 36

‫ﺗﻜ‬
‫ﻨﻮ‬
Chapter 3 : Pointers

‫ﻟﻮ‬
‫ﺟﻴ‬
3.1 Pointer vs. Variables
‫ﻟ‬ ‫ﺎﺍ‬ 47
‫ﻤﻌ‬

3.2 Pointer Initialization 48


‫ﻠﻮ‬
‫ﻣﺎ‬

3.3 Pointer Dereferencing 49


‫ﺕ‬

3.4 Pointers & Arrays (Pointer Arithmetic) 50


‫ﺑﻜ‬
‫ﻔﺮ‬

3.5 Pointers & Functions 52


‫ﺍﻟ‬

3.6 Pointers & User-defined Types 53


‫ﺸﻴ‬

Chapter 4 : Array and Linked List


-‫ﺦ‬
76

4.1 Basic concept of Arrays 56


42

4.2 What is an array? 58


-2

4.3 Single-Dimensional Arrays 59


8
1

4.3.1 Implementation 61
1-

4.3.2 Array Operations 64


-1
25

[Link] The operation of Traversing 64


20

[Link] The operation of Searching 65


[Link] The operation of Insertion 68
[Link] The operation of Deletion 69
4.4 Multidimensional Arrays 71
4.4.1 Implementation 72
4.5 Initializing Arrays 75
4
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
4.6 Linked Lists 77

‫ﺩﺍ‬
‫ﺭﺓ‬
4.6.1 The composition of a linked list 79

‫ﻭ‬
4.6.2 Linked List Operations 80

‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
[Link] Appending a Node to the List 81

‫ﻮ‬
‫ﺟﻴﺎ‬
[Link] Traversing a linked list 86

‫ﺍﻟ‬
[Link] Inserting a Node 88

‫ﻤﻌ‬
[Link] Deleting a Node 93

‫ﻠﻮ‬
‫ﻣﺎ‬
Chapter 5: Stack

‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
5.1 Introduction 100

‫ﺍﻟ‬
5.2 Definition 100

‫ﺸﻴ‬
5.3 Logical Level 101

-‫ﺦ‬
5.4 Stack Data Structure 102
76

‫ﻤﻌ‬
5.5 Implementing a stack 103
42

‫ﻬﺪ‬
5.6 Applications of stacks 105
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
5.7 Static and Dynamic Stacks 105
18

‫ﻰ‬
5.8 Operations on Stacks 106
1-

‫ﻟﻺ‬
5.9 Summary of Stack Concepts: 110
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Chapter 6: Queue

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
6.1 Introduction 118

‫ﻟﻮ‬
‫ﺟﻴ‬
6.2 The queue Data Structure 118
6.3 Implementing a Queue
‫ﻟ‬‫ﺎﺍ‬ 119
‫ﻤﻌ‬

6.4 Queue Logical Level 123


‫ﻠﻮ‬
‫ﻣﺎ‬

6.5 Application of Queues 125


‫ﺕ‬

6.6 Static and Dynamic Queues 126


‫ﺑﻜ‬
‫ﻔﺮ‬

6.7 Operations on Queues 126


‫ﺍﻟ‬

6.8 Dynamic Queues 137


‫ﺸﻴ‬

6.9 Summary of queue concepts 140


-‫ﺦ‬

Chapter 7: Trees and Graphs


76
42

7.1 Introduction 144


-2

7.2 Tree Operations 145


8
1

Chapter 8: Algorithm Analysis


1-
-1
25

8.1 Introduction 155


20

8.2 The Algorithm Concepts 155


8.3 Introduction to Search Algorithms 157
8.4 The linear Search 157
8.5 Efficiency of the Linear Search 160
8.6 The Binary Search 161
8.7 Efficiency of the Binary Search 167
5
‫‪8.9 Bubble Sort‬‬

‫‪20‬‬ ‫‪20‬‬
‫‪25‬‬ ‫‪25‬‬
‫‪8.10 The selection sort‬‬

‫‪-1‬‬ ‫‪-1‬‬
‫‪8.8 Sorting Algorithms‬‬

‫‪1-‬‬ ‫‪1-‬‬
‫‪1‬‬‫‪8‬‬ ‫‪18‬‬
‫‪-2‬‬ ‫‪-2‬‬

‫‪6‬‬
‫‪42‬‬ ‫‪42‬‬
‫‪76‬‬ ‫‪76‬‬
‫ﺦ‪-‬‬ ‫ﺦ‪-‬‬
‫ﺸﻴ‬ ‫ﺸﻴ‬
‫ﺍﻟ‬ ‫ﺍﻟ‬
‫ﻔﺮ‬ ‫ﻔﺮ‬
‫ﺑﻜ‬ ‫ﺑﻜ‬
‫ﺕ‬ ‫ﺕ‬
‫ﻣﺎ‬ ‫ﻣﺎ‬
‫ﻠﻮ‬ ‫ﻠﻮ‬
‫ﻤﻌ‬ ‫ﻤﻌ‬
‫ﻟ‬‫ﺎﺍ‬ ‫ﺍﻟ‬
‫ﺟﻴ‬ ‫ﺟﻴﺎ‬
‫ﻮ‬
‫‪174‬‬
‫‪168‬‬
‫‪168‬‬

‫ﻟﻮ‬ ‫ﻮﻟ‬
‫ﻨﻮ‬ ‫ﻨ‬
‫ﺗﻜ‬ ‫ﺗﻜ‬
‫ﻭ‬ ‫ﻭ‬
‫ﺭﺓ‬ ‫ﺭﺓ‬
‫ﺩﺍ‬ ‫ﺩﺍ‬
‫ﻟﻺ‬ ‫ﻟﻺ‬
‫ﻰ‬ ‫ﻰ‬
‫ﻌﺎﻟ‬ ‫ﻌﺎﻟ‬
‫ﺍﻟ‬ ‫ﺍﻟ‬
‫ﻬﺪ‬ ‫ﻬﺪ‬
‫ﻤﻌ‬ ‫ﻤﻌ‬
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 1 Introduction

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Chapter 1

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
Introduction

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
1.1 Introduction

‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
The foundation of computer science is based on the study of algorithms.

‫ﺸﻴ‬
-‫ﺦ‬
An algorithm is a sequence of clear and precise step-by-step instructions

76

‫ﻤﻌ‬
for solving a problem in a finite amount of time. Algorithms are
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
implemented by translating the step-by-step instructions into a computer

‫ﻌﺎﻟ‬
18

‫ﻰ‬
program that can be executed by a computer. This translation process is
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
called computer programming or simply programming. Computer

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
programs are constructed using a programming language appropriate to

‫ﻨﻮ‬
‫ﻟﻮ‬
the problem. While programming is an important part of computer

‫ﺟﻴ‬
‫ﺎﺍ‬
science, computer science is not the study of programming. Nor is it about
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

learning a particular programming language. Instead, programming and


‫ﻣﺎ‬
‫ﺕ‬

programming languages are tools used by computer scientists to solve


‫ﺑﻜ‬
‫ﻔﺮ‬

problems.
‫ﺍﻟ‬
‫ﺸﻴ‬

This course covers the key ideas involved in designing algorithms. We


-‫ﺦ‬

shall see how they depend on the design of suitable data structures, and
76
42

how some structures and algorithms are more efficient than others for the
-2

same task. We will concentrate on a few basic tasks, such as storing,


18
1-

sorting and searching data, that underlie much of computer science, but the
-1
25

techniques discussed will be applicable much more generally. We will not


20

restrict ourselves to implementing the various data structures and


algorithms in particular computer programming languages (e.g., Java, C,
Python), but specify them in simple pseudocode that can easily be
implemented in any appropriate language.
7
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 1 Introduction

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
1.2 Algorithms as opposed to programs

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
An algorithm for a particular task can be defined as "a finite sequence

‫ﻮ‬
‫ﺟﻴﺎ‬
of instructions, each of which has a clear meaning and can be performed

‫ﺍﻟ‬
‫ﻤﻌ‬
with a finite amount of effort in a finite length of time". As such, an

‫ﻠﻮ‬
‫ﻣﺎ‬
algorithm must be precise enough to be understood by human beings.

‫ﺕ‬
‫ﺑﻜ‬
However, in order to be executed by a computer, we will generally need a

‫ﻔﺮ‬
‫ﺍﻟ‬
program that is written in a rigorous formal language; and since

‫ﺸﻴ‬
-‫ﺦ‬
computers are quite inflexible compared to the human mind; programs

76

‫ﻤﻌ‬
usually need to contain more details than algorithms. Here we shall ignore
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
most of those programming details and concentrate on the design of

‫ﻌﺎﻟ‬
18

‫ﻰ‬
algorithms rather than programs.
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
The task of implementing the discussed algorithms as computer

‫ﺭﺓ‬
25

‫ﻭ‬
20

programs is important, of course, but this course will concentrate on the

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
theoretical aspects and leave the practical programming aspects to be

‫ﺟﻴ‬
‫ﺎﺍ‬
studied elsewhere. Having said that, we will often find it useful to write
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

down segments of actual programs in order to clarify and test certain


‫ﻣﺎ‬
‫ﺕ‬

theoretical aspects of algorithms and their data structures. It is also worth


‫ﺑﻜ‬
‫ﻔﺮ‬

bearing in mind the distinction between different programming paradigms:


‫ﺍﻟ‬
‫ﺸﻴ‬

Imperative Programming describes computation in terms of instructions


-‫ﺦ‬

that change the program/data state, whereas Declarative Programming


76
42

specifies what the program should accomplish without describing how to


-2

do it. This course will primarily be concerned with developing algorithms


18
1-

that map easily onto the imperative programming approach.


-1
25

Algorithms can obviously be described in plain English, and we will


20

sometimes do that. However, for computer scientists it is usually easier


and clearer to use something that comes somewhere in between formatted
English and computer program code, but is not runnable because certain

8
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 1 Introduction

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
details are omitted. This is called pseudocode, which comes in a variety of

‫ﺩﺍ‬
‫ﺭﺓ‬
forms.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
1.3 Fundamental questions about algorithms

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
Given an algorithm to solve a particular problem, we are naturally led to

‫ﻣﺎ‬
‫ﺕ‬
ask:

‫ﺑﻜ‬
‫ﻔﺮ‬
1. What is it supposed to do?

‫ﺍﻟ‬
‫ﺸﻴ‬
2. Does it really do what it is supposed to do?

-‫ﺦ‬
3. How efficiently does it do it?
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
The technical terms normally used for these three aspects are:
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
1. Specification.
18

‫ﻰ‬
1-

‫ﻟﻺ‬
2. Verification.
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

3. Performance analysis.

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
The specification should formalize the crucial details of the problem
‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬
that the algorithm is intended to solve. Sometimes that will be based on a
‫ﻠﻮ‬
‫ﻣﺎ‬

particular representation of the associated data, and sometimes it will be


‫ﺕ‬
‫ﺑﻜ‬

presented more abstractly. Typically, it will have to specify how the inputs
‫ﻔﺮ‬
‫ﺍﻟ‬

and outputs of the algorithm are related, though there is no general


‫ﺸﻴ‬
-‫ﺦ‬

requirement that the specification is complete or non-ambiguous. For


76

simple problems, it is often easy to see that a particular algorithm will


42
-2

always work, i.e. that it satisfies its specification. However, for more
8

complicated specifications and/or algorithms, the fact that an algorithm


1
1-
-1

satisfies its specification may not be obvious at all. In this case, we need to
25
20

spend some effort verifying whether the algorithm is indeed correct. In


general, testing on a few particular inputs can be enough to show that the
algorithm is incorrect. However, since the number of different potential
inputs for most algorithms is infinite in theory, and huge in practice, more

9
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 1 Introduction

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
than just testing on particular cases is needed to be sure that the algorithm

‫ﺩﺍ‬
‫ﺭﺓ‬
satisfies its specification. Formal verification techniques are complex and

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
will normally be left till after the basic ideas of this course have been

‫ﻮ‬
‫ﺟﻴﺎ‬
studied. Finally, the efficiency or performance of an algorithm relates to

‫ﺍﻟ‬
‫ﻤﻌ‬
the resources required by it, such as how quickly it will run, or how much

‫ﻠﻮ‬
‫ﻣﺎ‬
computer memory it will use. This will usually depend on the problem

‫ﺕ‬
‫ﺑﻜ‬
instance size, the choice of data representation, and the details of the

‫ﻔﺮ‬
‫ﺍﻟ‬
algorithm. Indeed, this is what normally drives the development of new

‫ﺸﻴ‬
-‫ﺦ‬
data structures and algorithms.

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
1.4 Data structures, abstract data types

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
For many problems, the ability to formulate an efficient algorithm
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

depends on being able to organize the data in an appropriate manner. The

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
term data structure is used to denote a particular way of organizing data

‫ﻟﻮ‬
‫ﺟﻴ‬
for particular types of operation. The course on data structures has been a
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
mainstay of most computer science departments. However, over the last
‫ﻠﻮ‬
‫ﻣﺎ‬

13 years the focus of this course has broadened considerably. The topic of
‫ﺕ‬
‫ﺑﻜ‬

data structures now has been subsumed under the broader topic of abstract
‫ﻔﺮ‬
‫ﺍﻟ‬

data types (ADTs).


‫ﺸﻴ‬
-‫ﺦ‬

Often we want to talk about data structures without having to worry


76

about all the implementation details associated with particular


42
-2

programming languages, or how the data is stored in computer memory.


8

We can do this by formulating abstract mathematical models of particular


1
1-
-1

classes of data structures or data types which have common features.


25
20

These are called abstract data types, and are defined only by the
operations that may be performed on them. Typically, we specify how
they are built out of more primitive data types (e.g., integers or strings),
how to extract that data from them, and some basic checks to control the

10
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 1 Introduction

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
flow of processing in algorithms. The idea that the implementational

‫ﺩﺍ‬
‫ﺭﺓ‬
details are hidden from the user and protected from outside access is

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
known as encapsulation. We shall see many examples of abstract data

‫ﻮ‬
‫ﺟﻴﺎ‬
types throughout these notes.

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
1.5 Data Types

‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
Programming languages commonly provide data types as part of the

‫ﺍﻟ‬
‫ﺸﻴ‬
language itself. These data types, known as primitives, come in two

-‫ﺦ‬
categories: simple and complex. The simple data types consist of values
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
that are in the most basic form and cannot be decomposed into smaller
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
parts. Integer and real types, for example, consist of single numeric values.
18

‫ﻰ‬
1-

‫ﻟﻺ‬
The complex data types, on the other hand, are constructed of multiple
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

components consisting of simple types or other complex types. The

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
primitive types provided by a language may not be sufficient for solving

‫ﻟﻮ‬
‫ﺟﻴ‬
large complex problems. Thus, most languages allow for the construction
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
of additional data types, known as user-defined types since they are
‫ﻠﻮ‬
‫ﻣﺎ‬

defined by the programmer and not the language. Some of these data types
‫ﺕ‬
‫ﺑﻜ‬

can themselves be very complex.


‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

1.6 Abstraction
76
42

To help manage complex problems and complex data types, computer


-2

scientists typically work with abstractions. An abstraction is a mechanism


1 8
1-

for separating the properties of an object and restricting the focus to those
-1
25

relevant in the current context. The user of the abstraction does not have to
20

understand all of the details in order to utilize the object, but only those
relevant to the current task or problem.

11
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 1 Introduction

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
Abstraction: A model of a complex system that includes only the details

‫ﻭ‬
‫ﺗﻜ‬
essential to the perspective of the viewer of the system

‫ﻨ‬
‫ﻮ‬‫ﻮﻟ‬
‫ﺟﻴﺎ‬
1.7 Abstract Data Types (ADTs)

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
An abstract data type (or ADT) is a programmer-defined data type that

‫ﺕ‬
‫ﺑﻜ‬
specifies a set of data values and a collection of well-defined operations

‫ﻔﺮ‬
‫ﺍﻟ‬
that can be performed on those values. Abstract data types are defined

‫ﺸﻴ‬
-‫ﺦ‬
independent of their implementation, allowing us to focus on the use of

76

‫ﻤﻌ‬
the new data type instead of how it's implemented. This separation is
42

‫ﻬﺪ‬
-2

typically enforced by requiring interaction with the abstract data type

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
through an interface or defined set of operations. This is known as
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
information hiding. By hiding the implementation details and requiring

‫ﺭﺓ‬
25

‫ﻭ‬
ADTs to be accessed through an interface, we can work with an
20

‫ﺗﻜ‬
‫ﻨﻮ‬
abstraction and focus on what functionality the ADT provides instead of

‫ﻟﻮ‬
‫ﺟﻴ‬
how that functionality is implemented. ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬

Abstract data types: a data type whose properties (domain and


‫ﺕ‬
‫ﺑﻜ‬

operations) are specified independently any particular implementation


‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

One important feature of any design method is that the details that are
76
42

specified in lower levels of the program design are hidden from the higher
-2

levels. The programmer sees only the details that are relevant at a
8
1

particular level of the design. The information hiding makes certain details
1-
-1

inaccessible to the programmer at higher level.


25
20

Information Hiding: The practice of hiding the details of a function on


data structure with the goal of controlling access to the details of a
module or structure

12
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 1 Introduction

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
1.8 Data Structures

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
A single integer can be very useful if we need a counter, a sum, or an

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮ‬‫ﻮﻟ‬
index in a program, but generally we must also deal with data that have

‫ﺟﻴﺎ‬
lots of parts, such as a list. We describe the logical properties of such a

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
collection of data as an abstract data type; we call the concrete

‫ﻣﺎ‬
‫ﺕ‬
implementation of the data a data structure. When a program's information

‫ﺑﻜ‬
‫ﻔﺮ‬
is made up of component parts, we must consider an appropriate data

‫ﺍﻟ‬
‫ﺸﻴ‬
structure. Data structures have a few features worth noting. First, they can

-‫ﺦ‬
be "decomposed" into their component elements. Second, the arrangement
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
of the elements is a feature of the structure that affects how each element
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
is accessed. Third, both the arrangement of the elements and the way they
18

‫ﻰ‬
1-

‫ﻟﻺ‬
are accessed can be encapsulated.
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

Data structure: A collection of data elements whose organization is

‫ﺗﻜ‬
‫ﻨﻮ‬
characterized by accessing operations that are used to store and

‫ﻟﻮ‬
‫ﺟﻴ‬
retrieve the individual data elements; the implementation of the
‫ﺎﺍ‬
‫ﻟ‬
composite data members in an abstract data type.
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

Notice the difference between an abstract data type and a data


‫ﺑﻜ‬
‫ﻔﺮ‬

structure. The abstract data type is a high-level description: the logical


‫ﺍﻟ‬
‫ﺸﻴ‬

picture of the data and the operations that manipulate them. The data
-‫ﺦ‬
76

structure is concrete: a collection of data elements and the operations


42

that store and retrieve individual elements. An abstract data type is


-2

implementation independent, whereas a data structure is


8
1
1-

implementation dependent. A data structure is how we implement the


-1
25

data in an abstract data type whose values have component parts. The
20

operations on an abstract data type are translated into algorithms on the


data structure.

1.9 Classification of data structures

13
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 1 Introduction

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
The classification of data structure mainly consists of: Primitive data

‫ﺩﺍ‬
‫ﺭﺓ‬
structure and Non-primitive data structure as in figure 1.1. Primitive data

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
structures (Built-in data structure) are known as basic data structures.

‫ﻮ‬
‫ﺟﻴﺎ‬
These data structures are directly operated upon by the machine

‫ﺍﻟ‬
‫ﻤﻌ‬
instructions. Normally, primitive data structures have different

‫ﻠﻮ‬
‫ﻣﺎ‬
representation on different computers. Non-Primitive data structures

‫ﺕ‬
‫ﺑﻜ‬
(User defined - data structure) are highly developed complex data

‫ﻔﺮ‬
‫ﺍﻟ‬
structures. Basically, these are developed from the primitive data structure.

‫ﺸﻴ‬
-‫ﺦ‬
The non-primitive data structure is responsible for organizing the group of

76

‫ﻤﻌ‬
homogeneous and heterogeneous data elements, such as array, list, and
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
files.

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
1.10 Operations on Data Structures
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

Design of efficient data structure must take operations to be performed on

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
the data structures into account. The most commonly used operations on data

‫ﺟﻴ‬
structure are broadly categorized into following types: ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

1. Create: The create operation results in reserving memory for


‫ﻣﺎ‬
‫ﺕ‬

program elements. This can be done by declaration statement.


‫ﺑﻜ‬
‫ﻔﺮ‬

Creation of data structure may take place either during compile-


‫ﺍﻟ‬
‫ﺸﻴ‬

time or run-time.
-‫ﺦ‬
76

2. Destroy: Destroy operation destroys memory space allocated for


42

specified data structure.


-2
18
1-
-1
25
20

14
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 1 Introduction

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
NON

‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
NON

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
3. Selection: Selection operation deals with accessing a particular

‫ﻟﻮ‬
‫ﺟﻴ‬
data within a data structure.
‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬
4. Updation: It updates or modifies the data in the data structure.
‫ﻠﻮ‬
‫ﻣﺎ‬

5. Searching: It finds the presence of desired data item in the list of


‫ﺕ‬
‫ﺑﻜ‬

data items; it may also find the locations of all elements that
‫ﻔﺮ‬
‫ﺍﻟ‬

satisfy certain conditions.


‫ﺸﻴ‬
-‫ﺦ‬

6. Sorting: Sorting is a process of arranging all data items in a data


76

structure in a particular order, say for example, either in


42
-2

ascending order or in descending order.


81

7. Merging: Merging is a process of combining the data items of


1-
-1

two different sorted lists into a single sorted list.


25
20

8. Splitting: Splitting is a process of partitioning single list


to multiple list.
9. Traversal: Traversal is a process of visiting each and every node
of a list in systematic manner.

15
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 1 Introduction

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
[Q1] Review Question

‫ﺩﺍ‬
‫ﺭﺓ‬
1- State the scientific name for each one:

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
a) A step- by- step description of the solution to a problem.

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
(-------------------------)

‫ﻤﻌ‬
‫ﻠﻮ‬
b) A list of instructions provide to computer to achieve a

‫ﻣﺎ‬
‫ﺕ‬
specific task.

‫ﺑﻜ‬
‫ﻔﺮ‬
(------------------------)

‫ﺍﻟ‬
‫ﺸﻴ‬
c) A model of a complex system that includes only the essential

-‫ﺦ‬
76
details

‫ﻤﻌ‬
42

‫ﻬﺪ‬
(------------------------)
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
d) A collection of data elements whose organization is
18

‫ﻰ‬
1-

‫ﻟﻺ‬
characterized by accessing operations that are used to store
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

and retrieve the individual data elements.

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
(------------------------)

‫ﻟﻮ‬
‫ﺟﻴ‬
e) a data type whose properties (domain and operations) are
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

specified independently any particular implementation


‫ﻠﻮ‬
‫ﻣﺎ‬

(------------------------)
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬

2- Identify true or false and rewrite the correct version of false one :
‫ﺸﻴ‬
-‫ﺦ‬
76

a) Primitive data structures is User defined - data structure.


42

( )
-2

b) An abstract data type is implementation dependent.


81
1-

( )
-1
25

c) Non-Primitive data structures such as array, list, and files.


20

( )
d) A data structure is how we implement the data in an
abstract data type.
( )
16
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 1 Introduction

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
3- Define the following terms:

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
o Algorithm

‫ﻨ‬
‫ﻮ‬ ‫ﻮﻟ‬
o Abstraction

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
o Abstract Data Types (ADTs)

‫ﻤﻌ‬
‫ﻠﻮ‬
o Information Hiding

‫ﻣﺎ‬
‫ﺕ‬
o Data Structure

‫ﺑﻜ‬
‫ﻔﺮ‬
o Primitive data structures

‫ﺍﻟ‬
‫ﺸﻴ‬
o Non-Primitive data structures

-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
4- What are the most commonly operations used on data structure?
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42
-2
8
1
1-
-1
25
20

17
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Chapter 2

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
Functions

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
2.1 Modular Programming

‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
A program may be broken up into a set of manageable functions, or

‫ﻔﺮ‬
‫ﺍﻟ‬
modules. This is called modular programming. A function is a collection

‫ﺸﻴ‬
-‫ﺦ‬
of statements that performs a specific task. So far you have used functions

76

‫ﻤﻌ‬
in two ways: 1) you have created a function called main in every program
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
you've written, and 2) you have called library functions such as pow and

‫ﻌﺎﻟ‬
18

‫ﻰ‬
sqrt. In this chapter you will learn how to create your own functions that
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
can be used like library functions. Functions are commonly used to break

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
a problem down into small manageable pieces, or modules. Instead of

‫ﻨﻮ‬
‫ﻟﻮ‬
writing one long function that contains all the statements necessary to

‫ﺟﻴ‬
‫ﺎﺍ‬
solve a problem, several smaller functions can be written, with each one
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

solving a specific part of the problem. These small functions can then be
‫ﻣﺎ‬
‫ﺕ‬

executed in the desired order to solve the problem. This approach is


‫ﺑﻜ‬
‫ﻔﺮ‬

sometimes called divide and conquer because a large problem is divided


‫ﺍﻟ‬
‫ﺸﻴ‬

into several smaller problems that are more easily solved. Figure 2-1
-‫ﺦ‬

illustrates this idea by comparing two programs, one that uses a single
76
42

module containing all of the statements necessary to solve a problem, and


-2

another that divides a problem into a set of smaller problems, each


8
1
1-

handled by a separate function.


-1
25

Another reason to write functions is that they simplify programs. If a


20

specific task is performed in several places in a program, a function can be


written once to perform that task, and then be executed anytime it is
needed. This benefit of using functions is known as code reuse because

18
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
you are writing the code to perform a task once and then reusing it each

‫ﺩﺍ‬
‫ﺭﺓ‬
time you need to perform the task.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮ‬ ‫ﻮﻟ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Figure 2.1

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
2.2 Defining and Calling Functions

‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
A function call is a statement that causes a function to execute. A
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬

function definition contains the statements that make up the function.


‫ﺕ‬

When creating a function, you must write its definition. All function
‫ﺑﻜ‬
‫ﻔﺮ‬

definitions have the following parts:


‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

Name Every function must have a name. In general, the same


76

rules that apply to variable names also apply to function


42

names.
-2
18

Parameter The program module that calls a function can send data to
1-

list it. The parameter list is the list of variables that hold the
-1
25

values being passed to the function. If no values are being


20

passed to the function, its parameter list is empty.

Body The body of a function is the set of statements that carry out
the task the function is performing. These statements are
enclosed in a set of braces.

19
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Return type A function can send a value back to the program module

‫ﺩﺍ‬
‫ﺭﺓ‬
that called it. The return type is the data type of the value

‫ﻭ‬
being sent back.

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
Figure 2-2 shows the definition of a simple function with the various

‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
parts labeled. Notice that the function's return type is actually listed first.

‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Figure 2.2

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
Void Functions ‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬

You already know that a function can return a value. The main function
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

in all of the programs you have seen in this book is declared to return an
‫ﺑﻜ‬
‫ﻔﺮ‬

int value to the operating system. The return 0; statement causes the value
‫ﺍﻟ‬
‫ﺸﻴ‬

0 to be returned when the main function finishes executing.


-‫ﺦ‬

It isn't necessary for all functions to return a value, however. Some


76
42

functions simply perform one or more statements and then return. In C++
-2

these are called void functions. The displayMessage function shown here
18

is an example:
1-
-1

void displayMessage()
25
20

{
cout << ''Hello from the function displayMessage. \n'';
}

20
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
The function's name is displayMessage. This name is descriptive, as

‫ﺩﺍ‬
‫ﺭﺓ‬
function names should be. It gives an indication of what the function does.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
It displays a message. Notice the function's return type is void. This means

‫ﻮ‬
‫ﺟﻴﺎ‬
the function does not send back a value when it has finished executing and

‫ﺍﻟ‬
‫ﻤﻌ‬
returns to the part of the program that invoked it. Because no value is

‫ﻠﻮ‬
‫ﻣﺎ‬
being sent back, no return statement is required. When the statements in

‫ﺕ‬
‫ﺑﻜ‬
the function have finished executing and the right braces that ends the

‫ﻔﺮ‬
‫ﺍﻟ‬
function is encountered, the program automatically returns.

‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
Calling a Function
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
A function is executed when it is called. Function main is called

‫ﻌﺎﻟ‬
18

‫ﻰ‬
automatically when a program starts, but all other functions must be
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
executed by function call statements. When a function is called, the

‫ﺭﺓ‬
25

‫ﻭ‬
20

program branches to that function and executes the statements in its body.

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
Let's look at Program 2-1, which contains two functions: main and

‫ﺟﻴ‬
displayMessage.
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

Program 2-1
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬

1 // This program has two functions: main and displayMessage.


‫ﺸﻴ‬
-‫ﺦ‬

2 #include <iostream>
3 using namespace std;
76

4
42

5 /***************************************
-2

6*
8
1

*
1-

displayMessage *
-1
25

7 This function displays a greeting. *


20

8 ***************************************/
9 void displayMessage()
10 {
11 cout << ''Hello from the function displayMessage. \n '';
12 }
13

21
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
14 /***************************************

‫ﺩﺍ‬
‫ﺭﺓ‬
15 * • main *

‫ﻭ‬
16 ***************************************/

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
17 int main()

‫ﻮ‬
‫ﺟﻴﺎ‬
18 {
19 cout << ''Hello from main. \n '';

‫ﺍﻟ‬
‫ﻤﻌ‬
20 displayMessage(); //Call displayMessage

‫ﻠﻮ‬
‫ﻣﺎ‬
21 cout << ''Back in function main again. \n '';

‫ﺕ‬
22 return 0;

‫ﺑﻜ‬
23 }

‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
Program Output

-‫ﺦ‬
Hello from main.
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
Hello from the function displayMessage.
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
Back in function main again.
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

The function displayMessage is called by the following statement in

‫ﻭ‬
20

‫ﺗﻜ‬
line 20:

‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
displayMessage();
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬

This statement is the function call. It is simply the name of the function
‫ﻔﺮ‬

followed by a set of parentheses and a semicolon. Let's compare this with


‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

the function header:


76
42

Function Header è void displayMessage()


-2
1 8

Function Call è displayMessage()


1-
-1
25

The function header is part of the function definition. It declares the


20

function's return type, name, and parameter list. It must not be terminated
with a semicolon because the definition of the function's body follows it.

22
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
The function call is a statement that executes the function, so it is

‫ﺩﺍ‬
‫ﺭﺓ‬
terminated with a semicolon like all other C++ statements. Notice that the

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
function call does not list the return type and, if the program is not passing

‫ﻮ‬
‫ﺟﻴﺎ‬
data into the function, the parentheses are left empty.

‫ﺍﻟ‬
‫ﻤﻌ‬
Even though the program starts executing at main, the function

‫ﻠﻮ‬
‫ﻣﺎ‬
displayMessage is defined first. This is because before a function can be

‫ﺕ‬
‫ﺑﻜ‬
called, the compiler must know certain things about it. It must know the

‫ﻔﺮ‬
‫ﺍﻟ‬
function's name, its return type, how many parameter variables it has, and

‫ﺸﻴ‬
-‫ﺦ‬
what the data type of each of these variables is. One way to ensure the

76

‫ﻤﻌ‬
compiler will know this information is to place the function definition
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
before all calls to that function. NOTE: You should always document your

‫ﻌﺎﻟ‬
18

‫ﻰ‬
functions by writing comments that describe what they do. These
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
comments should appear just before the function definition.

‫ﺭﺓ‬
25

‫ﻭ‬
20

Notice how Program 2-1 flows. It starts, of course, in function main.

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
When the call to displayMessage is encountered, the program branches to

‫ﺟﻴ‬
‫ﺎﺍ‬
that function and performs its statements. Once displayMessage has
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

finished executing, the program branches back to function main and


‫ﻣﺎ‬
‫ﺕ‬

resumes with the line that follows the function call. This is illustrated in
‫ﺑﻜ‬
‫ﻔﺮ‬

Figure 2-3.
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42
-2
18
1-
-1
25
20

Figure 2.3

23
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Program 2-2

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
1 // The function displayMessage is repeatedly called from within a

‫ﻮ‬
‫ﺟﻴﺎ‬
loop.
2 #include <iostream>

‫ﺍﻟ‬
‫ﻤﻌ‬
3 using namespace std;

‫ﻠﻮ‬
‫ﻣﺎ‬
4

‫ﺕ‬
5 /***************************************

‫ﺑﻜ‬
6 * displayMessage *

‫ﻔﺮ‬
7 * This function displays a greeting. *

‫ﺍﻟ‬
‫ﺸﻴ‬
8 ***************************************/

-‫ﺦ‬
9 void displayMessage()

76
10 {

‫ﻤﻌ‬
42

‫ﻬﺪ‬
11 cout << ''Hello from the function displayMessage. \n '';
-2

‫ﺍﻟ‬
12 }

‫ﻌﺎﻟ‬
18

13

‫ﻰ‬
1-

14 /***************************************

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
15 * main *

‫ﺭﺓ‬
25

16 ***************************************/

‫ﻭ‬
20

‫ﺗﻜ‬
17 int main()

‫ﻨﻮ‬
‫ﻟﻮ‬
18 {

‫ﺟﻴ‬
19 cout << ''Hello from main. \n''; ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
20
‫ﻠﻮ‬

21 for (int count = O; count < 3; count++)


‫ﻣﺎ‬

22 displayMessage(); //Call displayMessage


‫ﺕ‬
‫ﺑﻜ‬

23
‫ﻔﺮ‬

2 4 cout << ''Back in function main again. \n '';


‫ﺍﻟ‬

25 return 0;
‫ﺸﻴ‬
-‫ﺦ‬

26 }
76
42
-2

Program Output
1 8
1-

Hello from main.


-1
25

Hello from the function displayMessage.


20

Hello from the function displayMessage.


Hello from the function displayMessage.
Back in function main again.

24
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
It is possible to have many functions and function calls in a program.

‫ﺩﺍ‬
‫ﺭﺓ‬
Program 2-3 has three functions: main, first, and second.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
Program 2-3

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
1 // This program has three functions:main, first, and second

‫ﻣﺎ‬
2 #include <iostream>

‫ﺕ‬
3 using namespace std;

‫ﺑﻜ‬
‫ﻔﺮ‬
4

‫ﺍﻟ‬
5 /***************************************

‫ﺸﻴ‬
6 * first *

-‫ﺦ‬
7* This function displays a message. *
76

‫ﻤﻌ‬
8 ***************************************/
42

‫ﻬﺪ‬
9 void first ()
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
10 {
18

‫ﻰ‬
11 cout << ''I am now inside the function first. \n '';
1-

‫ﻟﻺ‬
12 }
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

13

‫ﻭ‬
20

14 /***************************************

‫ﺗﻜ‬
‫ﻨﻮ‬
15* second *

‫ﻟﻮ‬
‫ﺟﻴ‬
16* This function displays a message *
17 ***************************************/ ‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬

18 void second ( )
‫ﻠﻮ‬
‫ﻣﺎ‬

19 {
‫ﺕ‬

2 0 cout << ''I am now inside the function second. \n '';


‫ﺑﻜ‬

21 }
‫ﻔﺮ‬

22
‫ﺍﻟ‬
‫ﺸﻴ‬

23 /***************************************
-‫ﺦ‬

24 * main *
76

25 ***************************************I
42

26 int main ()
-2

27 {
8

28 cout <<''I am starting in function main. \n '' ;


1
1-

29 first ( ) ; // Call function first


-1

30 second (); // Call function second


25

31 cout << ''Back in function main again. \n '';


20

32 return 0;
33 }

25
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Program Output

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
I am starting in function main.

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
I am now inside the function first.

‫ﻮ‬
‫ﺟﻴﺎ‬
I am now inside the function second.
Back in function main again.

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
In lines 29 and 30 of Program 6-3, function main contains a call to first

‫ﺑﻜ‬
‫ﻔﺮ‬
and a call to second:

‫ﺍﻟ‬
‫ﺸﻴ‬
first ( ) ;

-‫ﺦ‬
second ( ) ;
Each call statement causes the program to branch to a function and then
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
back to mainwhen the function is finished. Figure 6-4 illustrates the paths
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
taken by the program.
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42
-2
18
1-
-1
25
20

Figure 2.4

26
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
2.3 Function Prototypes

‫ﺩﺍ‬
‫ﺭﺓ‬
A function prototype eliminates the need to place a function definition

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
before all calls to the function. Before the compiler encounters a call to a

‫ﻮ‬
‫ﺟﻴﺎ‬
particular function, it must already know certain things about the function.

‫ﺍﻟ‬
‫ﻤﻌ‬
In particular, it must know the number of parameters the function uses, the

‫ﻠﻮ‬
‫ﻣﺎ‬
type of each parameter, and the return type of the function. Parameters

‫ﺕ‬
‫ﺑﻜ‬
allow information to be sent to a function. Certain return types allow

‫ﻔﺮ‬
‫ﺍﻟ‬
information to be returned from a function. You will learn more about

‫ﺸﻴ‬
-‫ﺦ‬
parameters and return types in later sections of this chapter. For now, the

76

‫ﻤﻌ‬
functions we will use will have no parameters and, except for main, will
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
have a return type of void.

‫ﻌﺎﻟ‬
18

‫ﻰ‬
One way of ensuring that the compiler has this required information is
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
to place the function definition before all calls to that function. This was

‫ﺭﺓ‬
25

‫ﻭ‬
20

the approach taken in Programs 2-1 through 2-3. Another method is to

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
declare the function with a function prototype. Here is a prototype for the

‫ﺟﻴ‬
displayMessage function in Program 2-1:
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

void displayMessage();
‫ﻣﺎ‬
‫ﺕ‬

This prototype looks similar to the function header, except there is a


‫ﺑﻜ‬
‫ﻔﺮ‬

semicolon at the end. The statement tells the compiler that the function
‫ﺍﻟ‬
‫ﺸﻴ‬

displayMessage uses no parameters and has a void return type , meaning it


-‫ﺦ‬

doesn't return a value.


76
42
-2

NOTE: Function prototypes are also known as function declarations


1 8
1-
-1
25
20

WARNING! You must either place the function definition or the


function prototype ahead of all calls to the function. Otherwise the
program will not compile.

27
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Function prototypes are usually placed near the top of a program so the

‫ﺩﺍ‬
‫ﺭﺓ‬
compiler will ''see'' them before any function calls. Program 2-4 is a

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
modification of Program 2-3. The definitions of the functions first and

‫ﻮ‬
‫ﺟﻴﺎ‬
second have been placed after main, and their function prototypes have

‫ﺍﻟ‬
‫ﻤﻌ‬
been placed above main, directly after the using namespace std statement.

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
Program 2.4

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
1 // This program has three functions: main, first, and second .
2 // It uses function prototypes.
76

‫ﻤﻌ‬
42
3 #include <iostream>

‫ﻬﺪ‬
-2

4 using namespace std;

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
5
18

‫ﻰ‬
6 Function prototypes
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
7 void first();

‫ﺭﺓ‬
25

8 void second ( ) ;

‫ﻭ‬
20

‫ﺗﻜ‬
9

‫ﻨﻮ‬
10 int main()

‫ﻟﻮ‬
‫ﺟﻴ‬
11 {
12 cout <<''I am starting in function main. \n '';
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

13 first ( ) ; // Call function first


‫ﻠﻮ‬
‫ﻣﺎ‬

14 second (); // Call function second


‫ﺕ‬

15 cout << ''Back in function main again. \n'';


‫ﺑﻜ‬
‫ﻔﺮ‬

16 return 0;
‫ﺍﻟ‬

17 }
‫ﺸﻴ‬

18
-‫ﺦ‬

19 /***************************************
76

20 * first *
42

21 * This function displays a message. *


-2

22 ***************************************/
8

23 void first()
1
1-

24 {
-1

25 cout << ''I am now inside the function first. \n'';


25
20

26 }
27
28 / ***************************************
29 * second *
30 * This function displays a message. *
31 *************************************** /

28
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
32 void second()

‫ﺩﺍ‬
‫ﺭﺓ‬
33 {

‫ﻭ‬
34 cout << ''I am now inside the function second. \n '';

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
35 }

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
Program Output is the same as the output of Program 6-3.

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
When the compiler is reading Program 2-4, it encounters the calls to the

‫ﻔﺮ‬
‫ﺍﻟ‬
functions first and second in lines 13 and 14 before it has read the

‫ﺸﻴ‬
-‫ﺦ‬
definition of those functions. Because of the function prototypes, however,

76

‫ﻤﻌ‬
the compiler already knows the return type and parameter information of
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
first and second. There should be a prototype for each function in a

‫ﻌﺎﻟ‬
18

‫ﻰ‬
program except main. A prototype is never needed for main because it is
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
the starting point of the program. NOTE: Although some programmers

‫ﺭﺓ‬
25

‫ﻭ‬
20

make main the last function in the program, many prefer it to be first

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
because it is the program's starting point.

‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

2.4 Sending Data into a Function


‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬

CONCEPT: When a function is called, the program may send values


‫ﺍﻟ‬
‫ﺸﻴ‬

into the function.


-‫ﺦ‬

Values that are sent into a function are called arguments. You're already
76
42

familiar with how to use arguments in a function call . In the following


-2

statement the function pow is being called with two arguments, 2.0 and
18
1-

4.0 passed to it:


-1
25

result = pow(2.0, 4.0);


20

A parameter is a special variable that holds a value being passed as an


argument into a function. By using parameters, you can design your own
functions that accept data this way. Here is the definition of a function that
has a parameter. The parameter is num.

29
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
void displayValue(int num)

‫ﺩﺍ‬
‫ﺭﺓ‬
{

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
cout << ''The value is ''<< num << endl;

‫ﻮ‬
‫ﺟﻴﺎ‬
}

‫ﺍﻟ‬
‫ﻤﻌ‬
Notice that the parameter variable is defined inside the parentheses (int

‫ﻠﻮ‬
‫ﻣﺎ‬
num). Because it is declared to be an integer, the function displayValue

‫ﺕ‬
‫ﺑﻜ‬
can accept an integer value as an argument. Program 2-5 is a complete

‫ﻔﺮ‬
‫ﺍﻟ‬
program that uses this function.

‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
Program 2.5:
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

1 //This program demonstrates a function with a parameter.

‫ﻰ‬
1-

2 #include <iostream>

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
3 using namespace std;

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
5 //Function prototype

‫ﻨﻮ‬
‫ﻟﻮ‬
6 void displayValue(int num);

‫ﺟﻴ‬
7 ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
8 int main()
‫ﻠﻮ‬

9{
‫ﻣﺎ‬

10 cout << ''I am passing 5 to displayValue. \n '';


‫ﺕ‬
‫ﺑﻜ‬

11 displayValue(5); II Call displayValue with argument 5


‫ﻔﺮ‬

12 cout << ''Now I am back in main. \n '';


‫ﺍﻟ‬

13 return 0;
‫ﺸﻴ‬
-‫ﺦ‬

14 }
15
76

16 /********************************************
42

17* displayValue *
-2

18* This function uses an integer parameter *


8
1

19* whose value is displayed. *


1-

20 ********************************************I
-1
25

21 void displayValue(int num)


20

22 {
23 cout << ''The value is '' << num << endl;
24 }

30
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Program Output

‫ﺩﺍ‬
‫ﺭﺓ‬
I am passing 5 to displayValue.

‫ﻭ‬
The value is 5

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
Now I am back in main.

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
Notice the function prototype for displayValue in line 6:

‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
void displayValue(int num); // Function prototype

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
It lists both the data type and the name of the function's parameter
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
variable. However, it is not actually necessary to list the name of the
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
parameter variable inside the parentheses. Only the data type of the
18

‫ﻰ‬
1-

variable is required. The function prototype could have been written like

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

this:

‫ﻭ‬
20

‫ﺗﻜ‬
void displayValue(int); // Function prototype

‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
Because some instructors prefer that you list only the data type for each
‫ﻠﻮ‬
‫ﻣﺎ‬

parameter in a function prototype, while others prefer that you list both the
‫ﺕ‬
‫ﺑﻜ‬

data type and name, we use both versions throughout this book. Your
‫ﻔﺮ‬

instructor will tell you which version to use.


‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

NOTE: Your instructor will also tell you what to call the function
76

parameters. In this text, the values that are passed into a function are called
42

arguments, and the variables that receive those values are called
-2
8

parameters. However, there are several variations of these terms in use.


1
1-

Some call the arguments actual parameters and the parameters formal
-1
25

parameters. Others use the terms actual arguments and formal arguments.
20

Regardless of which set of terms you use, it is important to be consistent.

31
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
In Program 2-5 the displayValue function is called in line 11 of main

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
with the argument 5 inside the parentheses . The number 5 is passed into

‫ﻮ‬
‫ﺟﻴﺎ‬
num, which is displayValue' s parameter. This is illustrated in Figure 2-5.

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

Figure 2.5

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﺎﺍ‬
Any argument listed inside the parentheses of a function call is copied
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

into the function's parameter variable. In essence, parameter variables are


‫ﻣﺎ‬
‫ﺕ‬

initialized to the value of the corresponding arguments passed to them


‫ﺑﻜ‬
‫ﻔﺮ‬

when the function is called. Program 2- 6 shows the function displayValue


‫ﺍﻟ‬
‫ﺸﻴ‬

being called several times with a different argument passed each time.
-‫ﺦ‬
76
42

Program 2.6
-2

1 This program demonstrates a function with a parameter.


18

2 #include <iostream>
1-

3 using namespace std;


-1
25

4
20

5 // Function prototype
6 void displayValue(int num);
7
8 int main()
9{
10 cout << ''I am passing several values to displayValue. \n '';

32
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
11 displayValue(5); // Call displayValue with argument 5

‫ﺩﺍ‬
‫ﺭﺓ‬
12 displayValue(l0); // Call displayValue with argument 10

‫ﻭ‬
13 displayValue(2); // Call displayValue with argument 2

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
14 displayValue(16); // Call displayValue with argument 16

‫ﻮ‬
‫ﺟﻴﺎ‬
15 cout << ''Now I am back in main. \n '';
16 return 0;

‫ﺍﻟ‬
‫ﻤﻌ‬
17 }

‫ﻠﻮ‬
‫ﻣﺎ‬
18

‫ﺕ‬
19 /********************************************

‫ﺑﻜ‬
20* displayValue *

‫ﻔﺮ‬
21* This function uses an integer parameter *

‫ﺍﻟ‬
‫ﺸﻴ‬
22* whose value is displayed. *

-‫ﺦ‬
23 ********************************************I

76
24 void displayValue(int num)

‫ﻤﻌ‬
42

‫ﻬﺪ‬
25 {
-2

‫ﺍﻟ‬
26 cout << ''The value is '' << num << endl;

‫ﻌﺎﻟ‬
18

27 }

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Program Output

‫ﻭ‬
20

‫ﺗﻜ‬
I am passing several values to displayValue.

‫ﻨﻮ‬
‫ﻟﻮ‬
The value is 5

‫ﺟﻴ‬
The value is 10 ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
The value is 2
‫ﻠﻮ‬

The value is 16
‫ﻣﺎ‬

Now I am back in main.


‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬

In lines 11-14 of Program 2-6 the displayVal ue function is called four


‫ﺸﻴ‬
-‫ﺦ‬

times, and each time num takes on a different value. Any expression
76

whose value could normally be assigned to num may be used as an


42
-2

argument. For example, the following function call would pass the value 8
18

into num:
1-
-1

displayValue(3 + 5);
25
20

When a function is called, it is best if each argument passed to it has the


same data type as the parameter receiving it. However, it is possible to
send an argument with a different data type. In this case, the argument will
be promoted or demoted to match the data type of the parameter receiving

33
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
it. Be very careful if you do this, as you may introduce a hard to find bug.

‫ﺩﺍ‬
‫ﺭﺓ‬
For example, the displayValue function in Program 2-6 has an integer

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
parameter, which means it expects to receive an integer value. If the

‫ﻮ‬
‫ﺟﻴﺎ‬
function is called as shown here,

‫ﺍﻟ‬
‫ﻤﻌ‬
displayValue(4.7);

‫ﻠﻮ‬
‫ﻣﺎ‬
the argument will be truncated and the integer 4 will be stored in the

‫ﺕ‬
‫ﺑﻜ‬
parameter num. Often it is useful to pass several arguments into a

‫ﻔﺮ‬
‫ﺍﻟ‬
function. Program 2-7 includes a function that has three parameters. Look

‫ﺸﻴ‬
-‫ﺦ‬
carefully at how these parameters are defined in the function header in line

76

‫ﻤﻌ‬
27, as well as in the function prototype in line 6. In this case the prototype
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
lists variable names along with the data types of the function's parameters.

‫ﻌﺎﻟ‬
18

‫ﻰ‬
This makes the prototype look exactly like the function header except that
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
the prototype must end with a semicolon and the function header must not

‫ﺭﺓ‬
25

‫ﻭ‬
20

end with a semicolon.

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
//Prototype

‫ﺟﻴ‬
‫ﺎﺍ‬
void showSum(int numl, int num2, int num3); //Ends with ;
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

//Function header
‫ﻣﺎ‬
‫ﺕ‬

void showSum(int numl, int num2, int num3) // NO ;


‫ﺑﻜ‬
‫ﻔﺮ‬

Notice also that the call to the function in line 18 must now send three
‫ﺍﻟ‬
‫ﺸﻴ‬

arguments to the function.


-‫ﺦ‬

showSum(valuel,value2,value3);
76
42
-2

Program 2.7
8
1
1-
-1
25

1 // This program demonstrates a function with three parameters.


20

2 #include <iostream>
3 using namespace std;
4
5 // Function prototype
6 void showSum(int numl, int num2, int num3);
7

34
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
8 int main()

‫ﺩﺍ‬
‫ﺭﺓ‬
9{

‫ﻭ‬
10 int valuel, value2, value3;

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
11

‫ﻮ‬
‫ﺟﻴﺎ‬
12 // Get 3 integers
13 cout << ''Enter three integers and I will display '';

‫ﺍﻟ‬
‫ﻤﻌ‬
14 cout << ''their sum: '';

‫ﻠﻮ‬
‫ﻣﺎ‬
15 cin >> valuel >> value2 >> value3;

‫ﺕ‬
16

‫ﺑﻜ‬
17 // Call showSum, passing 3 arguments

‫ﻔﺮ‬
18 showSum(valuel, value2, value3);

‫ﺍﻟ‬
‫ﺸﻴ‬
19 return O;

-‫ﺦ‬
20 }

76
21

‫ﻤﻌ‬
42

‫ﻬﺪ‬
22 /********************************************
-2

‫ﺍﻟ‬
23 * shows num *

‫ﻌﺎﻟ‬
18

24* This function displays the sum of the *

‫ﻰ‬
1-

25* 3 integers passed into its parameters. *

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
26 ******************************************** /

‫ﺭﺓ‬
25

27 void showSum(int numl, int num2, int num3)

‫ﻭ‬
20

‫ﺗﻜ‬
28 {

‫ﻨﻮ‬
‫ﻟﻮ‬
2 9 cout << ''The sum is '' << ( numl + num2 + num3) << endl;

‫ﺟﻴ‬
30 } ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

Program Output with Example Input Shown in Bold


‫ﻣﺎ‬

Enter three integers and I will display their sum: 4 8 7[Enter]


‫ﺕ‬
‫ﺑﻜ‬

The sum is 19
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

One important point to mention about Program 2-7 is how the showSum
76
42

parameter variables are defined in its function header.


-2

void showSum(int numl, int num2, int num3)


8
1
1-

As you might expect they are each preceded by their data type and they
-1
25

are separated by commas . However, unlike regular variable definitions,


20

they cannot be combined into a single definition even if they all have the
same data type. That is, even though all three parameter variables are
integers, they cannot be defined like this:
void showSum(int numl, num2, num3) // Error!

35
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Another point to notice is that whereas the function prototype and

‫ﺩﺍ‬
‫ﺭﺓ‬
function header must list the data type of each parameter, the call to the

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
function must not list any data types. Each argument in the function call

‫ﻮ‬
‫ﺟﻴﺎ‬
must be a value or something that can be evaluated to produce a value. If

‫ﺍﻟ‬
‫ﻤﻌ‬
valuel, value2, and value3 hold the values 4, 8, and 7 respectively, as they

‫ﻠﻮ‬
‫ﻣﺎ‬
did in the sample run for Program 2-7, the following three function calls

‫ﺕ‬
‫ﺑﻜ‬
would all be legal and would cause the showsum function to display the

‫ﻔﺮ‬
‫ﺍﻟ‬
same thing.

‫ﺸﻴ‬
-‫ﺦ‬
showSum(valuel, value2, value3); // Legal The sum is 19

76

‫ﻤﻌ‬
showSum(4, 8, 7) ; // Legal The sum is 19
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
showSum( 3+1, 1612, 7) ; //Legal The sum is 19

‫ﻌﺎﻟ‬
18

‫ﻰ‬
But the following function call would cause an error.
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
showSum( int valuel, int value2, int value3); //Error!

‫ﺭﺓ‬
25

‫ﻭ‬
20

Figure 2-6 shows the difference in the syntax between the function call

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
and the function header when variables are used as arguments. It also

‫ﺟﻴ‬
‫ﺎﺍ‬
illustrates that when a function with multiple parameters is called, the
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

arguments are passed to the parameters in order.


‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42
-2
18
1-
-1
25
20

Figure 2-6
Like all variables, parameters have a scope. The scope of a parameter is
limited to the body of the function which uses it.

36
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
2.5 The Return Statement

‫ﺩﺍ‬
‫ﺭﺓ‬
Concept: The return statement causes a function to end immediately.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
When the last statement in a function has finished executing, the

‫ﻮ‬
‫ﺟﻴﺎ‬
function terminates. The program returns to the module that called it and

‫ﺍﻟ‬
‫ﻤﻌ‬
continues executing from the point immediately following the function

‫ﻠﻮ‬
‫ﻣﺎ‬
call. It is possible, however, to force a function to return to where it was

‫ﺕ‬
‫ﺑﻜ‬
called from before its last statement has been executed. This can be done

‫ﻔﺮ‬
‫ﺍﻟ‬
with the return statement, as illustrated in Program 6-10. In this program,

‫ﺸﻴ‬
-‫ﺦ‬
the function divide shows the quotient of argl divided by arg2. If arg2 is

76

‫ﻤﻌ‬
set to zero, however, the function returns back to main without performing
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
the division.

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
Program 2.8

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
1 This program uses a function to perform division.

‫ﻨﻮ‬
‫ﻟﻮ‬
2 It illustrates the return statement.

‫ﺟﻴ‬
3 #include <iostream> ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
4 using namespace std;
‫ﻠﻮ‬

5
‫ﻣﺎ‬

6 Function prototype
‫ﺕ‬
‫ﺑﻜ‬

7 void divide(double argl, double arg2);


‫ﻔﺮ‬

8
‫ﺍﻟ‬

9 int main ( )
‫ﺸﻴ‬
-‫ﺦ‬

10 {
11 double numl, num2;
76

12
42

13 cout << ''Enter two numbers and I will divide the first\n '';
-2

14 cout << ''number by the second number: '';


8
1

15 cin >> numl >> num2;


1-

16 divide(numl, num2);
-1
25

17 return O;
20

18 }
19
20/*******************************************************
21 * divide *
22 * This function uses two parameters, argl and arg2. *
23 * If arg2 does not = zero, the function displays the *

37
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
24 * result of argl/arg2. Otherwise it returns without *

‫ﺩﺍ‬
‫ﺭﺓ‬
25 * performing the division. *

‫ﻭ‬
26 ******************************************************/

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
27 void divide(double argl, double arg2)

‫ﻮ‬
‫ﺟﻴﺎ‬
28 {
29 if (arg2 == 0.0)

‫ﺍﻟ‬
‫ﻤﻌ‬
30 {

‫ﻠﻮ‬
‫ﻣﺎ‬
31 cout << ''Sorry, I cannot divide by zero. \n '';

‫ﺕ‬
32 return;

‫ﺑﻜ‬
33 }

‫ﻔﺮ‬
34 cout << ''The quotient is '' << (argl I arg2) << endl;

‫ﺍﻟ‬
‫ﺸﻴ‬
35 }

-‫ﺦ‬
76

‫ﻤﻌ‬
Program Output with Example Input Shown in Bold
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
Enter two numbers and I will divide the first

‫ﻌﺎﻟ‬
18

‫ﻰ‬
number by the second number: 12 O[Enter]
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
Sorry, I cannot divide by zero.

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
In the example running of the program, the user entered 12 and 0 as

‫ﺟﻴ‬
‫ﺎﺍ‬
input. These were stored as double values as variables numl and num2. In
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

line 16 the divide function was called, passing 12.0 into the argl parameter
‫ﻣﺎ‬
‫ﺕ‬

and 0.0 into the arg2 parameter. Inside the divide function, the if statement
‫ﺑﻜ‬
‫ﻔﺮ‬

in line 29 executes. Because arg2 is equal to 0.0, the code in lines 31 and
‫ﺍﻟ‬
‫ﺸﻴ‬

32 execute. When the return statement in line 32 executes, the divide


-‫ﺦ‬

function immediately ends. This means the cout statement in line 34 does
76
42

not execute. The program resumes at line 17 in the main function.


-2
1 8
1-

2.6 Returning a Value from a Function


-1
25
20

Concept: A function may send a value back to the part of the program
that called the function.
You've seen that data may be passed into a function by way of
parameter variables. Data may also be returned from a function back to the

38
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
statement that called it. Functions that return a value are known as value-

‫ﺩﺍ‬
‫ﺭﺓ‬
returning functions. The pow function, which you have already used, is an

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
example of a value-returning function. Here is an example:

‫ﻮ‬
‫ﺟﻴﺎ‬
double x;

‫ﺍﻟ‬
‫ﻤﻌ‬
x = pow(4.0, 2.0);

‫ﻠﻮ‬
‫ﻣﺎ‬
This code calls the pow function, passing 4.0 and 2.0 as arguments. The

‫ﺕ‬
‫ﺑﻜ‬
function calculates the value of 4.0 raised to the power of 2.0 and returns

‫ﻔﺮ‬
‫ﺍﻟ‬
that value. The value, which is 16.0, is assigned to the x variable by the =

‫ﺸﻴ‬
-‫ﺦ‬
operator.

76

‫ﻤﻌ‬
Although several arguments can be passed into a function, only one
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
value can be returned from it. Think of a function as having multiple

‫ﻌﺎﻟ‬
18

‫ﻰ‬
communication channels for receiving data (parameters), but only one
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
channel for sending data (the return value). This is illustrated in Figure 2-

‫ﺭﺓ‬
25

‫ﻭ‬
20

7.

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

Figure 2.7
42
-2
18

When you are writing a value-returning function, you must decide what
1-
-1

type of value the function will return. This is because you must specify the
25
20

data type of the return value in the function header and function prototype.
Up until now all the functions we have written have been void functions.
This means they do not return a value. These functions use the key word
void as the return type in their function header and function prototype. A

39
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
value returning function, on the other hand, uses int, double, bool, or any

‫ﺩﺍ‬
‫ﺭﺓ‬
other valid data type in its header. Here is an example of a function that

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
returns an int value:

‫ﻮ‬
‫ﺟﻴﺎ‬
int sum(int numl, int num2)

‫ﺍﻟ‬
‫ﻤﻌ‬
{

‫ﻠﻮ‬
‫ﻣﺎ‬
int result;

‫ﺕ‬
‫ﺑﻜ‬
result = numl + num2;

‫ﻔﺮ‬
‫ﺍﻟ‬
return result;

‫ﺸﻴ‬
-‫ﺦ‬
}

76

‫ﻤﻌ‬
The name of this function is sum. Notice in the function header that the
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
return type is int, as illustrated in Figure 2-8.

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
Figure 2-8.
‫ﻠﻮ‬
‫ﻣﺎ‬

This code defines a function named sum that accepts two int arguments.
‫ﺕ‬
‫ﺑﻜ‬

The arguments are passed into the parameter variables numl and num2.
‫ﻔﺮ‬
‫ﺍﻟ‬

Inside the function, the variable result is defined. Variables that are
‫ﺸﻴ‬
-‫ﺦ‬

defined inside a function are called local variables. After the variable
76

definition, the values of the parameter variables numl and num2 are added,
42
-2

and their sum is assigned to the result variable. The last statement in the
8

function is:
1
1-
-1

return result;
25

This statement causes the function to end, and it sends the value of the
20

result variable back to the statement that called the function. A value-
returning function must have a return statement written in the following
general format:

40
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
return expression;

‫ﺩﺍ‬
‫ﺭﺓ‬
In the general format, expression is the value to be returned. It can be

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
any expression that has a value, such as a variable, literal, or mathematical

‫ﻮ‬
‫ﺟﻴﺎ‬
expression. The value of the expression is converted to the data type that

‫ﺍﻟ‬
‫ﻤﻌ‬
the function returns and is sent back to the statement that called the

‫ﻠﻮ‬
‫ﻣﺎ‬
function. In this case, the sum function returns the value in the result

‫ﺕ‬
‫ﺑﻜ‬
variable.

‫ﻔﺮ‬
‫ﺍﻟ‬
However, we could have eliminated the result variable entirely and

‫ﺸﻴ‬
-‫ﺦ‬
returned the expression numl + num2, as shown in the following code:

76

‫ﻤﻌ‬
int sum(int numl, int num2)
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
{

‫ﻌﺎﻟ‬
18

‫ﻰ‬
return numl + num2;
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
}

‫ﺭﺓ‬
25

‫ﻭ‬
20

The prototype for a value-returning function follows the same

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
conventions that we covered earlier. Here is the prototype for the sum

‫ﺟﻴ‬
function: ‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

int sum(int numl, int num2);


‫ﻣﺎ‬
‫ﺕ‬

Program 2-9 shows an example of how to call the sum function.


‫ﺑﻜ‬
‫ﻔﺮ‬

Program 2.9
‫ﺍﻟ‬
‫ﺸﻴ‬

1 // This program uses a function that returns a value.


-‫ﺦ‬

2 #include <iostream>
76

3 using namespace std;


42

4
-2

5 // Function prototype
8

6 int sum(int numl, int num2);


1
1-

7
-1

8 int main()
25

9{
20

10 int valuel = 20, // The first value


11 value2 = 40, // The second value
12 total; // Holds the returned total
13
14 //Call the sum function, passing the contents of

41
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
15 //valuel and value2 as arguments. Assign the return

‫ﺩﺍ‬
‫ﺭﺓ‬
16 // value to the total variable.

‫ﻭ‬
17 total = sum(valuel, value2);

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
18

‫ﻮ‬
‫ﺟﻴﺎ‬
19 // Display the sum of the values
20 cout << ''The sum of '' << valuel << '' and ''

‫ﺍﻟ‬
‫ﻤﻌ‬
21 << value2 << '' is '' << total << endl;

‫ﻠﻮ‬
‫ﻣﺎ‬
22 return 0;

‫ﺕ‬
23 }

‫ﺑﻜ‬
24

‫ﻔﺮ‬
25 /******************************************************

‫ﺍﻟ‬
‫ﺸﻴ‬
26 * sum *

-‫ﺦ‬
27 * This function returns the sum of its two parameters. *

76
28 ******************************************************/

‫ﻤﻌ‬
42

‫ﻬﺪ‬
29 int sum(int numl, int num2)
-2

‫ﺍﻟ‬
30 {

‫ﻌﺎﻟ‬
18

31 return numl + num2;

‫ﻰ‬
1-

32 }

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Program Output

‫ﻭ‬
20

‫ﺗﻜ‬
The sum of 20 and 40 is 60

‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
Here is the statement in line 17, which calls the sum function, passing
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

valuel and value2 as arguments.


‫ﻠﻮ‬
‫ﻣﺎ‬

total = sum(valuel, value2);


‫ﺕ‬
‫ﺑﻜ‬

This statement assigns the value returned by the sum function to the
‫ﻔﺮ‬
‫ﺍﻟ‬

total variable. In this case, the function will return 60. Figure 2-9 shows
‫ﺸﻴ‬
-‫ﺦ‬

how the arguments are passed into the function and how a value is passed
76

back from the function.


42
-2
18
1-
-1
25
20

Figure 2.9
42
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
When you call a value-returning function, you usually want to do

‫ﺩﺍ‬
‫ﺭﺓ‬
something meaningful with the value it returns. Program 2-9 shows a

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
function's return value being assigned to a variable. This is commonly how

‫ﻮ‬
‫ﺟﻴﺎ‬
return values are used, but you can do many other things with them as

‫ﺍﻟ‬
‫ﻤﻌ‬
well. For example, the following code shows a math expression that uses a

‫ﻠﻮ‬
‫ﻣﺎ‬
call to the sum function:

‫ﺕ‬
‫ﺑﻜ‬
int x = 10, y = 15;

‫ﻔﺮ‬
‫ﺍﻟ‬
double average;

‫ﺸﻴ‬
-‫ﺦ‬
average = sum(x, y) / 2.0;

76

‫ﻤﻌ‬
In the last statement, the sum function is called with x and y as its
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
arguments. The function's return value, which is 25, is divided by 2.0. The

‫ﻌﺎﻟ‬
18

‫ﻰ‬
result, 12.5, is assigned to average. Here is another example:
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
int x = 10, y = 15;

‫ﺭﺓ‬
25

‫ﻭ‬
20

cout << ''The sum is '' << sum(x, y) << endl;

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
This code sends the sum function's return value to cout so it can be

‫ﺟﻴ‬
‫ﺎﺍ‬
displayed on the screen. The message ''The sum is 25'' will be displayed.
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

Remember, a value-returning function returns a value of a specific data


‫ﻣﺎ‬
‫ﺕ‬

type. You can use the function's return value anywhere that you can use a
‫ﺑﻜ‬
‫ﻔﺮ‬

regular value of the same data type. This means that anywhere an int value
‫ﺍﻟ‬
‫ﺸﻴ‬

can be used, a call to an int value-returning function can be used.


-‫ﺦ‬

Likewise, anywhere a double value can be used, a call to a double value-


76
42

returning function can be used. The same is true for all other data types.
-2

Let's look at another example. Program 2-10, which calculates the area
81
1-

of a circle, has two functions in addition to main. One of the functions is


-1
25

named square, and it returns the square of any number passed to it as an


20

argument. The square function is called in a mathematical statement. The


program also has a function named getRadius, which prompts the user to
enter the circle's radius. The value entered by the user is returned from the
function.

43
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Program 2.10

‫ﺩﺍ‬
‫ﺭﺓ‬
1 // This program demonstrates two value-returning functions.

‫ﻭ‬
‫ﺗﻜ‬
2 //The square function is called in a mathematical statement.

‫ﻨ‬
‫ﻮﻟ‬
3 #include <iostream>

‫ﻮ‬
‫ﺟﻴﺎ‬
4 #include <iomanip>

‫ﺍﻟ‬
5 using namespace std;

‫ﻤﻌ‬
‫ﻠﻮ‬
6

‫ﻣﺎ‬
7 //Function prototypes

‫ﺕ‬
8 double getRadius();

‫ﺑﻜ‬
‫ﻔﺮ‬
9 double square(double number);

‫ﺍﻟ‬
10

‫ﺸﻴ‬
11 int main ( )

-‫ﺦ‬
12 {
76

‫ﻤﻌ‬
13 canst double PI =3.14159; // Constant for pi
42

‫ﻬﺪ‬
14 double radius; // Holds the circle 's radius
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
15 double area; // Holds the circle 's area
18

‫ﻰ‬
16
1-

‫ﻟﻺ‬
17 // Set the numeric output formatting
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

18 cout << fixed << showpoint << setprecision(2);

‫ﻭ‬
20

19

‫ﺗﻜ‬
‫ﻨﻮ‬
20 //Get the radius of the circle

‫ﻟﻮ‬
‫ﺟﻴ‬
21 cout << ''This program calculates the area of a circle. \n '';
22 radius = getRadius();
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

23
‫ﻠﻮ‬
‫ﻣﺎ‬

24 // Caclulate the area of the circle


‫ﺕ‬

25 area = PI * square(radius);
‫ﺑﻜ‬

26
‫ﻔﺮ‬

27 // Display the area


‫ﺍﻟ‬
‫ﺸﻴ‬

28 cout << ''The area is '' << area << endl;


-‫ﺦ‬

29 return 0;
76

30 }
42

31
-2

32 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
8

33 * getRadius *
1
1-

34 * This function returns the circle radius *


-1

35 * input by the user. *


25

36 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
20

37 double getRadius()
38 {
39 double rad;
40
41 cout << ''Enter the radius of the circle: '';

44
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
42 cin >> rad;

‫ﺩﺍ‬
‫ﺭﺓ‬
43 return rad;

‫ﻭ‬
44 }

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
45

‫ﻮ‬
‫ﺟﻴﺎ‬
46 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
47 * square *

‫ﺍﻟ‬
‫ﻤﻌ‬
48 * This function returns the square of the *

‫ﻠﻮ‬
‫ﻣﺎ‬
49 * double argument sent to it *

‫ﺕ‬
50 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /

‫ﺑﻜ‬
51 double square(double number)

‫ﻔﺮ‬
52 {

‫ﺍﻟ‬
‫ﺸﻴ‬
53 return number * number;

-‫ﺦ‬
54 }

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
Program Output with Example Input Shown in Bold

‫ﻌﺎﻟ‬
18

This program calculates the area of a circle.

‫ﻰ‬
1-

Enter the radius of the circle: 10[Enter]

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
The area is 314.16

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
First, look at the getRadius function which is defined in lines 37

‫ﺟﻴ‬
‫ﺎﺍ‬
through 44. Notice that there is nothing inside the parentheses of the
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

function header on line 37. This means the function has no parameters, so
‫ﻣﺎ‬
‫ﺕ‬

no arguments are sent to it when it is called. The purpose of this function


‫ﺑﻜ‬
‫ﻔﺮ‬

is to get the circle radius from the user. In line 39 the function defines a
‫ﺍﻟ‬
‫ﺸﻴ‬

local variable, rad. Line 41 displays a prompt and line 42 accepts the user's
-‫ﺦ‬

input for the circle's radius, which is stored in the rad variable. In line 43
76
42

the value of the rad variable is returned. The getRadius function is called
-2

in line 22 of the main function. When the value is returned from the
8
1
1-

getRadius function it is assigned to the radius variable.


-1
25

Next look at the square function, which is defined in lines 51 through


20

54. When the function is called, a double argument is passed to it. The
function stores the argument in its number parameter. The return statement
in line 53 returns the value of the expression number * number, which is
the square of the value in the number parameter. The square function is
45
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
called in line 25 of the main function, with the value of radius passed as an

‫ﺩﺍ‬
‫ﺭﺓ‬
argument. The square function will return the square of the radius variable,

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
and that value will be used in the mathematical expression that computes

‫ﻮ‬
‫ﺟﻴﺎ‬
the circle's area.

‫ﺍﻟ‬
‫ﻤﻌ‬
Assuming the user has entered 10. 0 as the radius, and this value is

‫ﻠﻮ‬
‫ﻣﺎ‬
passed as an argument to the square function, the function will return the

‫ﺕ‬
‫ﺑﻜ‬
value 100. 0. Figure 2-10 illustrates how this value is passed back to be

‫ﻔﺮ‬
‫ﺍﻟ‬
used in the mathematical expression.

‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
Figure 2.10 ‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬

Functions can return values of any type. Both the getRadius and square
‫ﻠﻮ‬
‫ﻣﺎ‬

functions in Program 6-10 return a double. The sum function you saw in
‫ﺕ‬
‫ﺑﻜ‬

Program 6-9 returned an int. When a statement calls a value-returning


‫ﻔﺮ‬
‫ﺍﻟ‬

function, it should properly handle the return value. For example, if you
‫ﺸﻴ‬
-‫ﺦ‬

assign the return value of the square function to a variable, the variable
76

should be a double. If the return value of the function has a fractional


42
-2

portion and you assign it to an int variable, the value will be truncated.
18
1-
-1
25
20

46
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
[Q2] Review question

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
Q1- Is the following a function header or a function call?

‫ﻮ‬
‫ﺟﻴﺎ‬
calcTotal();

‫ﺍﻟ‬
‫ﻤﻌ‬
Q2 Is the following a function header or a function call?

‫ﻠﻮ‬
‫ﻣﺎ‬
void showResults()

‫ﺕ‬
‫ﺑﻜ‬
Q3 What will the output of the following program be if the user enters 10?

‫ﻔﺮ‬
‫ﺍﻟ‬
#include <iostream>

‫ﺸﻴ‬
using namespace std;

-‫ﺦ‬
void f unc 1 ( )
76

‫ﻤﻌ‬
{
42

‫ﻬﺪ‬
cout << ''Able was I\n'';
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
}
18

‫ﻰ‬
void func2()
1-

‫ﻟﻺ‬
{
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

cout << ''I saw Elba \n '';

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
int main()

‫ﻟﻮ‬
‫ﺟﻴ‬
{
int input;
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

cout << ''Enter a number: '';


‫ﻠﻮ‬
‫ﻣﺎ‬

cin >> input;


‫ﺕ‬

if (input < 10)


‫ﺑﻜ‬

{
‫ﻔﺮ‬

funcl();
‫ﺍﻟ‬
‫ﺸﻴ‬

func2();
-‫ﺦ‬

}
76

else
42

{
-2

func2();
8

funcl();
1
1-

}
-1

return 0;
25

}
20

Q4- Indicate which of the following is the function prototype, the function
header, and the function call:
void showNum(double num)
void showNum(double);

47
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 2 Functions

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
showNum(45.67);

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
Q5- Write a function named times Ten. The function should have an

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
integer parameter named number. When timesTen is called, it should

‫ﻮ‬
‫ﺟﻴﺎ‬
display the product of number times 10. (Note: just write the function. Do
not write a complete program.)

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
Q7- What is the output of the following program?

‫ﺕ‬
#include <iostream>

‫ﺑﻜ‬
using namespace std;

‫ﻔﺮ‬
void showDouble(int value); //Function prototype

‫ﺍﻟ‬
‫ﺸﻴ‬
int main ( )

-‫ﺦ‬
{

76
int num;

‫ﻤﻌ‬
42

‫ﻬﺪ‬
for (num = 0; num < 10; num++)
-2

‫ﺍﻟ‬
showDouble(num);

‫ﻌﺎﻟ‬
18

return O;

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
// Definition of function showDouble

‫ﺭﺓ‬
25

void showDouble(int value)

‫ﻭ‬
20

‫ﺗﻜ‬
{

‫ﻨﻮ‬
‫ﻟﻮ‬
cout << value << " " << (value * 2) << endl;

‫ﺟﻴ‬
} ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42
-2
18
1-
-1
25
20

48
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 3 Pointers

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Chapter 3

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
Pointer

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
This guide aims at providing a practical summary of c++ pointers and

‫ﺑﻜ‬
‫ﻔﺮ‬
pointer use. The first thing a student needs to know about the concept of

‫ﺍﻟ‬
‫ﺸﻴ‬
pointers is the fact that it is an extremely important and useful tool. Many

-‫ﺦ‬
students have an initial idea that pointers are difficult. They are not if you
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
really understand them. Combine information found in this summary with
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
the class notes and the textbook for more comprehensive understanding of
18

‫ﻰ‬
1-

‫ﻟﻺ‬
pointers. Finally, remember that practice makes perfect. The more you
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

solve, the easier pointers will be.

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
3.1Pointer vs. Variables
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
Knowing the difference between pointers and variables is the first step
‫ﻠﻮ‬
‫ﻣﺎ‬

toward understanding pointers. You have used many variables in your


‫ﺕ‬
‫ﺑﻜ‬

programming so far:
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

int x ;
-‫ﺦ‬

double y;
bool z;
76

char c;
42

string d
-2
18
1-

Pointers are themselves variables that have special characteristics. For


-1
25

instance, they have their own special way of declaring them. The general
20

rule of declaring any pointer is as follows:

49
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 3 Pointers

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
type * pointer_name ;

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
Compare it to the general rule of declaring a normal variable:

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
type type variable_name ;

‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‘type’ can be any valid data type. Including:

‫ﺸﻴ‬
-‫ﺦ‬
• Primitive Data Types ( int, double, float, char, bool, ..)

76

‫ﻤﻌ‬
• User-defined Data Types (Structs or Classes) ( Circle, Rect,
42

‫ﻬﺪ‬
-2

Record, Student ..)

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
• Type can also be a pointer. In other words, pointers are data types
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
too.

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
Here are some examples:

‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
Normal Variables ‫ﺎﺍ‬
Pointer Variables
‫ﻟ‬
‫ﻤﻌ‬

int x; int * xptr;


‫ﻠﻮ‬

double y;
‫ﻣﺎ‬

double *yptr;
‫ﺕ‬

Circle mycircle; // Circle is struct Circle * mycircleptr;


‫ﺑﻜ‬

Car mycar; // Car is a class


‫ﻔﺮ‬

Car * mycarptr;
float z
‫ﺍﻟ‬

float * zptr // pointer to float


‫ﺸﻴ‬

float ** zptrptr; // pointer to float pointer


-‫ﺦ‬
76

3.2Pointer Initialization
42
-2

Initialization means assigning an appropriate value to a defined


18
1-

variable. Let’s compare between how we initialized normal variables and


-1
25

how we initialize pointers. The general rule of initializing any pointer is as


20

follows:

variable_name = valid_address;

50
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 3 Pointers

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Compare it to the general rule of declaring a normal variable:

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
variable_name = valid_value;

‫ﻨ‬
‫ﻮ‬ ‫ﻮﻟ‬
‫ﺟﻴﺎ‬
By valid address we mean either zero (0), null ( NULL) or the address

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
of variable that meets the pointer’s expectation. To elaborate, suppose

‫ﻣﺎ‬
‫ﺕ‬
xptr is a pointer to float. Then only an address to a float variable can

‫ﺑﻜ‬
‫ﻔﺮ‬
match the expectation of xptr. Here are some examples

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
float x = 5;

76
float * xptr;

‫ﻤﻌ‬
42
xptr = &x;

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
float ** xptrptr ; // xptrptr is a pointer to “float pointer”
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
xptrptr = &xptr ; // notice that &xptr will give the address of the

‫ﺭﺓ‬
25

‫ﻭ‬
20

//float pointer xptr. Therefore, it is a valid initialization.

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
The & is an operator. Remember the ! operator (negation operator) that ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

we used to negate the value of a Boolean expression or variable. The &


‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

(address) operator tells the c++ compiler to look up the address of what
‫ﺑﻜ‬
‫ﻔﺮ‬

comes after the operator and return it. So &x will give the address of x
‫ﺍﻟ‬
‫ﺸﻴ‬

and &y will give me the address of y and so on.


-‫ﺦ‬
76
42

3.3 Pointer Dereferencing


-2

To dereference a pointer is to access ( read or write ) the content of the


18
1-

address that the pointer is pointing to.


-1
25

The general rule of dereferencing any pointer is as follows:


20

type variable = value ;


type * pointer = & variable;
*pointer = value ; // write
cout<<*pointer ; // read

51
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 3 Pointers

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Compare it to the general rule of referencing a normal variable:

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
type variable = value; // write

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
cout<<variable; // read

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
To access the content of the address pointed to by the pointer we simply

‫ﻣﺎ‬
‫ﺕ‬
use the * operator followed by the pointer name. We do that for read or

‫ﺑﻜ‬
‫ﻔﺮ‬
write. Here are some examples. float x = 5;

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
float * xptr;
xptr = &x;
76

‫ﻤﻌ‬
cout<<x<<endl;
42

‫ﻬﺪ‬
-2

*xptr = 10;

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
cout<<x<<endl;
18

‫ﻰ‬
cout<<*xptr<<endl;
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
3.4 Pointers & Arrays (Pointer Arithmetic)

‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
Arrays are nothing but a set of variables of a certain type that hold
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
consecutive memory locations. The name of the array is a pointer to the
‫ﻠﻮ‬
‫ﻣﺎ‬

first element of the array. If I know where to find the first element of the
‫ﺕ‬
‫ﺑﻜ‬

array and I know the size of that array, I can pretty much reach any point
‫ﻔﺮ‬

in the array easily.


‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

The general rule of creating an array is as follows:


76

type arrayname [size];


42
-2
8

Because arrays are nothing but pointers I can give the array another
1
1-
-1

name as follows:
25
20

type arrayname[size];
type * ptr ;
ptr = arrayname;

52
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 3 Pointers

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
To access the content of the array we use the following notation:

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
for ( int I = 0 ; I < size ; I++)

‫ﻨ‬
‫ﻮﻟ‬
{

‫ﻮ‬
‫ﺟﻴﺎ‬
cout<<arrayname[I]<<endl;

‫ﺍﻟ‬
‫ﻤﻌ‬
}

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
To access the array using an equivalent pointer we use the following:

‫ﺑﻜ‬
‫ﻔﺮ‬
for ( int I = 0 ; I < size ; I++)

‫ﺍﻟ‬
{

‫ﺸﻴ‬
cout<< *(ptr+I)<<endl;

-‫ﺦ‬
76

‫ﻤﻌ‬
} 42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
Remember that ptr is the address of the first element. C++ allows us to
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
use ptr+1 to mean the address of the second element of the array and so
25

‫ﻭ‬
20

‫ﺗﻜ‬
on. The following notations are equivalent:

‫ﻨﻮ‬
‫ﻟﻮ‬
Normal Array Access Pointer Array Access
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
arrayname[I] *(ptr+I)
‫ﻠﻮ‬
‫ﻣﺎ‬

arrayname[0] *(ptr+0)
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬

arrayname[2] *(ptr+2)
‫ﺍﻟ‬
‫ﺸﻴ‬

arrayname[x-4] *(ptr+x-4)
-‫ﺦ‬
76
42

Adding or subtracting from pointers helps moving forward or backward


-2
8

inside arrays. This is referred to pointer arithmetic.


1
1-
-1
25

3.5 Pointers & Functions


20

Since we know by now that pointers and arrays are closely related, we
can pass arrays to functions using pointers. The following is a declaration

53
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 3 Pointers

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
of a function that receives an array.

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
return_type function_name ( type array [], int size);

‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
The following is a declaration of a function that receives pointer:

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
return_type function_name ( type * ptr, int size);

‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
To pass the array to the function we do the following
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
return_type function_name ( type array [], int size);

‫ﻌﺎﻟ‬
18

int main ( )

‫ﻰ‬
1-

‫ﻟﻺ‬
{
-1

‫ﺩﺍ‬
type arrayname[size];

‫ﺭﺓ‬
25

function_name(arrayname,size);

‫ﻭ‬
20

‫ﺗﻜ‬
}

‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
To pass the array using pointers we do the following:
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

return_type function_name ( type * ptr, int size);


‫ﺑﻜ‬

int main ( )
‫ﻔﺮ‬

{
‫ﺍﻟ‬
‫ﺸﻴ‬

type arrayname[size];
-‫ﺦ‬

type * ptr = arrayname;


76

type *anotherptr = &arrayname[0];


42

function_name(ptr,size);
-2

function_name(anotherptr,size);
}
8
1
1-
-1
25
20

3.6 Pointers & User-defined Types

User defined types are structs and classes. The use of pointers with
structs and classes is similar. Assuming we have created a class

54
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 3 Pointers

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
class_name and struct struct_name. The following is how we access the

‫ﺩﺍ‬
‫ﺭﺓ‬
data of a struct with pointers.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬‫ﻮ‬
‫ﺟﻴﺎ‬
struct_name mystruct;
struct_name * structptr = &mystruct;

‫ﺍﻟ‬
‫ﻤﻌ‬
structptr->datamember; // method 1

‫ﻠﻮ‬
‫ﻣﺎ‬
(*structptr).datamember; // method 2

‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
Both method 1 and method 2 are equivalent. However, method 1 uses

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
fewer parentheses and is easier to read.

76

‫ﻤﻌ‬
42
Compare it to the normal way of accessing struct data:

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
struct_name mystruct;
1-

‫ﻟﻺ‬
mystruct.datamember1;
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
The following is how we can access class functions and public data using

‫ﺟﻴ‬
pointers: ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

class_name myclass;
‫ﺑﻜ‬

class_name * classptr = & myclass; classptr -


‫ﻔﺮ‬

>member_function1(); // method 1
‫ﺍﻟ‬
‫ﺸﻴ‬

classptr->public_data_member1;// method 1
-‫ﺦ‬

(*classptr).memberfunction(); // method 2
76

(*classptr).public_data_member1; //method 2
42
-2
1 8
1-
-1

Remember using public data members is not a suggested OOP


25

approach. The better approach is to define accessors (get functions) and


20

modifiers ( set functions) and use them to access class data.

55
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 3 Pointers

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
[Q3] Review Questions

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
Q1- Write a statement that displays the address of the variable count.

‫ﻨ‬
‫ﻮﻟ‬‫ﻮ‬
‫ﺟﻴﺎ‬
Q2- Write a statement defining a variable dPtr. The variable should be a

‫ﺍﻟ‬
‫ﻤﻌ‬
pointer to a double.

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
Q3- What is the output of the following program?

‫ﺑﻜ‬
‫ﻔﺮ‬
#include <iostream>

‫ﺍﻟ‬
using namespace std;

‫ﺸﻴ‬
-‫ﺦ‬
int main()
{
76

‫ﻤﻌ‬
int x = 50, y = 60, z = 70;
42

‫ﻬﺪ‬
int *ptr;
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
cout << x << '' '' << y << '' '' << z << endl;
18

‫ﻰ‬
ptr = &x;
1-

‫ﻟﻺ‬
*ptr *= 10;
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

ptr = &y;

‫ﻭ‬
20

‫ﺗﻜ‬
*ptr *= 5;

‫ﻨﻮ‬
ptr = &z;

‫ﻟﻮ‬
‫ﺟﻴ‬
*ptr *= 2;
cout << x << '' '' << y << '' "<<z<<endl; ‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬

return 0;
‫ﻠﻮ‬
‫ﻣﺎ‬

}
‫ﺕ‬
‫ﺑﻜ‬

Q4- Rewrite the following loop so it uses pointer notation (with the
‫ﻔﺮ‬

indirection operator) instead of subscript notation.


‫ﺍﻟ‬
‫ﺸﻴ‬

for (int x = O; x < 100; x++)


-‫ﺦ‬

cout << array[x] << endl;


76
42
-2
8
1
1-
-1
25
20

56
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Chapter 4

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
Arrays and linked list

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
4.1 Basic concept of Arrays

‫ﺑﻜ‬
‫ﻔﺮ‬
There are two basic methods by which collections of related data can be

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
stored: as arrays or lists. An array is a data structure where all elements are

76

‫ﻤﻌ‬
stored in contiguous memory, the defining parameters of an array are the
42

‫ﻬﺪ‬
memory address of its first element (called the base address), the size of
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

each element (e.g. 4 bytes for an integer, 1 byte for a char, etc.), and the

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

number of elements.

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
Arrays allow random access to their elements, which means that, given

‫ﻨﻮ‬
‫ﻟﻮ‬
the index of the element required, its location can be calculated by adding
‫ﺟﻴ‬
‫ﺎﺍ‬
an offset to the base address. This calculation involves the same number of
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

steps for all elements of the array, so the access time for any array element
‫ﻣﺎ‬
‫ﺕ‬

is constant. Arrays also require only as much space as is needed to store


‫ﺑﻜ‬
‫ﻔﺮ‬

data itself, no extra space pointers or other marked is needed.


‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

The main disadvantages of an array is that its size is fixed, either at


76

compilation or when space is allocated dynamically .sufficient space must


42
-2

be allocated for the largest number of elements that will ever be used in
18

the program, which can result in a lot of wasted space if most applications
1-
-1

only require a fraction of that space. Also, certain algorithms are less
25
20

efficient if used on data stored in arrays.

The main alternative to the array is the linked list. A list is a data
structure in which the first element is stored on its own, but is provided
with a pointer that pointed to the location of the second object, which may

57
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
be stored in a different area of memory. The second object in turn has a

‫ﺩﺍ‬
‫ﺭﺓ‬
pointer to the third, and so on, until the last object has a null pointer

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
associated with it to indicate that it is the end of the list.

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
The main advantage of a linked list over an array is that the sizes of the

‫ﻤﻌ‬
‫ﻠﻮ‬
list need not to be determined in advance, since extra space can be

‫ﻣﺎ‬
‫ﺕ‬
allocated as needed. Disadvantages are that extra space is needed to store

‫ﺑﻜ‬
‫ﻔﺮ‬
the pointer associated with each element, and that access to internal list

‫ﺍﻟ‬
‫ﺸﻴ‬
elements may take more time than with an array, since finding an internal

-‫ﺦ‬
list element list requires starting at the first element and following the
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
chain of pointers until the required element is located.
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
Although it is possible to implement a linked list using arrays, it is
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
inefficient to do so. Before we get into this, we need to examine the

‫ﺭﺓ‬
25

‫ﻭ‬
20

structure of a list in a bit more detail. A list node consists of two main

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
parts. The first part contains the actual information that is stored in the

‫ﺟﻴ‬
node. This could be something as simple as an int, or something as ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

complex as a complete dossier of information on some employee in a


‫ﻣﺎ‬
‫ﺕ‬

company. The other part of a list node is the pointer to the next node in the
‫ﺑﻜ‬
‫ﻔﺮ‬

list.
‫ﺍﻟ‬
‫ﺸﻴ‬

The overall list contains only a single pointer, which points to the
-‫ﺦ‬
76

beginning of the list. We will find that many of the list algorithms are
42

easier to implement if we always begin a list with a dummy node called


-2
8

the head node. The head node not used to store any of the actual data
1
1-

present in the list, although it could be used to store information pertaining


-1
25

to the list as a whole, such as the number of nodes .Note that the pointer in
20

the head node points to a value of null at this stage. The list pointer plus
the head node thus constitutes an empty list (see figure 4.1)

58
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
List pointer

‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
Data Pointer

‫ﻮ‬
null

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
Figure 4.1

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
The head node contains a pointer to the first data node in the list (or a

‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
null pointer if the list is empty).The space for this first data node, however,

-‫ﺦ‬
is not allocated until the data to be inserted in the node are actually
76

‫ﻤﻌ‬
provided to the program. Thus the class method for inserting a new node
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
into a linked list must handle this process

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
4.2 What is an array?
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
An array is a collection of data storage locations, each having the same

‫ﻨﻮ‬
‫ﻟﻮ‬
data type and the same name. Each storage location in an array is called

‫ﺟﻴ‬
an array element. Why do you need arrays in your programs" This
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

question can be answered with an example. If you're keeping track of your


‫ﻣﺎ‬
‫ﺕ‬

business expenses for 1998 and filing your receipts by month, you could
‫ﺑﻜ‬
‫ﻔﺮ‬

have a separate folder for each month's receipts, but it would be more
‫ﺍﻟ‬
‫ﺸﻴ‬

convenient to have a single folder with 12 compartments.


-‫ﺦ‬
76

Extend this example to computer programming. Imagine that you're


42
-2

designing a program to keep track of your business expenses. The


8

program could declare 12 separate variables, one for each month's


1
1-
-1

expense total. This approach is analogous to having 12 separate folders


25
20

for your receipts. Good programming practice, however, would utilize an


array with 12 elements, storing each month's total in the corresponding
array element. This approach is comparable to filing your receipts in a

59
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
single folder with 12 compartments. Figure 4.2 illustrates the difference

‫ﺩﺍ‬
‫ﺭﺓ‬
between using individual variables and an array.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
An array
Individual variables
42

‫ﻬﺪ‬
-2

Figure 4.2: Variables are like individual folders, whereas an array is like a

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
single folder with many compartments.
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

4.3 Single-Dimensional Arrays:

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
A single-dimensional array has only a single subscript. A subscript is a

‫ﺟﻴ‬
‫ﺎﺍ‬
number in brackets that follows an array's name. This number can identify
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

the number of individual elements in the array. An example should make


‫ﻣﺎ‬
‫ﺕ‬

this clear. For the business expenses program, you could use the following
‫ﺑﻜ‬
‫ﻔﺮ‬

line to declare an array of type float:


‫ﺍﻟ‬
‫ﺸﻴ‬

float expenses[12];
-‫ﺦ‬
76
42

The array is named expenses, and it contains 12 elements. Each of the


-2

12 elements is the exact equivalent of a single float variable. All of C's


1 8

data types can be used for arrays. C array elements are always numbered
1-
-1

starting at 0, so the 12 elements of expenses are numbered 0 through 11.


25
20

In the preceding example, January's expense total would be stored in


expenses [0], February's in expenses [1], and so on.

60
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
When you declare an array, the compiler sets aside a block of memory

‫ﺩﺍ‬
‫ﺭﺓ‬
large enough to hold the entire array. Individual array elements are stored

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
in sequential memory locations, as shown in figure 4.2.

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
int array [10]

‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
Array[0] Array[1] Array[2] ………………………………… Array[8] Array[9]
….

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
Figure 4.2: Array elements are stored in sequential memory loctions
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
Individual elements of the array are accessed by using the array name

‫ﻌﺎﻟ‬
18

‫ﻰ‬
followed by the element subscript enclosed in square brackets. For
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
example, the following statement stores the value 89.95 in the second

‫ﺭﺓ‬
25

‫ﻭ‬
20

array element (remember, the first array element is expenses[0], not

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
expenses[1]):

‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
expenses[1] = 89.95;
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

Likewise, the statement


‫ﺑﻜ‬
‫ﻔﺮ‬

expenses[1] = expenses[10]
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42

0 34.35
-2

1 89.95
18
1-

2 121.01
-1

5
25
20

……
……..

10 89.95

61
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
When you use arrays, keep the element numbering scheme in mind: in

‫ﺩﺍ‬
‫ﺭﺓ‬
an array of n elements, the allowable subscripts range from 0 to n-1. If you

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
use the subscript value n, you might get program errors.

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
Warning: Remember that array elements start with 0, not 1. Also

‫ﻤﻌ‬
‫ﻠﻮ‬
remember that the last element is one less than the number of elements

‫ﻣﺎ‬
in the array. For example, an array with 10 elements contains elements

‫ﺕ‬
‫ﺑﻜ‬
0 through 9.

‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
Sometimes you might want to treat an array of n elements as if its

‫ﻤﻌ‬
42

‫ﻬﺪ‬
elements were numbered 1 through n. For instance, in the previous
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

example, a more natural method might be to store January’s expense total

‫ﻰ‬
1-

‫ﻟﻺ‬
in expenses [1], February’s in expenses [2], and so on. The simplest way
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

to do this is to declare the array with one more element than needed, and

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
ignore element 0. In this case, you would declare the array as follows. You

‫ﻟﻮ‬
‫ﺟﻴ‬
could also store some related data in element 0 (the yearly expense total,
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

perhaps).
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

Float expenses [13];


‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

Use arrays instead of creating several variables that store the same
-‫ﺦ‬

thing. For example, if you want to store total sales for each month of the
76
42

year, create an array with 12 elements to hold sales rather than creating
-2

a sales variable for each month.


18
1-
-1
25

4.3.1 Implementation
20

The program expenses.c in Listing 8.1 demonstrates the use of an array.


This is a simple program with no real practical use; however, it helps
demonstrate the use of an array.

62
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Listing 4.1 expenses.c. Using an array

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
1: /* expenses.c - Demonstrates use of an array */

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
2:

‫ﻮ‬
‫ﺟﻴﺎ‬
3: #include <stdio.h>
4:

‫ﺍﻟ‬
‫ﻤﻌ‬
5: /* Declare an array to hold expenses, and a counter variable */

‫ﻠﻮ‬
‫ﻣﺎ‬
6:

‫ﺕ‬
7: float expenses[13];

‫ﺑﻜ‬
8: int count;

‫ﻔﺮ‬
9:

‫ﺍﻟ‬
‫ﺸﻴ‬
10: int main( void )

-‫ﺦ‬
11: {

76
12: /* Input data from keyboard into array */

‫ﻤﻌ‬
42

‫ﻬﺪ‬
13:
-2

‫ﺍﻟ‬
14: for (count = 1; count < 13; count++)

‫ﻌﺎﻟ‬
18

15: {

‫ﻰ‬
1-

‫ﻟﻺ‬
16: printf(“Enter expenses for month %d: “, count);
-1

‫ﺩﺍ‬
17: scanf(“%f”, &expenses[count]);

‫ﺭﺓ‬
25

18: }

‫ﻭ‬
20

‫ﺗﻜ‬
19:

‫ﻨﻮ‬
‫ﻟﻮ‬
20: /* Print array contents */

‫ﺟﻴ‬
21: ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
22: for (count = 1; count < 13; count++)
‫ﻠﻮ‬

23: {
‫ﻣﺎ‬
‫ﺕ‬

24: printf(“Month %d = $%.2f\n”, count, expenses[count]);


‫ﺑﻜ‬

25: }
‫ﻔﺮ‬

26: return 0;
‫ﺍﻟ‬
‫ﺸﻴ‬

27: }
-‫ﺦ‬

Input/Output
76
42

Enter expenses for month 1: 100


-2

Enter expenses for month 2: 200.12


8
1

Enter expenses for month 3: 150.50


1-
-1

Enter expenses for month 4: 300


25

Enter expenses for month 5: 100.50


20

Enter expenses for month 6: 34.25


Enter expenses for month 7: 45.75
Enter expenses for month 8: 195.00
Enter expenses for month 9: 123.45
Enter expenses for month 10: 111.11
Enter expenses for month 11: 222.20
63
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Enter expenses for month 12: 120.00

‫ﺩﺍ‬
‫ﺭﺓ‬
Month 1 = $100.00

‫ﻭ‬
Month 2 = $200.12

‫ﻨ‬‫ﺗﻜ‬
‫ﻮﻟ‬
Month 3 = $150.50

‫ﻮ‬
‫ﺟﻴﺎ‬
Month 4 = $300.00
Month 5 = $100.50

‫ﺍﻟ‬
‫ﻤﻌ‬
Month 6 = $34.25

‫ﻠﻮ‬
‫ﻣﺎ‬
Month 7 = $45.75

‫ﺕ‬
Month 8 = $195.00

‫ﺑﻜ‬
Month 9 = $123.45

‫ﻔﺮ‬
Month 10 = $111.11

‫ﺍﻟ‬
‫ﺸﻴ‬
Month 11 = $222.20

-‫ﺦ‬
Month 12 = $120.00

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
Analysis
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
When you run expenses.c, the program prompts you to enter expenses
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
for months 1 through 12. The values you enter are stored in an array. You

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
must enter a value for each month. After the 12th value is entered, the

‫ﻨﻮ‬
‫ﻟﻮ‬
array contents are displayed on-screen.

‫ﺟﻴ‬
‫ﺎﺍ‬
The flow of the program is similar to listings you’ve seen before. Line 1
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

starts with a comment that describes what the program does. Notice that
‫ﻣﺎ‬
‫ﺕ‬

the name of the program, expenses.c, is included. When the name of the
‫ﺑﻜ‬
‫ﻔﺮ‬

program is included in a comment, you know which program you’re


‫ﺍﻟ‬
‫ﺸﻴ‬

viewing. This is helpful when you’re reviewing printouts of a listing.


-‫ﺦ‬

Line 5 contains an additional comment explaining the variables that are


76
42

being declared. In line 7, an array of 13 elements is declared. In this


-2

program, only 12 elements are needed, one for each month, but 13 have
18
1-

been declared. The for loop in lines 14 through 18 ignores element 0. This
-1
25

lets the program use elements 1 through 12, which relate directly to the 12
20

months. Going back to line 8, a variable, count, is declared and is used


throughout the program as a counter and an array index.
The program’s main() function begins on line 10. As stated earlier, this
program uses a for loop to print a message and accept a value for each of
64
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
the 12 months. Notice that in line 17, the scanf() function uses an array

‫ﺩﺍ‬
‫ﺭﺓ‬
element. In line 7, the expenses array was declared as float, so %f is used.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
The address-of operator (&) also is placed before the array element, just

‫ﻮ‬
‫ﺟﻴﺎ‬
as if it were a regular type float variable and not an array element.

‫ﺍﻟ‬
‫ﻤﻌ‬
Lines 22 through 25 contain a second for loop that prints the values just

‫ﻠﻮ‬
‫ﻣﺎ‬
entered. An additional formatting command has been added to the printf()

‫ﺕ‬
‫ﺑﻜ‬
function so that the expenses values print in a more orderly fashion. For

‫ﻔﺮ‬
‫ﺍﻟ‬
now, know that %.2f prints a floating number with two digits to the right

‫ﺸﻴ‬
-‫ﺦ‬
of the decimal. Additional formatting commands are covered in more

76

‫ﻤﻌ‬
detail on Day 14, “Working with the Screen, Printer, and Keyboard.”
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

4.3.2 Array Operations:

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
Now that you know what arrays are when they are used, and how they

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
store elements, we will dive into their operations like insertion and

‫ﻨﻮ‬
‫ﻟﻮ‬
removal.
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
[Link] The operation of Traversing:
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

Traversing is an operation on the array. It is the process where you


‫ﺑﻜ‬
‫ﻔﺮ‬

access each and every element present in an array. Accessing means


‫ﺍﻟ‬
‫ﺸﻴ‬

visiting every element at least ones, just to display them to the user or
-‫ﺦ‬

perform an operation on all the elements. For example, when you have the
76
42

percentage of every element, you go through each and every element,


-2

performing the necessary operations. Traversal is the most basic of the


18
1-

operations that can be performed on any data structures. Here is a


-1
25

pseudocode representation of the general algorithm.


20

65
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
ALGORITHM: (Traversing of Linear Array) Here Arr is a linear array

‫ﺭﺓ‬
with lower bound L and upper bound U

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
1. [initialize counter] set k = LB

‫ﻮ‬
‫ﺟﻴﺎ‬
2. repeat steps 3 and 4 while k<= UB

‫ﺍﻟ‬
3. [visit the element] apply process to a[k]

‫ﻤﻌ‬
4. [increase counter] set k = k + 1

‫ﻠﻮ‬
‫ﻣﺎ‬
5. [end of step 2 loop]

‫ﺕ‬
6. Exit

‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
[Link] The operation of Searching:

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
In an unsorted array, the search operation can be performed by linear

‫ﻌﺎﻟ‬
18

‫ﻰ‬
traversal from the first element to the last element. Every item is checked
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
and if a match is found then that particular item is returned, otherwise the

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
search continues till the end of the data collection. Here is a pseudocode

‫ﻨﻮ‬
‫ﻟﻮ‬
representation of the general algorithm.

‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

ALGORITHM: (Searching of Linear Array)


‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

1. Repeat step 2& 3 while loc<numer


‫ﺑﻜ‬

2. if array[loc] = item [search for element in array]


‫ﻔﺮ‬

3. print “element found at position loc” and exit


‫ﺍﻟ‬
‫ﺸﻴ‬

o [end of if structure]
-‫ﺦ‬

4. set loc = loc + 1


76

o [end of step 1 loop]


42

5. [Link] loc>=number
-2

6. display “element not found”


8

7. exit
1
1-
-1
25

Example: We have linear array A as below:


20

10 14 19 26 27 31 33 35 42 44

66
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Suppose item to be searched is 33. We will start from beginning and will

‫ﺩﺍ‬
‫ﺭﺓ‬
compare 33 with each element. This process will continue until element is

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
found or array is finished. Here

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

1) Compare 10 with 33, 10 # 33, go to next element.

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

2) Compare 14 with 33, 14 # 33, go to next element.


42
-2
18
1-
-1
25
20

67
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
3) Compare 19 with 33, 19 # 33, go to next element.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
4) Compare 26 with 33, 26 # 33, go to next element.

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

5) Compare 27 with 33, 27 # 33, go to next element.


-‫ﺦ‬
76
42
-2
8
1
1-
-1
25
20

6) Compare 31 with 33, 31 # 33, go to next element.


68
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
7) Compare 33 with 33, 33 = 33, so 33 is found and its location is 6
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
[Link] The operation of Insertion:

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
Insert operation is to insert one or more data elements into an array.
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Based on the requirement, a new element can be added at any given index

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
of array. For inserting an element in 1-D array, one has to shift elements

‫ﻟﻮ‬
‫ﺟﻴ‬
one position down. Here is a pseudocode representation of the general
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

algorithm.
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

ALGORITHM :(Inserting into a Linear Array) INSERT (Arr, N, K,


‫ﺑﻜ‬
‫ﻔﺮ‬

ITEM)
‫ﺍﻟ‬

Here Arr is a linear array with N elements, K is positive integer such


‫ﺸﻴ‬
-‫ﺦ‬

that K<=N and ITEM is the element to be inserted.


76
42

1. [Initialize Counter] Set I=N.


-2

[Link] steps 3 and 4 while I>=K.


8

[Link] Arr[I+1] =Arr[I].


1
1-

4. [Decrease Counter] Set I=I-1.


-1
25

5. [Inserting ITEM] Set Arr[K]=ITEM.


20

6. [Reset Number of Elements]. Set N=N+1.


7. Exit

Example: We have linear array A as below:


69
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
10 20 50 30 15

‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
New element to be inserted is 100 and location for insertion is 4. So shift

‫ﺍﻟ‬
‫ﻤﻌ‬
the elements from 4th location to 2nd location downwards by 1 place. And

‫ﻠﻮ‬
‫ﻣﺎ‬
then insert 100 at 2ndlocation. It is shown below:

‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

[Link] The operation of Deletion:


‫ﺑﻜ‬
‫ﻔﺮ‬

In delete operation, the element to be deleted is searched using the


‫ﺍﻟ‬
‫ﺸﻴ‬

linear search and then delete operation is performed followed by shifting


-‫ﺦ‬
76

the elements. Here is a pseudocode representation of the general


42

algorithm.
-2
18
1-
-1
25
20

70
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
ALGORITHM :(Deleting From Linear Array)

‫ﻭ‬
Here Arr is a linear array with N elements ,K is positive integer such

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
that K<=N and Kth element is to be deleted.

‫ﻮ‬
‫ﺟﻴﺎ‬
1.[Initialize Counter] Set I=K.

‫ﺍﻟ‬
‫ﻤﻌ‬
[Link] steps 3 and 4 while I=N-1.

‫ﻠﻮ‬
‫ﻣﺎ‬
[Link][I]=Arr=[I+1].

‫ﺕ‬
4.[Increase Counter]Set I=I+1.

‫ﺑﻜ‬
5.[Reset Number Of Elements].Set N=N-1.

‫ﻔﺮ‬
[Link]

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

Example: We have linear array A as below:

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
10 20 50 40 25 60
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
The element to be deleted is 50 which is at 3rd location. So shift the

‫ﻟﻮ‬
‫ﺟﻴ‬
elements from 4th to 6th location upwards by 1 place. It is shown below:
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42
-2
8
1
1-
-1
25
20

After deletion the array will be:


10 20 40 25 60 --

71
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
4.4 Multidimensional Arrays:

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
A multidimensional array has more than one subscript. A two-

‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
dimensional array has two subscripts; a three-dimensional array has three

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
subscripts, and so on. There is no limit to the number of dimensions a C

‫ﻤﻌ‬
‫ﻠﻮ‬
array can have. (There is a limit on total array size, as discussed later in

‫ﻣﺎ‬
‫ﺕ‬
today’s lesson.)

‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
For example, you might write a program that plays checkers. The

‫ﺸﻴ‬
-‫ﺦ‬
checkerboard contains 64 squares arranged in eight rows and eight

76

‫ﻤﻌ‬
columns. Your program could represent the board as a two-dimensional
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
array, as follows:

‫ﻌﺎﻟ‬
18

‫ﻰ‬
int checker[8][8];
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

The resulting array has 64 elements: checker[0][0], checker[0][1],

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
checker[0][2] ... checker[7][6], checker[7][7]. The structure of this two-

‫ﻟﻮ‬
‫ﺟﻴ‬
dimensional array is illustrated in Figure 4.3.
‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

int checker[8][8];
‫ﻣﺎ‬
‫ﺕ‬

Checker [0][0] Checker [0][1] Checker [0][7]


‫ﺑﻜ‬
‫ﻔﺮ‬

Checker [1][0] Checker [1][1] Checker [1][7]


‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

Checker [2][0] Checker [2][1] Checker [2][7]


76
42
-2

Checker [7][0] Checker [7][1] Checker [7][7]


18

Figure 4.3: A two-dimensional array has a row-and-column structure.


1-
-1
25
20

Similarly, a three-dimensional array could be thought of as a cube.


Four-dimensional arrays (and higher) are probably best left to your
imagination. All arrays, no matter how many dimensions they have, are
stored sequentially in memory.

72
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
4.4.1 Implementation

‫ﺩﺍ‬
‫ﺭﺓ‬
Listing 4.2 Random.c creates a multidimensional array

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
1: /* random.c - Demonstrates using a multidimensional array */

‫ﻮ‬
‫ﺟﻴﺎ‬
2:
3: #include <stdio.h>

‫ﺍﻟ‬
‫ﻤﻌ‬
4: #include <stdlib.h>

‫ﻠﻮ‬
‫ﻣﺎ‬
5: /* Declare a three-dimensional array with 1000 elements */

‫ﺕ‬
6:

‫ﺑﻜ‬
7: int random_array[10][10][10];

‫ﻔﺮ‬
8: int a, b, c;

‫ﺍﻟ‬
‫ﺸﻴ‬
9:

-‫ﺦ‬
10: int main( void )

76
11: {

‫ﻤﻌ‬
42

‫ﻬﺪ‬
12: /* Fill the array with random numbers. The C library */
-2

‫ﺍﻟ‬
13: /* function rand() returns a random number. Use one */

‫ﻌﺎﻟ‬
18

14: /* for loop for each array subscript. */

‫ﻰ‬
1-

‫ﻟﻺ‬
15:
-1

‫ﺩﺍ‬
16: for (a = 0; a < 10; a++)

‫ﺭﺓ‬
25

17: {

‫ﻭ‬
20

‫ﺗﻜ‬
18: for (b = 0; b < 10; b++)

‫ﻨﻮ‬
‫ﻟﻮ‬
19: {

‫ﺟﻴ‬
20: for (c = 0; c < 10; c++) ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
21: {
‫ﻠﻮ‬

22: random_array[a][b][c] = rand();


‫ﻣﺎ‬
‫ﺕ‬

23: }
‫ﺑﻜ‬

24: }
‫ﻔﺮ‬

25: }
‫ﺍﻟ‬
‫ﺸﻴ‬

26:
-‫ﺦ‬

27: /* Now display the array elements 10 at a time */


28:
76

29: for (a = 0; a < 10; a++)


42

30: {
-2

31: for (b = 0; b < 10; b++)


8
1

32: {
1-
-1

33: for (c = 0; c < 10; c++)


25

34: {
20

35: printf(“\nrandom_array[%d][%d][%d] = “, a, b, c);


36: printf(“%d”, random_array[a][b][c]);
37: }
38: printf(“\nPress Enter to continue, CTRL-C to quit.”);
39:
40: getchar();
73
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
41: }

‫ﺩﺍ‬
‫ﺭﺓ‬
42: }

‫ﻭ‬
43: return 0;

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
44: } /* end of main() */

‫ﻮ‬
‫ﺟﻴﺎ‬
Output

‫ﺍﻟ‬
‫ﻤﻌ‬
random_array[0][0][0] = 346

‫ﻠﻮ‬
‫ﻣﺎ‬
random_array[0][0][1] = 130

‫ﺕ‬
random_array[0][0][2] = 10982

‫ﺑﻜ‬
random_array[0][0][3] = 1090

‫ﻔﺮ‬
random_array[0][0][4] = 11656

‫ﺍﻟ‬
‫ﺸﻴ‬
random_array[0][0][5] = 7117

-‫ﺦ‬
random_array[0][0][6] = 17595

76
random_array[0][0][7] = 6415

‫ﻤﻌ‬
42

‫ﻬﺪ‬
random_array[0][0][8] = 22948
-2

‫ﺍﻟ‬
random_array[0][0][9] = 31126

‫ﻌﺎﻟ‬
18

Press Enter to continue, CTRL-C to quit.

‫ﻰ‬
1-

‫ﻟﻺ‬
random_array[0][1][0] = 9004
-1

‫ﺩﺍ‬
random_array[0][1][1] = 14558

‫ﺭﺓ‬
25

random_array[0][1][2] = 3571

‫ﻭ‬
20

‫ﺗﻜ‬
random_array[0][1][3] = 22879

‫ﻨﻮ‬
‫ﻟﻮ‬
random_array[0][1][4] = 18492

‫ﺟﻴ‬
random_array[0][1][5] = 1360 ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
random_array[0][1][6] = 5412
‫ﻠﻮ‬

random_array[0][1][7] = 26721
‫ﻣﺎ‬
‫ﺕ‬

random_array[0][1][8] = 22463
‫ﺑﻜ‬

random_array[0][1][9] = 25047
‫ﻔﺮ‬

Press Enter to continue, CTRL-C to quit


‫ﺍﻟ‬
‫ﺸﻴ‬

... ...
-‫ﺦ‬

random_array[9][8][0] = 6287
random_array[9][8][1] = 26957
76

random_array[9][8][2] = 1530
42

random_array[9][8][3] = 14171
-2

random_array[9][8][4] = 6951
81

random_array[9][8][5] = 213
1-
-1

random_array[9][8][6] = 14003
25

random_array[9][8][7] = 29736
20

random_array[9][8][8] = 15028
random_array[9][8][9] = 18968
Press Enter to continue, CTRL-C to quit.
random_array[9][9][0] = 28559
random_array[9][9][1] = 5268
random_array[9][9][2] = 20182
74
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
random_array[9][9][3] = 3633

‫ﺩﺍ‬
‫ﺭﺓ‬
random_array[9][9][4] = 24779

‫ﻭ‬
random_array[9][9][5] = 3024

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
random_array[9][9][6] = 10853

‫ﻮ‬
‫ﺟﻴﺎ‬
random_array[9][9][7] = 28205
random_array[9][9][8] = 8930

‫ﺍﻟ‬
‫ﻤﻌ‬
random_array[9][9][9] = 2873

‫ﻠﻮ‬
‫ﻣﺎ‬
Press Enter to continue, CTRL-C to quit.

‫ﺕ‬
‫ﺑﻜ‬
Analysis

‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
Before you look at the for statements in detail, note that lines 7 and 8

-‫ﺦ‬
declare four variables. The first is an array named random_array, used to
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
hold random numbers. random_array is a three-dimensional type int
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
array that is 10-by-10-by-10, giving a total of 1,000 type int elements
18

‫ﻰ‬
1-

‫ﻟﻺ‬
(10*10*10). Imagine coming up with 1,000 unique variable names if you
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

couldn’t use arrays! Line 8 then declares three variables, a, b, and c, used

‫ﻭ‬
20

‫ﺗﻜ‬
to control the for loops.

‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
This program also includes the header file stdlib.h (for standard
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
library) on line 4. It is included to provide the prototype for the rand()
‫ﻠﻮ‬
‫ﻣﺎ‬

function used on line 22.


‫ﺕ‬
‫ﺑﻜ‬

The bulk of the program is contained in two nests of for statements. The
‫ﻔﺮ‬

first is in lines16 through 25, and the second is in lines 29 through 42.
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

Both for nests have the same structure. They work just like the loops in
76

Listing 6.2, but they go one level deeper. In the first set of for statements,
42

line 22 is executed repeatedly. Line 22 assigns the return value of a


-2
8

function, rand(), to an element of the random_array array, where rand() is


1
1-
-1

a library function that returns a random number.


25

Going backward through the listing, you can see that line 20 changes
20

variable c from 0 to 9. This loops through the farthest right subscript of


the random_array array. Line 18 loops through b, the middle subscript of
the random array. Each time b changes, it loops through all the c

75
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
elements. Line 16 increments variable a, which loops through the farthest

‫ﺩﺍ‬
‫ﺭﺓ‬
left subscript. Each time this subscript changes, it loops through all 10

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
values of subscript b, which in turn loop through all 10 values of c. This

‫ﻮ‬
‫ﺟﻴﺎ‬
loop initializes every value in the random array to a random number.

‫ﺍﻟ‬
‫ﻤﻌ‬
Lines 29 through 42 contain the second nest of for statements. These

‫ﻠﻮ‬
‫ﻣﺎ‬
work like the previous for statements, but these loops print each of the

‫ﺕ‬
‫ﺑﻜ‬
values assigned previously. After 10 are displayed, line 38 prints a

‫ﻔﺮ‬
‫ﺍﻟ‬
message and waits for Enter to be pressed. Line 40 takes care of the

‫ﺸﻴ‬
-‫ﺦ‬
keypress using getchar(). If Enter hasn’t been pressed, getchar() waits

76

‫ﻤﻌ‬
until it is. Run this program and watch the displayed values.
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
4.5 Initializing Arrays
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
• One – Dimensional Arrays

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
You can initialize all or part of an array when you first declare it.

‫ﻟﻮ‬
‫ﺟﻴ‬
Follow the array declaration with an equal sign and a list of values
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

enclosed in braces and separated by commas. The listed values are


‫ﻠﻮ‬
‫ﻣﺎ‬

assigned in order to array elements starting at number 0. For this


‫ﺕ‬
‫ﺑﻜ‬

example, the following code assigns the value 100 is assigned to


‫ﻔﺮ‬
‫ﺍﻟ‬

array[0], the value 200 is assigned to array[1], the value 300 is


‫ﺸﻴ‬
-‫ﺦ‬

assigned to array[2], and the value 400 is assigned to array[3].


76

int array[4] = { 100, 200, 300, 400 };


42
-2

If you omit the array size, the compiler creates an array just large
18
1-

enough to hold the initialization values. Thus, the following


-1
25

statement would have exactly the same effect as the previous array
20

declaration statement:
int array[] = { 100, 200, 300, 400 };

76
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
You can, however, include too few initialization values, as in this

‫ﺩﺍ‬
‫ﺭﺓ‬
example:

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
int array[10] = { 1, 2, 3 };

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
If you don’t explicitly initialize an array element, you can’t be

‫ﻤﻌ‬
‫ﻠﻮ‬
sure what value it holds when the program runs. If you include too

‫ﻣﺎ‬
‫ﺕ‬
many initializers (more initializers than array elements), the

‫ﺑﻜ‬
‫ﻔﺮ‬
compiler detects an error. According to the ANSI standard, the

‫ﺍﻟ‬
‫ﺸﻴ‬
elements that are not initialized will be set to zero.

-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
• Multidimensional Arrays:
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
Multidimensional arrays can also be initialized. The list of
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
initialization values is assigned to array elements in order, with the

‫ﺭﺓ‬
25

‫ﻭ‬
20

last array subscript changing first. For example:

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
int array[4][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

‫ﺟﻴ‬
results in the following assignments:
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

array[0][0] is equal to 1
‫ﺑﻜ‬
‫ﻔﺮ‬

array[0][1] is equal to 2
‫ﺍﻟ‬
‫ﺸﻴ‬

array[0][2] is equal to 3
-‫ﺦ‬

array[1][0] is equal to 4
76
42

array[1][1] is equal to 5
-2

array[1][2] is equal to 6
18
1-

array[2][0] is equal to 7
-1
25

array[2][1] is equal to 8
20

array[2][2] is equal to 9
array[3][0] is equal to 10
array[3][1] is equal to 11
array[3][2] is equal to 12

77
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
When you initialize multidimensional arrays, you can make your

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
source code clearer by using extra braces to group the initialization

‫ﻮ‬
‫ﺟﻴﺎ‬
values and also by spreading them over several lines. The following

‫ﺍﻟ‬
‫ﻤﻌ‬
initialization is equivalent to the one just given:

‫ﻠﻮ‬
‫ﻣﺎ‬
int array[4][3] = {{ 1, 2, 3 },{ 4, 5, 6 },{ 7, 8, 9 },{ 10, 11, 12 } };

‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
Remember, initialization values must be separated by a comma—

‫ﺍﻟ‬
‫ﺸﻴ‬
even when there is a brace between them. Also, be sure to use

-‫ﺦ‬
braces in pairs a closing brace for every opening brace—or the
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
compiler becomes confused.
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
4.6 Linked Lists
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
In order to avoid the linear cost of insertion and deletion, we need to

‫ﺭﺓ‬
25

‫ﻭ‬
20

ensure that the list is not stored contiguously, since otherwise entire parts

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
of the list will need to be moved. Figure 4.3shows the general idea of a

‫ﺟﻴ‬
linked list. ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬

Null
‫ﺕ‬

A1 A2 A3 A4 A5
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬

Figure 4.3
‫ﺸﻴ‬
-‫ﺦ‬
76

The linked list consists of a series of structures, which are not


42
-2

necessarily adjacent in memory. Each structure conations the element and


8

a pointer to a structure containing its successor. We call this the next


1
1-
-1

pointer. The last cell's Next pointer points to NULL.


25
20

Recall that a pointer variable is just a variable that contains the address
where some other data are sorted. Thus if P is declared to be pointer to a
structure, then the value stored in P is interpreted as the location, in main
memory, where a structure can be found. A field of that structure can be

78
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
accessed by P->FieldName, where FieldName is the name of the field we

‫ﺩﺍ‬
‫ﺭﺓ‬
wish to examine. Figure 4.5 shows the actual representation of the list in

‫ﻭ‬
‫ﻨ‬ ‫ﺗﻜ‬
‫ﻮﻟ‬
Figure 4.4. The list contains five structures, which happen to reside in

‫ﻮ‬
‫ﺟﻴﺎ‬
memory locations 1000, 800, 712, 922, 692, respectively. The next pointer

‫ﺍﻟ‬
‫ﻤﻌ‬
in the first structure has the value 800, which provides the indication of

‫ﻠﻮ‬
‫ﻣﺎ‬
where the second structure is. The other structures each have a pointer that

‫ﺕ‬
‫ﺑﻜ‬
serves a similar purpose. Of course, in order to access this list we need to

‫ﻔﺮ‬
‫ﺍﻟ‬
know where the first cell can be found. A pointer variable can be used for

‫ﺸﻴ‬
-‫ﺦ‬
this purpose. It is important to remember that a pointer is just a number.

76

‫ﻤﻌ‬
For the rest of this chapter we will draw pointers with arrows because they
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
are more illustrative.

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
A1 800 A2 712 A3 992 A4 692 A5 0

‫ﺭﺓ‬
25

1000 800 712 992 692

‫ﻭ‬
20

‫ﺗﻜ‬
Figure 4.4

‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬

A1 A2 A3 A4 A5 Null
‫ﺕ‬
‫ﺑﻜ‬

Figure 4.5
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

CONCEPT: Dynamically allocated data structures may be linked


76

together in memory to form a chain (linked list).


42
-2
8
1

A linked list is a series of connected nodes, where each node is a data


1-
-1

structure. A linked list can grow or shrink in size as the program runs.
25

This is possible because the nodes in a linked list are dynamically


20

allocated. If new information needs to be added to a linked list, the


program simply allocates another node and inserts it into to the series.
If a particular piece of information needs to be removed from the
linked list, the program deletes the node containing that information.

79
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
4.6.1 The composition of a linked list

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
Each node in a linked list contains one or more members that represent

‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
data. (Perhaps the nodes hold inventory record, or customer names,

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
address, and telephone numbers).In addition to the data, each node

‫ﻤﻌ‬
‫ﻠﻮ‬
contains a pointer, which can point to another node. The make-up of a

‫ﻣﺎ‬
‫ﺕ‬
node is illustrated in figure 4.6

‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
Data Members Pointer

‫ﺸﻴ‬
-‫ﺦ‬
Figure 4.6

76

‫ﻤﻌ‬
A linked list is called "linked" because each node in the series has a
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
pointer that points to the next node in the list. This creates a chain where

‫ﻌﺎﻟ‬
18

‫ﻰ‬
the first node points to the second node, the second node points to the third
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
node, and so on. This is illustrated in figure 4.7

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
List head

‫ﻟﻮ‬
Null

‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
Figure 4.7
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

The list depicted in figure 4.7 has three nodes, plus a pointer known as
‫ﺑﻜ‬
‫ﻔﺮ‬

the list head. The head simply points to the first node in the list. Each
‫ﺍﻟ‬
‫ﺸﻴ‬

node, in turn, points to the next node in the list. (Node 1 points to node 2,
-‫ﺦ‬

which points to node3.) Since node 3 is the last one, it points to the
76
42

address (address 0). This is usually how the end of a linked list is signified
-2

by letting the last node point to NULL.


8
1
1-

Since the ListNode structure shown above contains a pointer to an


-1
25

object of the same type, it known as a self-referential data structure. This


20

makes it possible to create nodes that point to other nodes of the same
type.

80
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Note: The illustration above depicts the nodes in the linked list as being

‫ﺩﺍ‬
‫ﺭﺓ‬
very close to each other, neatly arranged in a raw. In reality, the nodes

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
may be scattered various parts of memory

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
4.6.2 Linked List Operations

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
The basic list operations are appending a node, traversing the list,

‫ﺑﻜ‬
‫ﻔﺮ‬
inserting a node, deleting a node, and destroying the list.

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
class FloatList
42

‫ﻬﺪ‬
{
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
private:
18

‫ﻰ‬
// Declare a structure for the list
1-

‫ﻟﻺ‬
struct ListNode
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

float value;

‫ﺗﻜ‬
‫ﻨﻮ‬
struct ListNode *next;

‫ﻟﻮ‬
‫ﺟﻴ‬
};
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

ListNode *head; // List head pointer


‫ﻠﻮ‬
‫ﻣﺎ‬

public:
‫ﺕ‬

FloatList(void) // Constructor
‫ﺑﻜ‬

{ head = NULL; }
‫ﻔﺮ‬

~FloatList(void); // Destructor
‫ﺍﻟ‬
‫ﺸﻴ‬

void appendNode(float);
-‫ﺦ‬

void insertNode(float);
76

void deleteNode(float);
42

void displayList(void);
-2

};
8
1
1-
-1
25

Notice that the constructor initializes the head pointer to NULL, thereby
20

indicating that the list starts out empty. The class has an add function that
takes a value and adds it to the end of the list, as well as a displayList
function that prints to the screen all values stored in the list. A destructor
function destroys the list by deleting all its nodes. With the exception of

81
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
remove ( ) , all of these functions are defined in NumberList. cpp. The

‫ﺩﺍ‬
‫ﺭﺓ‬
remove ( ) function will be added later.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
[Link] Appending a Node to the List

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
To append a node to a linked list means to add the node to the end of

‫ﻣﺎ‬
‫ﺕ‬
the list. The appendNode member function accepts a float argument, num.

‫ﺑﻜ‬
‫ﻔﺮ‬
The function will allocate a new ListNode structure, store the value in

‫ﺍﻟ‬
‫ﺸﻴ‬
num in the node's value member, and then append the node to the end of

-‫ﺦ‬
76
the list. Here is a pseudocode representation of the general algorithm.

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
Create a new node.
18

‫ﻰ‬
Store a data in the new node.
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
If there are no nodes in the list

‫ﺭﺓ‬
25

Make the new node the first node.

‫ﻭ‬
20

‫ﺗﻜ‬
Else

‫ﻨﻮ‬
‫ﻟﻮ‬
Traverse the list to find the last node

‫ﺟﻴ‬
Add the new node to the end of the list ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

End If.
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬

Let's step through pseudocode above, observing how the appendNode


‫ﺍﻟ‬
‫ﺸﻴ‬

function build a linked list to store the following three argument


-‫ﺦ‬

values:
76
42
-2

• [Link](2.5)
8

• [Link](7.9)
1
1-
-1

• [Link](12.6)
25
20

The head pointer is automatically initialized to 0 (Null), which indicates


that the list is empty. The first call to appendNode passes 2.5 as the
argument. In the following statement, a new node is allocated in memory,
2.5 is copied into its value number, and null is assigned to the node's next

82
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
pointer. Figure 4.8 illustrates the state of the Head pointer and the new

‫ﺩﺍ‬
‫ﺭﺓ‬
node.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
newNode = new ListNode;

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
[Link]=num;

‫ﻤﻌ‬
‫ﻠﻮ‬
[Link]=Null;

‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
Null 2.5 Null

‫ﺍﻟ‬
‫ﺸﻴ‬
head New Node

-‫ﺦ‬
Figure 4.8
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
The next statement to execute is the following if statement.
18

‫ﻰ‬
1-

‫ﻟﻺ‬
If (head==Null)
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Head=newNode

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
Since head points to NULL, the condition (head== null) is true. The

‫ﺟﻴ‬
statement head= newNode is executed, making newNode the first node in ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

the list. This is illustrated in Figure 4.9


‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬

2.5 Null
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

head New Node


-‫ﺦ‬
76

Figure 4.9
42
-2
8

There are no more statements to execute, so control returns to function


1
1-

main. In the second call to appendNode, 7.9 is passes as the argument.


-1
25

Once again, the first three statements in the algorithm create a new node,
20

store the argument in the node's value member, and assign its next pointer
to Null. Figure 4.10 illustrates the current state of the list and the new
node.

83
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
7.9 Null

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
NewNode

‫ﻮﻟ‬
head 2.5 Null

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
Figure 4.10

‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
Since no longer points to NULL, the else part of the if statement executes:

‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
Else Part

-‫ﺦ‬
76
Initialize nodePtr to head of list

‫ﻤﻌ‬
42

‫ﻬﺪ‬
While [Link] != null
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

nodePtr=[Link]

‫ﻰ‬
1-

‫ﻟﻺ‬
insert newNode as the last node ([Link]=newNode)
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
The first statement in the else block assigns the value in head to nodePtr.

‫ﻟﻮ‬
‫ﺟﻴ‬
This causes nodePtr to point to the same node that head points to. This
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

illustrated in figure 4.11


‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬

7.9 Null
‫ﺍﻟ‬
‫ﺸﻴ‬

head NewNode
2.5 Null
-‫ﺦ‬
76
42

nodePt
-2

r
1 8

Figure 4.11
1-
-1
25
20

Look at the next member of the node that nodePtr point to. Its value is
NULL, which means that [Link] also points to Null. nodePtr is
already at the end of the list, so the while loop immediately terminates.
The last statement, [Link]=newNode, causes [Link] to point

84
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
to the new node. This inserts new node at the end of the list as shown in

‫ﺩﺍ‬
‫ﺭﺓ‬
figure 4.12.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
head
2.5 Null

‫ﻠﻮ‬
7.9

‫ﻣﺎ‬
NewNode

‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
nodePt

‫ﺍﻟ‬
r

‫ﺸﻴ‬
Figure 4.12

-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
The third time appendNode is called, 12.6 is passes as the argument. Once
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
again, the first statements create a node with the argument stored in the
18

‫ﻰ‬
1-

‫ﻟﻺ‬
value member. This is shown in Figure 4.13.
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
head

‫ﻟﻮ‬
2.5 Null

‫ﺟﻴ‬
7.9 12.6 Null
NewNode ‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

Figure 4.13
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬

Next, the else part of the if statement executes. As before, nodePtr is


‫ﺍﻟ‬
‫ﺸﻴ‬

made to point to the same node as head as shown in Figure 4.14.


-‫ﺦ‬
76
42
-2

head
8

2.5 7.9 Null 12.6 Null


1
1-

NewNode
-1
25

nodePtr
20

Figure 4.14

Since [Link] is not NULL, the while loop will execute. After its first
iteration, nodePtr will point to the second node in the list as in Figure 4.15.

85
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
head
2.5 Null

‫ﻭ‬
7.9 12.6 Null

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
NewNode

‫ﻮ‬
‫ﺟﻴﺎ‬
nodePtr

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
Figure 4.15

‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
The while loop's conditional test fail after the first iteration because

‫ﺍﻟ‬
‫ﺸﻴ‬
[Link] now points to NULL. The last statement, [Link]=

-‫ﺦ‬
newNode, causes [Link] to point to the new node. This inserts
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
newNode at the end of the list as shown in Figure 4.16.
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

head

‫ﺩﺍ‬
2.5 7.9 12.6 Null

‫ﺭﺓ‬
25

New Node

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
nodePtr

‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
Figure 4.16
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

The figure above depicts the final state of the linked list. The following
‫ﺑﻜ‬
‫ﻔﺮ‬

code is the actual c++ code for the append function.


‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

void FloatList::appendNode(float num)


42

{
-2

ListNode *newNode, *nodePtr;


1 8
1-

// Allocate a new node & store num


-1

newNode = new ListNode;


25

newNode->value = num;
20

newNode->next = NULL;
// If there are no nodes in the list
// make newNode the first node
if (!head)
head = newNode;

86
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
else // Otherwise, insert newNode at end

‫ﺩﺍ‬
‫ﺭﺓ‬
{

‫ﻭ‬
// Initialize nodePtr to head of list

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
nodePtr = head;

‫ﻮ‬
‫ﺟﻴﺎ‬
// Find the last node in the list
while (nodePtr->next)

‫ﺍﻟ‬
‫ﻤﻌ‬
nodePtr = nodePtr->next;

‫ﻠﻮ‬
‫ﻣﺎ‬
// Insert newNode as the last node

‫ﺕ‬
nodePtr->next = newNode;

‫ﺑﻜ‬
}

‫ﻔﺮ‬
}

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
[Link] Traversing a linked list
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
The appendNode function demonstrated in the previous section contains
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
a while loop that traverses, or travels through the linked list. In this section

‫ﺭﺓ‬
25

‫ﻭ‬
20

we will demonstrate the displayList member function which traverses the

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
list, displaying the value member of each node. The following pseudocode

‫ﺟﻴ‬
represents the algorithm. ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬

Assign List head to node pointer


‫ﺕ‬

While node pointer is not NULL


‫ﺑﻜ‬
‫ﻔﺮ‬

Display the value member of the node pointed to


‫ﺍﻟ‬
‫ﺸﻴ‬

node pointer
-‫ﺦ‬

Assign node pointer to its own next member.


76

End while
42
-2
18

Let's step through pseudocode above, observing how the traverse


1-
-1

function displays the value member of each node in the following List
25
20

in Figure 4.17:

87
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
head

‫ﻭ‬
2.5 7.9 10.7 12.6 Null

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
Figure 4.17

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
First, assign List head to node pointer as shown in Figure 4.18.

‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
head

-‫ﺦ‬
2.5 7.9 10.7 12.6 Null

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

nodePtr

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
Figure 4.18
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

While (nodePtr != NULL)

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
Display [Link]

‫ﺟﻴ‬
nodePtr=[Link] ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬

head
2.5 7.9 10.7 12.6 Null
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

nodePtr
42

Figure 4.19
-2
1 8
1-
-1
25

head
2.5 7.9 10.7 12.6 Null
20

nodePtr

Figure 4.20

88
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
head

‫ﻮﻟ‬
2.5 7.9 10.7 12.6 Null

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
nodePtr

‫ﻣﺎ‬
‫ﺕ‬
Figure 4.21

‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
Figure 4.21 shows that the [Link]== NULL, the function

-‫ﺦ‬
terminates the while loop.
76

‫ﻤﻌ‬
42
The following code is c++ code of the traverse operation pseudocode:

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

void FloatList::displayList(void)

‫ﻰ‬
1-

‫ﻟﻺ‬
{
-1

‫ﺩﺍ‬
ListNode *nodePtr;

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
nodePtr = head;

‫ﻨﻮ‬
‫ﻟﻮ‬
while (nodePtr)

‫ﺟﻴ‬
{ ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
cout << nodePtr->value << endl;
‫ﻠﻮ‬

nodePtr = nodePtr->next;
‫ﻣﺎ‬
‫ﺕ‬

}
‫ﺑﻜ‬

}
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

[Link] Inserting a Node


-‫ﺦ‬
76

Appending a node is a straightforward procedure. Inserting a node in


42
-2

the middle of a list, however, is more involved. For example, suppose the
18

values in a list are stored and you will all new values to be inserted in their
1-
-1

proper position. This will preserve the order of the list. Using the ListNode
25
20

structure again, the pseudocode below shows an algorithm, for finding a


new nodes proper position in the list and inserting there. The algorithm
assumes the nodes in the list are already in order.

89
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
Create a new node

‫ﺭﺓ‬
Store the data in the new node.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
If there are no nodes in the list

‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
Make the new node the first node.
Else

‫ﺍﻟ‬
‫ﻤﻌ‬
Find the first node whose value is greater than or equal the new

‫ﻠﻮ‬
‫ﻣﺎ‬
value, or the end of the list (whichever is first).

‫ﺕ‬
‫ﺑﻜ‬
Insert the new node before the found node, or at the end of the

‫ﻔﺮ‬
list if no node was found

‫ﺍﻟ‬
‫ﺸﻴ‬
End If

-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
Notice that the new algorithm finds the first node whose value is greater
18

‫ﻰ‬
1-

‫ﻟﻺ‬
than or equal to the new value. The new node is then inserted before the
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

found node. This will require the use of two node pointers during the

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
traversal: one to point to the node being inspected, and another to point to

‫ﻟﻮ‬
‫ﺟﻴ‬
the previous node. ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

Let's step through pseudocode above, observing how the insertNode


‫ﻣﺎ‬
‫ﺕ‬

function which is called with argument 10.5, will insert the node in the
‫ﺑﻜ‬
‫ﻔﺮ‬

list of (2.5, 7.9, 12.6). In insertNode, a new node is created and the
‫ﺍﻟ‬
‫ﺸﻴ‬

function argument is copied to its member. Since the list is already has
-‫ﺦ‬

nodes stored in it, the else part of the If statement will execute. It begins
76
42

by assigning nodePtr to Head. Figure 4.22 illustrates the state of the list at
-2

this point.
8
1
1-
-1
25

nodePtr
20

7.9

head 2.5 12.6 Null

10.5 ???
newNode

90
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Figure: 4.22

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
Since nodePtr is not null and [Link] is less than num, the while

‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
loop will iterate. During the iteration, previousNode will be made to pint

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
to the node that nodePtr is pointing to. nodePtr will then be advanced to

‫ﻤﻌ‬
‫ﻠﻮ‬
point to the next node. This is shown in figure 4.23.

‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
while (nodePtr != Null && [Link] < num)

‫ﻔﺮ‬
‫ﺍﻟ‬
previousNode == NodePtr

‫ﺸﻴ‬
-‫ﺦ‬
NodePtr == [Link]

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
nodePtr
1-

‫ﻟﻺ‬
7.9
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

head
2.5 12.6 Null

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
previousNode
‫ﻟ‬‫ﺎﺍ‬
10.5
‫ﻤﻌ‬
???
‫ﻠﻮ‬

newNode
‫ﻣﺎ‬
‫ﺕ‬

Figure: 4.23
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

Once again, the loop performs its test. Since nodePtr is not NULL and
-‫ﺦ‬

[Link] is less than num, the loop will iterate a second time. During
76
42

the second iteration, both previousNode and nodePtr are advanced by one
-2

in the list. This is shown in Figure 4.24.


8
1
1-
-1
25
20

91
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
nodePtr

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
7.9

‫ﻮ‬
‫ﺟﻴﺎ‬
head 2.5 12.6 Null

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
previousNode 10.5

‫ﺑﻜ‬
???

‫ﻔﺮ‬
newNode

‫ﺍﻟ‬
‫ﺸﻴ‬
Figure. 4.24

-‫ﺦ‬
76

‫ﻤﻌ‬
This time, the loop's test will fail because nodePtr is not less than num.
42

‫ﻬﺪ‬
-2

The statement after the loop will execute, which cause [Link]

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
to point to newNode, and [Link] to point to nodePtr. This is
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
illustrated in Figure 4.25.

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
[Link] == newNode

‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
[Link] == nodePtr
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

nodePtr
‫ﺑﻜ‬
‫ﻔﺮ‬

7.9
‫ﺍﻟ‬
‫ﺸﻴ‬

head 2.5 12.6 Null


-‫ﺦ‬
76
42

previousNode
-2

10.5
8

newNode
1
1-

Figure 4.25
-1
25
20

This leaves the list in its final state. If you will follow the links, from
head pointer to the NULL, you will see that the nodes are stored in the
order of their value members. Here is the associated C++ code.

92
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
void FloatList::insertNode(float num)

‫ﺩﺍ‬
‫ﺭﺓ‬
{

‫ﻭ‬
ListNode *newNode, *nodePtr, *previousNode;

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
// Allocate a new node & store Num
newNode = new ListNode;

‫ﺍﻟ‬
‫ﻤﻌ‬
newNode->value = num;

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
// If there are no nodes in the list

‫ﺑﻜ‬
// make newNode the first node

‫ﻔﺮ‬
if (!head)

‫ﺍﻟ‬
‫ﺸﻴ‬
{

-‫ﺦ‬
head = newNode;

76
newNode->next = NULL;

‫ﻤﻌ‬
42

‫ﻬﺪ‬
}
-2

‫ﺍﻟ‬
else // Otherwise, insert newNode.

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
// Initialize nodePtr to head of list
-1

‫ﺩﺍ‬
nodePtr = head;

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
// Skip all nodes whose value member is less

‫ﻨﻮ‬
‫ﻟﻮ‬
// than num.

‫ﺟﻴ‬
while (nodePtr != NULL && nodePtr->value < num) ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
{
‫ﻠﻮ‬

previousNode = nodePtr;
‫ﻣﺎ‬
‫ﺕ‬

nodePtr = nodePtr->next;
‫ﺑﻜ‬

}
‫ﻔﺮ‬

// If the new mode is to be the 1st in the list,


‫ﺍﻟ‬
‫ﺸﻴ‬

// insert it before all other nodes.


-‫ﺦ‬

if (previousNode == NULL)
76

{
42
-2

head = newNode;
newNode-> = nodePtr;
18

}
1-
-1

else
25

{
20

previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}

93
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
[Link] Deleting a Node

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
Deleting a node from a linked list requires two steps:

‫ﻨ‬
‫ﻮ‬‫ﻮﻟ‬
‫ﺟﻴﺎ‬
• Remove the node from the list without breaking the links created

‫ﺍﻟ‬
‫ﻤﻌ‬
by the next pointers

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
• Deleting the node from memory

‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
The deleteNode member function searches for a node containing a

-‫ﺦ‬
particular value and deletes it from the list. It uses an algorithm similar to
76

‫ﻤﻌ‬
the insertNode function. Two node pointers, nodePtr and previousNode,
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
are used to traverse the list. previousNode always points to the node

‫ﻌﺎﻟ‬
18

‫ﻰ‬
whose position is just before the one pointed to by nodePtr. When nodePtr
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
points to the node that is to be deleted, [Link] is made to point

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
to [Link]. This removes the node pointed to by nodePtr from the list.

‫ﻨﻮ‬
‫ﻟﻮ‬
The final step performed by this function is to free the memory used by

‫ﺟﻴ‬
‫ﺎﺍ‬
the node with the delete operator. There are two types of removing a node
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

from a linked list:


‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬

1. The node is first node in the list :


‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

For example, we will illustrate how deleteNode (2.5) from the


-‫ﺦ‬

following list:
76
42
-2
1 8

head
1-

2.5 7.9 12.6 Null


-1

***
25

Figure 4.26
20

If ([Link] == Num)
NodePtr = [Link]
Delete head

94
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Head = nodePtr

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
head 2.5

‫ﻮ‬
7.9 12.6 Null

‫ﺟﻴﺎ‬
***

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
nodePtr

‫ﺕ‬
Figure 4.27

‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
head
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
7.9 12.6 Null
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
Figure 4.28

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
2. The node is in the middle of the list:

‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬

To illustrate how deleteNode(7.9) works, we will step through


‫ﻠﻮ‬
‫ﻣﺎ‬

the second type of deleting node from a list, which deletes a node
‫ﺕ‬
‫ﺑﻜ‬

in the middle of the list.


‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

- First, Initialize nodePtr to head of list as shown in figure 4.29


-‫ﺦ‬

nodePtr=head
76
42
-2
8
1
1-

7.9
-1
25

head ****
2.5 12.6 Null
20

nodePtr

Figure 4.29

95
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
while (nodePtr != Null && [Link]!= num)

‫ﺩﺍ‬
‫ﺭﺓ‬
previousNode == NodePtr

‫ﻭ‬
‫ﻨ‬‫ﺗﻜ‬
‫ﻮﻟ‬
nodePtr == [Link]

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
nodePtr

‫ﻠﻮ‬
‫ﻣﺎ‬
7.9

‫ﺕ‬
****

‫ﺑﻜ‬
head 2.5 12.6 Null

‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
previousNode

76

‫ﻤﻌ‬
42 Figure 4.30

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
- Next, the following statement executes:
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
previousNode =[Link]

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
The statement above causes the links in the list to bypass

‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
the node that nodePtr points to. Although the node sill in
‫ﻟ‬‫ﺎﺍ‬
memory, this removes it from the list, as illustrated in figure
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬

4.31.
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

nodePtr
-‫ﺦ‬

7.9
76

head ****
2.5 12.6 Null
42
-2
18
1-

previousNode
-1
25

Figure 4.31
20

- The last statement uses the delete operator to complete the


total deletion of the node.

Delete nodePtr
96
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
The code c++ code is :

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
void FloatList::deleteNode(float num)

‫ﺗﻜ‬
‫ﻨ‬
{

‫ﻮﻟ‬
‫ﻮ‬
ListNode *nodePtr, *previousNode;

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
// If the list is empty, do nothing.

‫ﻠﻮ‬
if (!head)

‫ﻣﺎ‬
‫ﺕ‬
return;

‫ﺑﻜ‬
‫ﻔﺮ‬
// Determine if the first node is the one.

‫ﺍﻟ‬
‫ﺸﻴ‬
if (head->value == num)

-‫ﺦ‬
{

76
nodePtr = head->next;

‫ﻤﻌ‬
42
delete head;

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
head = nodePtr;

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

else

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
// Initialize nodePtr to head of list

‫ﻨﻮ‬
nodePtr = head;

‫ﻟﻮ‬
‫ﺟﻴ‬
// Skip all nodes whose value member is
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

// not equal to num.


‫ﻠﻮ‬
‫ﻣﺎ‬

while (nodePtr != NULL && nodePtr->value != num)


‫ﺕ‬

{
‫ﺑﻜ‬

previousNode = nodePtr;
‫ﻔﺮ‬

nodePtr = nodePtr->next;
‫ﺍﻟ‬
‫ﺸﻴ‬

}
-‫ﺦ‬
76

// Link the previous node to the node after


42

// nodePtr, then delete nodePtr.


-2

previousNode->next = nodePtr->next;
8

delete nodePtr;
1
1-

}
-1

}
25
20

97
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
[Q4.1] Review Question

‫ﺩﺍ‬
‫ﺭﺓ‬
Q1- Explain the different way to store a collection of related data?

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
Q2- What is the main disadvantages of an array?

‫ﻮ‬
‫ﺟﻴﺎ‬
Q3- What is the main advantages of a linked list over an array?

‫ﺍﻟ‬
‫ﻤﻌ‬
Q4- Discuss what is meant by linked list?

‫ﻠﻮ‬
‫ﻣﺎ‬
[Q4.2] Review Question

‫ﺕ‬
‫ﺑﻜ‬
Q1- Identify true of false and rewrite the correct version of false one:

‫ﻔﺮ‬
‫ﺍﻟ‬
A. An array is a data structure all elements are located in contiguous

‫ﺸﻴ‬
-‫ﺦ‬
memory.

76

‫ﻤﻌ‬
B. The access time for any array element is constant.
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
C. In single dimension array, a subscript can identify the number of

‫ﻌﺎﻟ‬
18

‫ﻰ‬
individual element in it.
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
Q2- what happens if I use:

‫ﺭﺓ‬
25

‫ﻭ‬
20

• a subscript on an array that is larger than the number of elements in

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
the array ?

‫ﺟﻴ‬
• an array without initializing it ?
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

Q3- How many elements are in the following array?


‫ﺑﻜ‬
‫ﻔﺮ‬

- A[6][4]
‫ﺍﻟ‬
‫ﺸﻴ‬

- A[8]
-‫ﺦ‬
76

Q4- write the pseudocode of array insertion operation


42

Q5- write the pseudocode of traverse array operation


-2

Q6- write the pseudocode of delete operation in array


18
1-

Q7- write the pseudocode of the search operation in array


-1
25
20

Q8-For the following declaration:


Y[3][4]={3,5,3,4,0,14,8,6,7,11,12,08}
What is the value of ?
- Y[2][0],
98
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
- Y[0][3],

‫ﺩﺍ‬
‫ﺭﺓ‬
- Y[2][2],

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
- Y[1][3]

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
Q9- For the following declaration:

‫ﻠﻮ‬
‫ﻣﺎ‬
x[]={30,53,40,48,67,108}

‫ﺕ‬
‫ﺑﻜ‬
What is the value of ?

‫ﻔﺮ‬
‫ﺍﻟ‬
- x[2],

‫ﺸﻴ‬
-‫ﺦ‬
- x[5].

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
[Q4.3] Review Question

‫ﻌﺎﻟ‬
18

‫ﻰ‬
Q1- Describe the two parts of node.
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
Q2- What is a list head?

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
Q3- What signifies the end of a linked list?

‫ﻨﻮ‬
‫ﻟﻮ‬
Q4- Write the pseudocode of algorithm that appends a node to the linked

‫ﺟﻴ‬
list.
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

Q5- Write the pseudocode of algorithm that inserts a node to the linked
‫ﻣﺎ‬
‫ﺕ‬

list.
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

Q6- Trace the following operations of the linked list:


-‫ﺦ‬

• appendNode(2)
76
42

• appendNode(7)
-2

• appendNode(12)
18
1-

• insertNode(10)
-1
25
20

Q7- what is the difference between appending a node to a list and inserting
a note to a list?

Q8- Complete the following sentences:


99
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 4 Arrays and linked list

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
A. The ‫ ــــــــــــــــــــــ‬points to the first node in a linked list.

‫ﺩﺍ‬
‫ﺭﺓ‬
B. A data structure that points to an object of the same type as itself is

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
known as a ‫ ــــــــــــــــــــــ‬data structure.

‫ﻮ‬
‫ﺟﻴﺎ‬
C. After creating a linked list's head pointer, you should make sure it

‫ﺍﻟ‬
‫ﻤﻌ‬
points to‫ ــــــــــــــــــــــ‬before using it in any operations.

‫ﻠﻮ‬
‫ﻣﺎ‬
D. ‫ ــــــــــــــــــــــ‬a node means adding it to the end of a list.

‫ﺕ‬
‫ﺑﻜ‬
E. ‫ ــــــــــــــــــــــ‬a node means adding it to a list, but not necessarily to

‫ﻔﺮ‬
‫ﺍﻟ‬
the end.

‫ﺸﻴ‬
-‫ﺦ‬
F. ‫ ــــــــــــــــــــــ‬a list traveling through the list.

76

‫ﻤﻌ‬
G. In a ‫ ــــــــــــــــــــــ‬list, the last node has a pointer to the first node.
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
H. In a ‫ ــــــــــــــــــــــ‬list, each node has a pointer to the one before it and

‫ﻌﺎﻟ‬
18

‫ﻰ‬
the one after it.
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
Q9- True or False:

‫ﻨﻮ‬
‫ﻟﻮ‬
1. The programmer must know in advance how many nodes will be

‫ﺟﻴ‬
needed in a linked list.
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

2. It is not necessary for each node in a lined list to have a self-


‫ﻣﺎ‬
‫ﺕ‬

referential pointer.
‫ﺑﻜ‬
‫ﻔﺮ‬

3. The nodes in a linked list may be scattered around various parts of


‫ﺍﻟ‬
‫ﺸﻴ‬

memory.
-‫ﺦ‬

4. When the head pointer points to Null, it signifies an empty list.


76
42

5. Deleting a node in a linked list is a simple matter of using the delete


-2

operator to free the node's memory.


18
1-
-1
25
20

100
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Chapter 5

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
Stack

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
1.5 Introduction

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
In this chapter we consider new abstract data types (Stacks). Such data

‫ﺑﻜ‬
‫ﻔﺮ‬
type can be viewed as a pile of items. In this case of a stack, items are

‫ﺍﻟ‬
‫ﺸﻴ‬
added to and removed from the top of the pile. Consider the pile of papers

-‫ﺦ‬
on your desk. Suppose you add papers only to the top of the pile or
76

‫ﻤﻌ‬
42
remove them only from the top of the pile. At any point in time, the only

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
paper that is visible is the one on the top. What you have is a stack. Now

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

suppose your boss comes along and asks you to immediately complete a

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

form. You stop doing whatever it is you are doing, and place the form on

‫ﻭ‬
20

‫ﺗﻜ‬
top of your pile of papers. When you have filled-out the form, you remove

‫ﻨﻮ‬
‫ﻟﻮ‬
it from the top of the stack and return to task you were working on before
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
your boss interrupted you. This example illustrates that a stack can be used
‫ﻤﻌ‬
‫ﻠﻮ‬

to keep track of partially completed tasks. In this chapter full detail about
‫ﻣﺎ‬
‫ﺕ‬

the stack, Stack ADT, Stack operations are explained in detail by


‫ﺑﻜ‬
‫ﻔﺮ‬

examples.
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

5.2 Definition
76
42

Like an array or a linked list, a stack is a data structure that holds a


-2
8

sequence of elements. Unlike arrays and lists, however, stacks are last-in-
1
1-

first–out (LIFO) structures. This means that when a program retrieves


-1
25

elements from a stack. The last element inserted into the stack is the first
20

one retrieved (and likewise, the first element inserted is the last one
retrieved).

101
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
When visualizing the way a stack works, think of a stack of plates at the

‫ﺩﺍ‬
‫ﺭﺓ‬
beginning of a cafeteria line. When a cafeteria worker replenishes the

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
supply of plates, the first one he or she puts on the stack is the last one

‫ﻮ‬
‫ﺟﻴﺎ‬
taken off. This is illustrated in figure 5.1.

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Figure 5.1

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
5.3 Logical Level

‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﺎﺍ‬
Consider the items pictured in Figure 5.1. Although the objects are all
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

different, each illustrates a common concept the stack. At the logical level,
‫ﻣﺎ‬
‫ﺕ‬

a stack is an ordered group of homogeneous items or elements. The


‫ﺑﻜ‬
‫ﻔﺮ‬

removal of existing items and the addition of new ones can take place only
‫ﺍﻟ‬
‫ﺸﻴ‬

at the top of the stack.


-‫ﺦ‬
76

For instance, if your favorite blue short is underneath a faded, old, red
42
-2

one in a stack of shirts, you must first remove the red shirt (the top item)
18

form the stack. Only then can you remove the desired blue shirt, which is
1-
-1

now the top item in the stack. The red shirt may then be replaced on the
25
20

top of the stack or thrown away.

The stack may be considered an "ordered" group of items because


elements occur in a sequence according to how long they've been in the
stack.
102
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
The items that have been in the stack the longest are at the bottom, the

‫ﺩﺍ‬
‫ﺭﺓ‬
most recent are at the top. At any time, given any two elements in a stack,

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
one is higher than the other. (For instance, the red shirt was higher in the

‫ﻮ‬
‫ﺟﻴﺎ‬
stack than the blue shirt).

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
Because items are added and removed only from the top of the stack,

‫ﻣﺎ‬
‫ﺕ‬
the last element to be added is the first to be removed. There is a handy

‫ﺑﻜ‬
‫ﻔﺮ‬
mnemonic to help you remember this rule of stack behavior: a stack is a

‫ﺍﻟ‬
‫ﺸﻴ‬
LIFO
Stack (Last In, First
data Out)
type instructure.

-‫ﺦ‬
an abstract which elements are added and removed from only one
end, a "last in, first out" structure
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
The accessing protocol for a stack is summarized as follows: Both to
18

‫ﻰ‬
1-

‫ﻟﻺ‬
retrieve elements and to store new elements, access only the top of the
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

stack.

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
5.4 Stack Data Structure

‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
To get a feel for what a stack is, and why it might be useful, suppose
‫ﻠﻮ‬
‫ﻣﺎ‬

you wanted to design an algorithm for reversing a string of characters. If


‫ﺕ‬
‫ﺑﻜ‬

the characters are stored in an array, the simplest way to reverse them
‫ﻔﺮ‬
‫ﺍﻟ‬

might be to locate the end of the string and just list the characters from
‫ﺸﻴ‬
-‫ﺦ‬

that point back to the beginning of the array, using a simole for loop.
76

However, suppose you don’t know in advance how many characters there
42
-2

are in the string. This might be the case if you are reading the characters
1 8

in, one at a time, from the keyboard or a file. One particularly simple
1-
-1

method of reversing such a string might be the following:


25
20

1. Read in a character.

2. Place the character ‘on top’ of any previously read characters.

103
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
3. Repeat steps 1 and 2 until no more characters are presented for

‫ﺩﺍ‬
‫ﺭﺓ‬
reading.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
4. Remove the top character from the pile you created in the first part of

‫ﻮﻟ‬
‫ﻮ‬
the algorithm.

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
5. Print the character to the right of any previously printed characters.

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
6. Repeat steps 4 and 5 until all the characters have been printed out

‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
If you try this algorithm for a string of your own devising, you will see

-‫ﺦ‬
76
that it produces as its output the original string in reverse order. The pile

‫ﻤﻌ‬
42

‫ﻬﺪ‬
of characters that you created by repeated application of steps 1 and 2 is an
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

example of a stack.

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
The main features of a stack are:
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
• Access is allowed at one end (the top of the stack) only. Data are

‫ﻟﻮ‬
‫ﺟﻴ‬
added and removed at this end. ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

• The action of adding a data element to a stack is called pushing.


‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

• The action of removing a data element from a stack is called


‫ﺑﻜ‬
‫ﻔﺮ‬

popping.
‫ﺍﻟ‬
‫ﺸﻴ‬

• Stacks handle data in a last-in-first-out, or LIFO, fashion: the last


-‫ﺦ‬

item pushed onto the stack is always the first item popped off the
76
42

stack
-2
18

5.5 Implementing a stack


1-
-1
25

If we wanted to implement stacks directly from their definition, we


20

might try the following implementation in terms of arrays. First, we


reserve some specific data value (such as ASCII 0, if we are storing
characters on a stack) which we know will never form part of the valid

104
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
data that are to be stored on the stack. We fill the array with this value to

‫ﺩﺍ‬
‫ﺭﺓ‬
indicate an empty stack.

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
To push an item onto an empty stack, we insert it at array location 0. To

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
push the next item onto the stack, we must first move the item at location

‫ﻤﻌ‬
‫ﻠﻮ‬
0 over to location 1 to make room for the new item, which is placed at

‫ﻣﺎ‬
‫ﺕ‬
location 0. We can continue pushing items onto the stack in this fashion:

‫ﺑﻜ‬
‫ﻔﺮ‬
first move all previously added items over one location to make room for

‫ﺍﻟ‬
‫ﺸﻴ‬
the new item, then store the new item at location 0.

-‫ﺦ‬
76

‫ﻤﻌ‬
42
Popping data from the stack works just like pushing in reverse. We

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
remove (or read) the item at location 0, test to see if it is the special

‫ﻌﺎﻟ‬
18

‫ﻰ‬
character indicating an empty stack and, if not, move over all the other
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

items in the stack to fill in the gap left by removing the top element. We

‫ﻭ‬
20

‫ﺗﻜ‬
can continue popping data from the stack until we encounter the special

‫ﻨﻮ‬
‫ﻟﻮ‬
character indicating that all the data have been removed from the stack.
‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬

While this method of doing things follows the definition of a stack very
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

closely, it is also highly inefficient. Every time we push data onto or pop
‫ﺑﻜ‬
‫ﻔﺮ‬

data from the stack, we must move all the other elements in the stack to
‫ﺍﻟ‬
‫ﺸﻴ‬

compensate. If the type of data we are storing is complex, or if there are a


-‫ﺦ‬

lot of data on the stack (or both), this can be very costly in terms of
76
42

computing time. It turns out that there is a much more efficient way of
-2

doing things.
8
1
1-
-1

Rather than defining the top of the stack to be at location 0 in the array,
25

we will define the bottom of the stack at that location. The stack will then
20

grow into the array, so that the location of its top (in terms of the array
index) depends on the amount of data in the stack. The first item added to

105
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
an empty stack is therefore added at location 0, the next at location 1, the

‫ﺩﺍ‬
‫ﺭﺓ‬
next at location 2, and so on.

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮ‬ ‫ﻮﻟ‬
Doing things this way obviously requires that we keep track of where

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
the top of the stack is after each push and pop. This is easily done by

‫ﻤﻌ‬
‫ﻠﻮ‬
defining an auxiliary integer variable known as the stack pointer, which

‫ﻣﺎ‬
‫ﺕ‬
stores the array index currently being used as the top of the stack.

‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
In an array with StackSize elements, the array locations are indexed

-‫ﺦ‬
from 0 to StackSize - 1. A full stack will therefore have its stack pointer
76

‫ﻤﻌ‬
42
pointing to location StackSize - 1, and an empty stack will have a stack

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
pointer of -1.

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
5.6 Applications of stacks

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
Stacks are useful data structures for algorithms that work first with the

‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
last saved elements of a series. For example, computer systems use stacks
‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬
while executing programs. When a function is called, they save the
‫ﻠﻮ‬
‫ﻣﺎ‬

program's return address on a stack. They also create local variables on a


‫ﺕ‬
‫ﺑﻜ‬

stack. When the function terminates, the local variables are removed from
‫ﻔﺮ‬

the stack and the return address is retrieved. Also, some calculators use a
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

stack for performing mathematical operations.


76
42

5.7 Static and Dynamic Stacks.


-2
1 8

There are two types of stack data structure: static and dynamic. Static
1-
-1

stacks have fixed size, and are implemented as arrays. Dynamic stacks
25
20

grow in size as needed, and are implemented as lined lists. A linked list-
based stack offers two advantages over an array-based stack. First, there is
no need to specify the starting size of the stack. A dynamic stack simply
starts as an empty linked list, and then expands by one node each time a

106
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
value is pushed. Second, a dynamic stack will never be full, as long as the

‫ﺩﺍ‬
‫ﺭﺓ‬
system has enough free memory.

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮ‬‫ﻮﻟ‬
5.8 Operations on Stacks

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
The logical picture of the structure is only half the definition of an

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
abstract data type. The other half is a set of operations that allows the user

‫ﺑﻜ‬
‫ﻔﺮ‬
to access and manipulate the elements stored in the structure. Given the

‫ﺍﻟ‬
‫ﺸﻴ‬
logical view of a stack, what kinds of operations do we need in order to

-‫ﺦ‬
use a stack?
76

‫ﻤﻌ‬
42
The operation that adds an element to the top of a stack is usually called

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
push, and the operation that removes the top element off the stack is

‫ﻌﺎﻟ‬
18

‫ﻰ‬
referred to as pop. The following algorithms for pushing and popping data:
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
The algorithm of push operation:

‫ﻟﻮ‬
‫ﺟﻴ‬
1. If stack is not full then: ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
2. Add 1 to the stack pointer,
‫ﻠﻮ‬

3. Store item at stack pointer location.


‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

The algorithm of pop operation:


-‫ﺦ‬

1. If stack is not empty then:


76

2. Read item at stack pointer location,


42
-2

3. Subtract 1 from the stack pointer.


1 8
1-
-1

A stack has two primary operations: push and pop. The push operation
25
20

causes a value to be stored, or pushed onto the stack. For example,


suppose we have an empty integer stack that is capable of holding a
maximum of three values. With that stack we execute the following
push operations.

107
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Push (5),

‫ﺩﺍ‬
‫ﺭﺓ‬
Push (10),

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
Push (15).

‫ﻮ‬
‫ﺟﻴﺎ‬
Figure 5.2 illustrates the state of the stack after each of the push

‫ﺍﻟ‬
‫ﻤﻌ‬
operations listed above.

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
Push (5)

‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
5

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Push (10) 10

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
5

‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬

15
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

Push (15) 10
76
42

5
-2
18
1-

Figure 5.2
-1
25

The pop operation retrieves (and hence, removes) a value from the
20

stack. Suppose we execute three consecutive pop operations on the


stack shown in the illustration above. Figure 5.3 depicts the results.

108
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
15

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮ‬ ‫ﻮﻟ‬
‫ﺟﻴﺎ‬
Pop ()

‫ﺍﻟ‬
10

‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
5

‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
10

-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
Pop ()
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
5
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
5

‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

Pop ()
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

Figure 5.3
-‫ﺦ‬
76

As you can see from the illustration, the last pop operation leaves the
42

stack empty. For a static stack (one with a fixed size), we will need a
-2

Boolean isFull operation. The isFull operation return true if the stack is
18
1-

full, and false otherwise. This operation is necessary to prevent a stack


-1
25

overflow in the event a push operation is attempted when all the stack's
20

elements have values stored in them.


For both static and dynamic stacks we will need a boolean isEmpty
operation. The isEmpty operation return true when the stack is empty, and

109
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
false otherwise. This prevents an error from occurring when a pop

‫ﺩﺍ‬
‫ﺭﺓ‬
operation is attempted on an empty stack.

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮ‬ ‫ﻮﻟ‬
‫ﺟﻴﺎ‬
Example: Execute the following operations:

‫ﺍﻟ‬
‫ﻤﻌ‬
Push(8),

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
Push(7) ,

‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
Pop(8) ,

‫ﺸﻴ‬
-‫ﺦ‬
Push(19),
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
Push(21) ,
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
pop().
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Push (8)

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
8
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬

Push ()
‫ﻔﺮ‬

7
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

8
76
42
-2

Pop ()
18
1-

8
-1
25
20

110
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
Push (19)

‫ﻮ‬
19

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
8

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
21

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
Push (21)
19

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
8
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
Pop ()

‫ﻟﻮ‬
19

‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
8
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬

Figure 5.4
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬

5.9 Summary of Stack Concepts:


‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

This simplest of all the containers is a stack. A stack is a container


76
42

which provides exactly one function, Push, for putting objects into the
-2

container, and one function, Pop, for tacking objects out of the container.
18

Figure 5.5 illustrates the basic idea.


1-
-1
25

Objects which are stored in stack are kept in a pile. The last item put
20

into the stack is at the top. When an item is pushed into a stack, it is placed
at the top of the pile. When an item popped, it is always the top item
which is removed. Since it is always the last item to be put into the stack

111
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
that is the first item to be removed, a stack is a last-in, first-out or LIFO

‫ﺩﺍ‬
‫ﺭﺓ‬
data structure.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
Figure 5.5
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

The code C++ for the class:


‫ﺑﻜ‬
‫ﻔﺮ‬

Contents of IntStack. h
‫ﺍﻟ‬
‫ﺸﻴ‬

1 class IntStack
-‫ﺦ‬

2{
76

3 private:
42

4 int *stackArray;
-2

5 int capacity;
8

6 int top;
1
1-

7 public:
-1

8 IntStack(int capacity); //Constructor


25

9 -IntStack() {delete[] stackArray; }


20

10 void push(int value);


11 void pop(int &value);
12 bool isEmpty() canst;
13 };

112
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
The IntStack constructor allocates an array of a specified capacity and

‫ﺩﺍ‬
‫ﺭﺓ‬
sets the member variable top to 0. All stack functions use top in such a

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
way that it always points to the next available slot in the stack's array.

‫ﻮ‬
‫ﺟﻴﺎ‬
When top equals capacity, there are no more slots available to store

‫ﺍﻟ‬
‫ﻤﻌ‬
values, and the next call to push throws an exception. Likewise, when top

‫ﻠﻮ‬
‫ﻣﺎ‬
is zero, the stack is empty and a call to pop throws an exception. Because

‫ﺕ‬
‫ﺑﻜ‬
there are no provisions in the program to catch either exception, the

‫ﻔﺮ‬
‫ﺍﻟ‬
occurrence of either one will terminate the program with an error

‫ﺸﻴ‬
-‫ﺦ‬
message. Notice that push increments top after adding a value to the stack,

76

‫ﻤﻌ‬
and pop decrements top before returning the value stored at stackArray
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
[top].

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

Contents of IntStack. cpp

‫ﺩﺍ‬
‫ﺭﺓ‬
25

1 #include ''intstack.h''

‫ﻭ‬
20

‫ﺗﻜ‬
2 II************************************

‫ﻨﻮ‬
3 II Constructor *

‫ﻟﻮ‬
‫ﺟﻴ‬
4 II************************************
5 IntStack::IntStack(int capacity)
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

6{
‫ﻠﻮ‬
‫ﻣﺎ‬

7 this->capacity = capacity;
‫ﺕ‬

8 stackArray = new int[capacity];


‫ﺑﻜ‬

9 top = O;
‫ﻔﺮ‬

10 }
‫ﺍﻟ‬
‫ﺸﻴ‬

11
-‫ﺦ‬

12 II***********************************
76

13 II Adds a value to the stack *


42

14 II***********************************
-2

15 void IntStack::push(int value)


8

16 {
1
1-

17 if (top == capacity) throw IntStack::Overflow();


-1

18 stackArray[top] = value;
25

19 top++;
20

20 }
21
22 II****************************************
23 II Determines whether the stack is empty *
24 II****************************************

113
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
25 bool IntStack::isEmpty() canst

‫ﺩﺍ‬
‫ﺭﺓ‬
26 {

‫ﻭ‬
27 if (top == 0)

‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
28 return true;

‫ﻮ‬
‫ﺟﻴﺎ‬
29 else
30 return false;

‫ﺍﻟ‬
‫ﻤﻌ‬
31 }

‫ﻠﻮ‬
‫ﻣﺎ‬
32

‫ﺕ‬
33 II************************************************

‫ﺑﻜ‬
34 II Removes a value from the stack and returns it *

‫ﻔﺮ‬
35 II************************************************

‫ﺍﻟ‬
‫ﺸﻴ‬
36 void IntStack::pop(int &value)

-‫ﺦ‬
37 {

76
38 if (isEmpty()) throw IntStack::Underflow();

‫ﻤﻌ‬
42

‫ﻬﺪ‬
39 top --;
-2

‫ﺍﻟ‬
40 value = stackArray[top];

‫ﻌﺎﻟ‬
18

41 }

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Program 5.1 IntStack class

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
1 // This program illustrates the IntStack class.

‫ﺟﻴ‬
2 #include ''intstack.h '' ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
3 #include <iostream>
‫ﻠﻮ‬

4 using namespace std;


‫ﻣﺎ‬
‫ﺕ‬

5 int main()
‫ﺑﻜ‬

6{
‫ﻔﺮ‬

7 IntStack stack(5);
‫ﺍﻟ‬
‫ﺸﻴ‬

8 int values[] = {5, 10, 15, 20, 25};


-‫ﺦ‬

9 int value;
10
76

11 cout << ''Pushing ... \n '';


42

12 for (int k = O; k < 5; k++)


-2

13 {
8
1

14
1-
-1

15
25

cout << values [ k] << ''


20

[Link](values[k]);
16 }
1 7 cout << '' \nPopping ... \n '';
18 while (![Link]())
19 {
20 [Link](value);
114
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫‪Chapter 5‬‬ ‫‪Stack‬‬

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫‪21 cout << value << '' '' .‬‬

‫ﺩﺍ‬
‫ﺭﺓ‬
‫} ‪22‬‬

‫ﻭ‬
‫;‪23 cout << endl‬‬

‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
‫;‪24 return O‬‬

‫ﻮ‬
‫ﺟﻴﺎ‬
‫} ‪25‬‬

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫‪Program Output‬‬

‫ﺕ‬
‫ﺑﻜ‬
‫‪Pushing ...‬‬

‫ﻔﺮ‬
‫‪5 10 15 20 25‬‬

‫ﺍﻟ‬
‫ﺸﻴ‬
‫ﺦ‪-‬‬
‫‪Popping ...‬‬

‫‪76‬‬
‫‪25 20 15 10‬‬

‫ﻤﻌ‬
‫‪42‬‬

‫ﻬﺪ‬
‫‪-2‬‬

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
‫‪18‬‬

‫ﻰ‬
‫‪1-‬‬

‫ﻟﻺ‬
‫‪-1‬‬

‫ﺩﺍ‬
‫ﺭﺓ‬
‫‪25‬‬

‫ﻭ‬
‫‪20‬‬

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
‫ﺦ‪-‬‬
‫‪76‬‬
‫‪42‬‬
‫‪-2‬‬
‫‪1‬‬‫‪8‬‬
‫‪1-‬‬
‫‪-1‬‬
‫‪25‬‬
‫‪20‬‬

‫‪115‬‬
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
[Q3] Review Question

‫ﺩﺍ‬
‫ﺭﺓ‬
Q1- Describe what LIFO means.

‫ﻭ‬
‫ﻨ‬‫ﺗﻜ‬
‫ﻮﻟ‬
Q2- what are the two types of the stack data structure?

‫ﻮ‬
‫ﺟﻴﺎ‬
Q3- What is the difference between static and dynamic stacks? What

‫ﺍﻟ‬
‫ﻤﻌ‬
advantages do dynamic stacks have over static stacks?

‫ﻠﻮ‬
‫ﻣﺎ‬
Q4- Write the algorithms for pushing and popping data operations in

‫ﺕ‬
‫ﺑﻜ‬
stack.

‫ﻔﺮ‬
‫ﺍﻟ‬
Q5- Identify true of false and rewrite the correct version of false one:

‫ﺸﻴ‬
-‫ﺦ‬
A. A stack like lists and arrays are last-in-first-out(LIFO) structures.

76

‫ﻤﻌ‬
B. Elements of stacks are added and removed from only one end.
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
C. A dynamic stack is built on the linked list instead of an array.

‫ﻌﺎﻟ‬
18

‫ﻰ‬
D. Computer systems use stacks while executing programs.
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
E. The size of a dynamic stack must be known in advance.

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
F. The push operation inserts an element at the end of a stack.

‫ﻨﻮ‬
‫ﻟﻮ‬
G. The pop operation removes an element from the top of a stack.

‫ﺟﻴ‬
‫ﺎﺍ‬
Q6- Suppose the following operations were performed on an empty stack:
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

Push(3),
‫ﻣﺎ‬
‫ﺕ‬

Push(9),
‫ﺑﻜ‬
‫ﻔﺮ‬

Push(12),
‫ﺍﻟ‬
‫ﺸﻴ‬

Push(1).
-‫ﺦ‬

Insert numbers in the diagram below to show what will be stored in the
76
42

static after the operations above have executed.


-2

Top of Stack
18
1-
-1
25
20

Bottom

116
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 5 Stack

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
Q7- Suppose the following operations were performed on an empty stack.

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
Push(8),

‫ﻮ‬
‫ﺟﻴﺎ‬
Push(7),

‫ﺍﻟ‬
‫ﻤﻌ‬
Pop(),

‫ﻠﻮ‬
‫ﻣﺎ‬
Push(19),

‫ﺕ‬
‫ﺑﻜ‬
Push(21).

‫ﻔﺮ‬
‫ﺍﻟ‬
Pop().

‫ﺸﻴ‬
-‫ﺦ‬
Insert numbers in the diagram below to show what will be stored in the

76

‫ﻤﻌ‬
static stack after the operations above have executed.
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
Top of Stack
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

Bottom
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬

Q8- Trace the following operations:


‫ﺍﻟ‬
‫ﺸﻴ‬

Push(5),
-‫ﺦ‬
76

Push(10),
42

Push(15),
-2
8

Push(20),
1
1-

Pop().
-1
25
20

Q9- Given an initially empty stack which accepts single characters , The
following operations are performed:
Push(T),

117
‫‪Chapter 5‬‬

‫‪Pop().‬‬
‫‪Pop(),‬‬
‫‪Pop(),‬‬

‫‪Pop (),‬‬
‫‪Push(E),‬‬

‫‪Push(R),‬‬

‫‪Push(Y),‬‬
‫‪Push(X),‬‬

‫‪20‬‬ ‫‪20‬‬
‫‪25‬‬ ‫‪25‬‬

‫‪Trace the above operations.‬‬


‫‪-1‬‬ ‫‪-1‬‬
‫‪1-‬‬ ‫‪1-‬‬
‫‪1‬‬‫‪8‬‬ ‫‪18‬‬
‫‪-2‬‬ ‫‪-2‬‬

‫‪118‬‬
‫‪42‬‬ ‫‪42‬‬
‫‪76‬‬ ‫‪76‬‬
‫ﺦ‪-‬‬ ‫ﺦ‪-‬‬
‫ﺸﻴ‬ ‫ﺸﻴ‬
‫ﺍﻟ‬ ‫ﺍﻟ‬
‫ﻔﺮ‬ ‫ﻔﺮ‬
‫ﺑﻜ‬ ‫ﺑﻜ‬
‫ﺕ‬ ‫ﺕ‬
‫ﻣﺎ‬ ‫ﻣﺎ‬
‫ﻠﻮ‬ ‫ﻠﻮ‬
‫ﻤﻌ‬ ‫ﻤﻌ‬
‫ﻟ‬‫ﺎﺍ‬ ‫ﺍﻟ‬
‫ﺟﻴ‬ ‫ﺟﻴﺎ‬
‫ﻟﻮ‬ ‫ﻮ‬ ‫ﻮﻟ‬
‫ﻨﻮ‬ ‫ﻨ‬
‫ﺗﻜ‬ ‫ﺗﻜ‬
‫ﻭ‬ ‫ﻭ‬
‫‪Stack‬‬

‫ﺭﺓ‬ ‫ﺭﺓ‬
‫ﺩﺍ‬ ‫ﺩﺍ‬
‫ﻟﻺ‬ ‫ﻟﻺ‬
‫ﻰ‬ ‫ﻰ‬
‫ﻌﺎﻟ‬ ‫ﻌﺎﻟ‬
‫ﺍﻟ‬ ‫ﺍﻟ‬
‫ﻬﺪ‬ ‫ﻬﺪ‬
‫ﻤﻌ‬ ‫ﻤﻌ‬
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
Chapter 6

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
Queue

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
6.1 Introduction

‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
In the preceding chapter we saw that a stack comprises a pile of objects

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
that can be accessed only at one end – the top. In this chapter we examine

76

‫ﻤﻌ‬
a similar data structure called a single-ended queue.
42

‫ﻬﺪ‬
-2

Whereas in a stack we add and remove elements at the same end of the

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
pile, in a single-ended queue we add elements at one end and remove them
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
from the other. Since it is always the first item to be put into the queue that

‫ﺭﺓ‬
25

is the first item to be removed, a queue is a first-in, first-out or FIFO data

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
structure.

‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

6.2 The queue Data Structure


‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬

A queue is used in computing in much the same way as it is used in


‫ﺍﻟ‬
‫ﺸﻴ‬

everyday life: to allow a sequence of items to be processed on a first-


-‫ﺦ‬

come-first-served basis. In most computer installations, for example, one


76
42

printer is connected to several different machines, so that more than one


-2

user can submit printing jobs to the same printer. Since printing a job takes
18

much longer than the process of actually transmitting the data from the
1-
-1

computer to the printer, a queue of jobs is formed so that the jobs print out
25
20

in the same order in which they were received by the printer. This has the
irritating consequence that if your job consists of printing only a single
page while the job in front of you is printing an entire 200-page thesis, you
must still wait for the large job to finish before you can get your page.

119
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
From the point of view of data structures, a queue is similar to a stack,

‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
in that data are stored in a linear fashion, and access to the data is allowed

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
only at the ends of the queue. The actions allowed on a queue are:

‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
• creating an empty queue

‫ﺑﻜ‬
‫ﻔﺮ‬
• testing if a queue is empty

‫ﺍﻟ‬
‫ﺸﻴ‬
• adding data to the tail of the queue

-‫ﺦ‬
76
• removing data from the head of the queue

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

These operations are similar to those for a stack, except that pushing has

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

been replaced by adding an item to the tail of the queue, and popping has

‫ﺩﺍ‬
‫ﺭﺓ‬
25

been replaced by removing an item from the head of the queue. Because

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
queues process data in the same order in which they are received, a queue

‫ﻟﻮ‬
‫ﺟﻴ‬
is said to be a first-in-first-out or FIFO data structure. ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

Like a stack, a queue (pronounced "cue") is a data structure that holds a sequence of
‫ﻣﺎ‬

elements. A queue, however, provides access to its elements in first-in, first-out


‫ﺕ‬

(FIFO) order. The elements in a queue are processed like customers standing in a
‫ﺑﻜ‬
‫ﻔﺮ‬

grocery check-out line: the first customer in line is the first one served
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

6.3 Implementing a Queue


76
42
-2

Just as with stacks, queues can be implemented using arrays or lists. For
18

the present, we will consider the implementation using arrays. If we


1-
-1

attempt to follow the algorithm for adding and removing data from a
25
20

queue directly, we might try to implement a queue using an array as


follows.
Define a class containing an array for storing the queue elements, and
two markers: one pointing to the location of the head of the queue, and the

120
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
other to the first empty space following the tail. When an item is to be

‫ﻭ‬
‫ﺗﻜ‬
added to the queue, a test to see if the tail marker points to a valid location

‫ﻨ‬
‫ﻮ‬‫ﻮﻟ‬
is made, then the item is added to the queue and the tail marker is

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
incremented by 1. When an item is to be removed from the queue, a test is

‫ﻤﻌ‬
‫ﻠﻮ‬
made to see if the queue is empty and, if not, the item at the location

‫ﻣﺎ‬
‫ﺕ‬
pointed to by the head marker is retrieved and the head marker is

‫ﺑﻜ‬
‫ﻔﺮ‬
incremented by 1.

‫ﺍﻟ‬
‫ﺸﻴ‬
This procedure works well until the first time when the tail marker

-‫ﺦ‬
76
reaches the end of the array. If some removals have occurred during this

‫ﻤﻌ‬
42

‫ﻬﺪ‬
time, there will be empty space at the beginning of the array. However,
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
because the tail marker points to the end of the array, the queue is thought
18

‫ﻰ‬
1-

‫ﻟﻺ‬
to be ‘full’ and no more data can be added. We could shift the data so that
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

the head of the queue returns to the beginning of the array each time this

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
happens, but shifting data is costly in terms of computer time, especially if

‫ﻟﻮ‬
‫ﺟﻴ‬
the data being stored in the array consist of large data objects.
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬

A more efficient way of storing a queue in an array is to ‘wrap around’


‫ﻠﻮ‬
‫ﻣﺎ‬

the end of the array so that it joins the front of the array. Such a circular
‫ﺕ‬
‫ﺑﻜ‬

array allows the entire array (well, almost, as we’ll see in a bit) to be used
‫ﻔﺮ‬
‫ﺍﻟ‬

for storing queue elements without ever requiring any data to be shifted. A
‫ﺸﻴ‬
-‫ﺦ‬

circular array with QSIZE elements (numbered from 0 to QSIZE - 1) may


76

be visualized as shown in Figure. 6.1.


42
-2
18
1-
-1
25
20

Figure 6.1 A circular array.

121
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
The array is, of course, stored in the normal way in memory, as a linear

‫ﻭ‬
‫ﺗﻜ‬
block of QSIZE elements. The circular diagram is just a convenient way

‫ﻨ‬
‫ﻮ‬ ‫ﻮﻟ‬
of representing the data structure.

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
We will need head and tail markers to indicate the location of the head

‫ﻤﻌ‬
‫ﻠﻮ‬
and the location just after the tail where the next item should be added to

‫ﻣﺎ‬
‫ﺕ‬
the queue, respectively. An empty queue is denoted by the condition head

‫ﺑﻜ‬
‫ﻔﺮ‬
== tail, as shown in Figure 6.2.

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
Figure 6.2. An empty queue.

‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬

At this point, the first item of data would be added at the location
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

indicated by the tail marker, that is, at array index 0. Adding this element
‫ﺑﻜ‬
‫ﻔﺮ‬

gives us the situation shown in Figure 6.3.


‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42
-2
8
1
1-
-1
25
20

Figure 6.3 A queue containing one element.


Let us use the queue until the tail marker reaches QSIZE - 1. We will
assume that some items have been removed from the queue, so that head

122
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
has moved along as well Figure 6.4.

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
Figure 6.4
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
Now we add another element to the queue at the location marked by

‫ﻌﺎﻟ‬
18

‫ﻰ‬
tail, that is, at array index QSIZE - 1. The tail marker now advances one
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
step, which positions it at array index 0. The tail marker has wrapped

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
around the array and come back to its starting point. Since the head marker

‫ﻨﻮ‬
‫ﻟﻮ‬
has moved along, those elements at the beginning of the array from index
‫ﺟﻴ‬
‫ﺎﺍ‬
0 up to index head - 1 are available for storage. Using a circular array
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

means that we can make use of these elements without having to shift any
‫ﻣﺎ‬
‫ﺕ‬

data.
‫ﺑﻜ‬
‫ﻔﺮ‬

In a similar way, if we keep removing items from the queue, eventually


‫ﺍﻟ‬
‫ﺸﻴ‬

head will point to array index QSIZE - 1. If we remove another element,


-‫ﺦ‬
76

head will advance another step and wrap around the array, returning to
42

index 0.
-2

We have seen that the condition for an empty queue is that head == tail.
18
1-

What is the condition for a full queue? If we try to make use of all the
-1
25

array elements, then in a full queue, the tail of the queue must be the
20

element immediately prior to the head. Since we are using the tail marker
to point to the array element immediately following the tail element in the
queue, tail would have to point to the same location as head for a full
queue. But we have just seen that the condition head == tail is the
123
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
condition for an empty queue. Therefore, if we try to make use of all the

‫ﻭ‬
‫ﺗﻜ‬
array elements, the conditions for full and empty queues become identical.

‫ﻨ‬
‫ﻮ‬ ‫ﻮﻟ‬
We therefore impose the rule that we must always keep at least one free

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
space in the array, and that a queue becomes full when the tail marker

‫ﻤﻌ‬
‫ﻠﻮ‬
points to the location immediately prior to head.

‫ﻣﺎ‬
‫ﺕ‬
We may now formalize the algorithms for dealing with queues in a

‫ﺑﻜ‬
‫ﻔﺮ‬
circular array.

‫ﺍﻟ‬
‫ﺸﻴ‬
• Creating an empty queue: set head = tail = 0.

-‫ﺦ‬
76
• Testing if a queue is empty: is head == tail?

‫ﻤﻌ‬
42

‫ﻬﺪ‬
• Testing if a queue is full: is (tail + 1) % QSIZE == head?
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

• Adding an item to a queue: if queue is not full, add item at location

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

tail and set tail = (tail + 1) % QSIZE.

‫ﺩﺍ‬
‫ﺭﺓ‬
25

• Removing an item from a queue: if queue is not empty, remove item

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
from location head and set head = (head + 1) % QSIZE.

‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬

Recall that the % operator is the modulus operator. This ensures that
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

head and tail wrap around the end of the array properly. For example,
‫ﺑﻜ‬
‫ﻔﺮ‬

suppose that tail is QSIZE - 1 and we wish to add an item to the queue.
‫ﺍﻟ‬
‫ﺸﻴ‬

We add the item at location tail (assuming that the queue is not full) and
-‫ﺦ‬

then set tail = (QSIZE - 1) + 1 % QSIZE = QSIZE % QSIZE = 0.


76
42
-2

6.4 Queue Logical Level


18
1-
-1

A stack is an abstract data structure with the special property that


25
20

elements are always added to and removed from the top. We know from
experience that many collections of data elements operate in the reverse
manner: items are added at one end and removed from the other. This
structure, called a FIFO (First In, First Out) queue, has many uses in

124
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
computer programs. We consider the FIFO queue data structure at three

‫ﻭ‬
‫ﺗﻜ‬
levels: logical, implementation, and application. In the rest of this chapter,

‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
“queue” refers to a FIFO queue.

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
A queue (pronounced like the letter Q) is an ordered, homogeneous

‫ﻣﺎ‬
‫ﺕ‬
group of elements in which new elements are added at one end (the “rear”)

‫ﺑﻜ‬
‫ﻔﺮ‬
and elements are removed from the other end (the “front”). As an example

‫ﺍﻟ‬
‫ﺸﻴ‬
of a queue, consider a line of students waiting to pay for their textbooks at

-‫ﺦ‬
76
a university bookstore (see Figure 6.5). In theory, if not in practice, each

‫ﻤﻌ‬
42

‫ﻬﺪ‬
new student gets in line at the rear. When the cashier is ready to help a
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
new customer, the student at the front of the line is served.
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

To add elements to a queue, we access the rear of the queue; to remove

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
elements, we access the front of the queue. The middle elements are

‫ﻟﻮ‬
‫ﺟﻴ‬
logically inaccessible, even if we physically store the queue elements in a
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

random-access structure such as an array. It is convenient to picture the


‫ﻠﻮ‬
‫ﻣﺎ‬

queue as a linear structure with the front at one end and the rear at the
‫ﺕ‬
‫ﺑﻜ‬

other end. However, we must stress that the “ends” of the queue are
‫ﻔﺮ‬
‫ﺍﻟ‬

abstractions; they may or may not correspond to any physical


‫ﺸﻴ‬
-‫ﺦ‬

characteristics of the queue’s implementation. The essential property of


76

the queue is its FIFO access.


42
-2
8

Queue A data structure in which elements are added to the rear and removed from
1
1-

the front; a “first in, first out” (FIFO) structure.


-1
25
20

125
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
Figure 6.5 A FIFO queue
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

Like a stack, a queue is a holding structure for data that we use later.

‫ﺩﺍ‬
‫ﺭﺓ‬
25

We put a data item onto the queue; when we need it later, we remove it

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
from the queue. If we want to change the value of an element, we must

‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﺎﺍ‬
take that element off the queue, change its value, and then return it to the
‫ﻟ‬
‫ﻤﻌ‬

queue. We do not directly manipulate the values of items that are


‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

currently in the queue.


‫ﺑﻜ‬
‫ﻔﺮ‬

6.5 Application of Queues


‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

Queue data structures are commonly used in computer operating


76

systems. They are especially important in multi-user/multitasking


42
-2

environment where several or tasks may be requesting the same resource


1 8

simultaneously. Printing for example, is controlled by a queue because


1-
-1

only one document may be printed at a time. A queue is used to hold print
25
20

jobs submitted by users of the system, while the printer services those jobs
one at a time.

Communications software salon uses queues to hold information


received over networks and dial-up connections. Sometimes information is

126
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
transmitted to a system faster than it can be processed, so it is place in a

‫ﻭ‬
‫ﺗﻜ‬
queue when it is received.

‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
6.6 Static and Dynamic Queues

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
Just as stacks are implemented as arrays or linked lists, so are queues.

‫ﻣﺎ‬
‫ﺕ‬
Dynamic queues offer the same advantaged over static queues that

‫ﺑﻜ‬
‫ﻔﺮ‬
dynamic stacks over static stacks. In fact, the primary difference between

‫ﺍﻟ‬
‫ﺸﻴ‬
queues and stacks is the way data elements are accessed in each structure.

-‫ﺦ‬
76

‫ﻤﻌ‬
6.7 Operations on Queues 42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
The bookstore example suggests two operations that we can apply to a
18

‫ﻰ‬
1-

queue. First, new elements can be added to the rear of the queue, an

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

operation that we call Enqueue. We can also remove elements from the

‫ﻭ‬
20

‫ﺗﻜ‬
front of the queue, an operation that we call Dequeue. Unlike the stack

‫ﻨﻮ‬
‫ﻟﻮ‬
operations Push and Pop, the adding and removing operations on a queue
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
do not have standard names. Enqueue is sometimes called Enq, Enque,
‫ﻤﻌ‬
‫ﻠﻮ‬

Add, and Insert; Dequeue is also called Deq, Deque, Remove, and
‫ﻣﺎ‬
‫ﺕ‬

Serve.
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬

Another useful queue operation is checking whether the queue is empty.


‫ﺸﻴ‬
-‫ﺦ‬

The IsEmpty function returns true if the queue is empty and false
76

otherwise. We can apply dequeue only when the queue is not empty.
42
-2

Theoretically, we can always apply enqueue, because in principle a queue


8
1

has no limit on its size. We know from our experience with stacks,
1-
-1

however, that certain implementations (an array representation, for


25
20

instance) require that we test whether the structure is full before we add
another element. This real-world consideration applies to queues as well,
so we define an IsFull operation. We also need an operation to initialize a

127
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
queue to an empty state, which we call MakeEmpty. Figure 6.7 shows

‫ﻭ‬
‫ﺗﻜ‬
how a series of these operations would affect a queue.

‫ﻨ‬
‫ﻮ‬‫ﻮﻟ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
IsEmpty ( )==true

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
Enqueue(A)

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
Enqueue(B)

‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

Dequeue()
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42

Enqueue(C)
-2
1 8
1-
-1
25
20

MakeEmpty( )

Figure 6.6 the effects of queue operations

128
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
Just like check-out lines in a grocery store, think as having a front and a

‫ﻭ‬
‫ﺗﻜ‬
rear. This is illustrated in figure 6.7.

‫ﻨ‬
‫ﻮ‬ ‫ﻮﻟ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
Figure 6.7
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
When an element is added to a queue, it is added to the rear. When an
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
element is removed from a queue, it is removed from the front. The two

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
primary queue operations are enqueing and dequeuing. To ensure means

‫ﻨﻮ‬
‫ﻟﻮ‬
to insert an element at the rear of a queue, and to dequeue means to

‫ﺟﻴ‬
‫ﺎﺍ‬
remove an element from the front of a queue. There are several different
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

algorithms for implementing these operations. We will begin by looking at


‫ﻣﺎ‬
‫ﺕ‬

the most simple.


‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬

Suppose we have an empty static integer queue that is capable of


‫ﺸﻴ‬
-‫ﺦ‬

holding a maximum of three values. With that queue we execute the


76

following enqueue operations.


42
-2

Enqueue(3)
18
1-

Enqueue(6)
-1
25
20

Enqueue(9)

129
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
Figure 6.8 illustrates the state of the queue after each of the enqueue

‫ﻭ‬
‫ﺗﻜ‬
operations shown above. Notice that front index (which is a variable

‫ﻨ‬
‫ﻮ‬ ‫ﻮﻟ‬
holding a subscript, or perhaps a pointer) always reference the same

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
physical element. The rear index moves forward in the array, as items are

‫ﻤﻌ‬
‫ﻠﻮ‬
enqueued.

‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
front rear

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
Enqueue (3) 3

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
front rear
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
Enqueue (6) 3 6

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
front rear ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬

Enqueue (9) 3 6 9
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬

Figure 6.8
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

Now let's see how dequeue operations are performed. Figure 6.9
76
42

illustrates the state of the queue after of three consecutive dequeue


-2

operations.
1 8
1-
-1
25
20

130
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
front rear

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
dequeue () 6 9

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
front rear

‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
dequeue () 9

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
Front = -1 Rear = -1
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
dequeue ()
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Figure 6.9

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
In the dequeueing operation, the element at the front of the queue is

‫ﻟﻮ‬
‫ﺟﻴ‬
removed. This is done by removing all the elements after it forward by one ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

position. After the first dequeue operation, the value 3 is removed from the
‫ﻠﻮ‬
‫ﻣﺎ‬

queue, and the value 6 is at the front. After the second operation, the value
‫ﺕ‬
‫ﺑﻜ‬

6 is removed and the value 9 is at the front. Notice that when only one
‫ﻔﺮ‬
‫ﺍﻟ‬

value is stored in the queue, that value is at both the front and the rear.
‫ﺸﻴ‬
-‫ﺦ‬

When the last dequeue operation is performed in the illustration above,


76

the queue is empty. An empty queue can be signified by setting both front
42
-2

and rear indicates to -1. The problem with the algorithm described above
1 8

is its inefficiency. Each time an item is dequeued, the remaining items in


1-
-1

the queue are copied forward to their neighboring element. The more
25
20

items there are in the queue, the longer each successive dequeue operation
will take.
Here is one way to overcome the problem: make both the front and rear
move in the array. As before, when an item is enqueued, the rear index is

131
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
moved to make room for it. But, in this design, when an item is dequeued,

‫ﻭ‬
‫ﺗﻜ‬
the front index moves by one element toward the rear of the queue. This

‫ﻨ‬
‫ﻮ‬‫ﻮﻟ‬
logically removes the front item from the queue, and eliminates the need

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
to copy the remaining items to their neighboring elements.

‫ﻤﻌ‬
‫ﻠﻮ‬
Using this approach, as items are added and removed, the queue

‫ﻣﺎ‬
‫ﺕ‬
gradually "crawls" toward the end of the array. This is illustrated in figure

‫ﺑﻜ‬
‫ﻔﺮ‬
6.10. The shaded squares represent the queue elements (between the front

‫ﺍﻟ‬
‫ﺸﻴ‬
and rear).

-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

Figure 6.10
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

The problem with this approach is that the rear index cannot move
-‫ﺦ‬

beyond the last element in the array. The solution is to think of the array as
76

circular instead of linear. When an item moves past the end of a circular
42
-2

array, it simply "wraps around" to the beginning. For example, consider


8
1

the queue depicted in Figure 6.11.


1-
-1

7 9 6 3
25
20

[0] [1] [2] [3] [4] [5] [6] [7] [8]


Rear=8
Front=5
Figure 6.11

132
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
The value 3 is at the rear of the queue, and the value 7 is at the front of

‫ﻭ‬
‫ﺗﻜ‬
the queue. Now, suppose an enqueue operation is performed, inserting the

‫ﻨ‬
‫ﻮ‬‫ﻮﻟ‬
value 5 into the queue. Figure 6.12 shows how the rear of the queue wraps

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
around to the beginning of the array.

‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
4 7 9 6 3

‫ﺑﻜ‬
‫ﻔﺮ‬
[0] [1] [2] [3] [4] [5] [6] [7] [8]

‫ﺍﻟ‬
‫ﺸﻴ‬
Rear=0

-‫ﺦ‬
76
Front=5

‫ﻤﻌ‬
42

‫ﻬﺪ‬
Figure 6.12
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
So, what is the code for wrapping the rear marker around to the opposite
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

end of the array? One straight- forward approach is to use an if statement

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
such as:

‫ﻟﻮ‬
‫ﺟﻴ‬
if (rear= queueSize-1) ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

rear=0
‫ﻠﻮ‬
‫ﻣﺎ‬

else
‫ﺕ‬
‫ﺑﻜ‬

rear ++
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

Static queue class Implementation


76
42

The declaration of the IntQueue class is as follows:


-2
8

Contents of IntQueue. H
1
1-
-1

1 #ifndef INTQUEUE H
25

2 #define INTQUEUE H
20

3
4 class IntQueue
5{
6 private:
7 int *queueArray;

133
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
8 int queueSize;

‫ﻭ‬
9 int front;

‫ﺗﻜ‬
‫ﻨ‬
10 int rear;

‫ﻮﻟ‬
‫ﻮ‬
11 int numitems;

‫ﺟﻴﺎ‬
12 public:

‫ﺍﻟ‬
‫ﻤﻌ‬
13 IntQueue(int);

‫ﻠﻮ‬
14 -IntQueue();

‫ﻣﺎ‬
‫ﺕ‬
15 void enqueue(int);

‫ﺑﻜ‬
16 void dequeue(int &);

‫ﻔﺮ‬
17 bool isEmpty() const;

‫ﺍﻟ‬
‫ﺸﻴ‬
18 bool isFull() const;

-‫ﺦ‬
19 void clear();

76
20 };

‫ﻤﻌ‬
42
21 #endif

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
Notice that in addition to the operations discussed in this section, the class
18

‫ﻰ‬
1-

‫ﻟﻺ‬
also declares a member function named clear. This function clears the
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

queue by resetting the front and rear indices and setting the numitems

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
member to 0. The member function definitions are listed here:

‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
Contents of IntQueue. cpp
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬

1 #include <iostream>
‫ﺕ‬

2 #include '' IntQueue. h ''


‫ﺑﻜ‬
‫ﻔﺮ‬

3 using namespace std;


‫ﺍﻟ‬

4
‫ﺸﻴ‬

5 //*************************
-‫ﺦ‬

6 // Constructor. *
76

7 //*************************
42

8 IntQueue::IntQueue(int s)
-2

9{
8
1

10
1-

11
-1
25

12
20

queueArray = new int[s];


queueSize = s;
front = -1· I
13 rear = -1;
14 numitems = O;
15 }

134
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
16

‫ﻭ‬
17 //*************************

‫ﺗﻜ‬‫ﻨ‬
18 // Destructor. *

‫ﻮ‬ ‫ﻮﻟ‬
19 //*************************

‫ﺟﻴﺎ‬
20 IntQueue::-IntQueue()

‫ﺍﻟ‬
‫ﻤﻌ‬
21 {

‫ﻠﻮ‬
22 delete [] queueArray;

‫ﻣﺎ‬
‫ﺕ‬
23 }

‫ﺑﻜ‬
24

‫ﻔﺮ‬
25 II********************************************

‫ﺍﻟ‬
‫ﺸﻴ‬
26 II Function enqueue inserts the value in num *

-‫ﺦ‬
27 II at the rear of the queue. *

76
28 II********************************************

‫ﻤﻌ‬
42
29 void IntQueue::enqueue(int num)

‫ﻬﺪ‬
-2

30 {

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
31 if (isFull())
18

‫ﻰ‬
1-

32 {

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
33 cout << ''The queue is full. \n '';

‫ﺭﺓ‬
25

34 exit(l);

‫ﻭ‬
20

‫ﺗﻜ‬
35 }

‫ﻨﻮ‬
‫ﻟﻮ‬
36 else

‫ﺟﻴ‬
37 { ‫ﻟ‬ ‫ﺎﺍ‬
38 II Calculate the new rear position
‫ﻤﻌ‬
‫ﻠﻮ‬

39 rear = (rear + 1) % queueSize;


‫ﻣﺎ‬

40 II Insert new item


‫ﺕ‬

41 queueArray[rear] = num;
‫ﺑﻜ‬
‫ﻔﺮ‬

42 II Update item count


‫ﺍﻟ‬

43 numitems++;
‫ﺸﻴ‬

44 }
-‫ﺦ‬

45 }
76

46
42

47 II**********************************************
-2

48 II Function dequeue removes the value at the *


18

49 II front of the queue, and copies it into num. *


1-

50 II**********************************************
-1
25

51 void IntQueue::dequeue(int &num)


20

52 {
53 if (isEmpty())
54 {
55
56
57 }

135
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
cout << ''The queue is empty. \n '';

‫ﻭ‬
exit(l);

‫ﺗﻜ‬‫ﻨ‬
58 else

‫ﻮ‬ ‫ﻮﻟ‬
59 {

‫ﺟﻴﺎ‬
60 II Move front

‫ﺍﻟ‬
‫ﻤﻌ‬
61 front = (front + 1) % queueSize;

‫ﻠﻮ‬
62 II Retrieve the front item

‫ﻣﺎ‬
‫ﺕ‬
63 num = queueArray[front];

‫ﺑﻜ‬
64 II Update item count

‫ﻔﺮ‬
65 numitems--;

‫ﺍﻟ‬
‫ﺸﻴ‬
66 }

-‫ﺦ‬
67 }

76
68

‫ﻤﻌ‬
42
69 II*********************************************

‫ﻬﺪ‬
-2

70 II Function isEmpty returns true if the queue *

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
71 II is empty, and false otherwise. *
18

‫ﻰ‬
1-

72 II*********************************************

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
73 bool IntQueue::isEmpty() canst

‫ﺭﺓ‬
25

74 {

‫ﻭ‬
20

‫ﺗﻜ‬
75 if (numitems > 0)

‫ﻨﻮ‬
‫ﻟﻮ‬
76 return false;

‫ﺟﻴ‬
77 else ‫ﻟ‬ ‫ﺎﺍ‬
78
‫ﻤﻌ‬
‫ﻠﻮ‬

79 }
‫ﻣﺎ‬

80
‫ﺕ‬

return true;
‫ﺑﻜ‬
‫ﻔﺮ‬

8 1 II********************************************
‫ﺍﻟ‬

82 II Function isFull returns true if the queue *


‫ﺸﻴ‬

8 3 II is full, and false otherwise. *


-‫ﺦ‬

8 4 II********************************************
76

85 bool IntQueue::isFull() canst


42

86 {
-2

87 if (numitems < queueSize)


18

88 return false;
1-

8 9 else
-1
25

90 return true;
20

91 }
92
93 II*******************************************
94 II Function clear resets the front and rear *
95 II indices, and sets numitems to 0. *
96 II*******************************************

136
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
97 void IntQueue::clear()

‫ﻭ‬
98 {

‫ﺗﻜ‬‫ﻨ‬
99 front = - 1 ·I

‫ﻮ‬ ‫ﻮﻟ‬
100 rear = - 1 ;

‫ﺟﻴﺎ‬
101 numitems = O;

‫ﺍﻟ‬
‫ﻤﻌ‬
102 }

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
Program 4-1 is a driver that demonstrates the IntQueue class.

‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
1 II This program demonstrates the IntQueue class.

‫ﺸﻴ‬
2 #include <iostream>

-‫ﺦ‬
3 #include ''IntQueue.h ''
76

‫ﻤﻌ‬
4 using namespace std;
42

‫ﻬﺪ‬
5
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
6 int main()
18

‫ﻰ‬
7{
1-

‫ﻟﻺ‬
8 IntQueue iQueue(5);
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

1 0 cout << ''Enqueuing 5 items ... \n '';

‫ﺗﻜ‬
‫ﻨﻮ‬
11

‫ﻟﻮ‬
1 2 II Enqueue 5 items
‫ﺟﻴ‬
1 3 for (int k = 1 ; k <= 5; k++) ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬

14 [Link](k*k);
‫ﻠﻮ‬

15
‫ﻣﺎ‬
‫ﺕ‬

1 6 II Deqeue and retrieve all items in the queue


‫ﺑﻜ‬

1 7 cout << ''The values in the queue were: '';


‫ﻔﺮ‬

1 8 while (![Link]())
‫ﺍﻟ‬
‫ﺸﻴ‬

19{
-‫ﺦ‬

20 int value;
76

21 [Link](value);
42

22 cout << value << '' '' .


-2

I
8

23 }
1
1-

24 cout << endl;


-1

25 return O;
25

26 }
20

Program Output
Enqueuing 5 items ...
The values in the queue were: 1 4 9 16 25

137
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
6.8 Dynamic Queues

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮ‬ ‫ﻮﻟ‬
Concept: A queue may be implemented as a linked list, and expand or

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
shrink with each enqueue or dequeue operation.

‫ﻤﻌ‬
‫ﻠﻮ‬
Dynamic queues, which are built around linked lists, are much more

‫ﻣﺎ‬
‫ﺕ‬
intuitive to understand than static queues. A dynamic queue starts as an

‫ﺑﻜ‬
‫ﻔﺮ‬
empty linked list. With the first enqueue operation, a node is added, which

‫ﺍﻟ‬
‫ﺸﻴ‬
is pointed to by the front and rear pointers. As each new item as added to

-‫ﺦ‬
76
the queue, anew node is added to the rear of the list, and the rear pointer is

‫ﻤﻌ‬
42

‫ﻬﺪ‬
updated to point to the new node. As each item is dequeued, the node
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
pointed to by the front pointer is deleted, and front is made to pint to the
18

‫ﻰ‬
1-

‫ﻟﻺ‬
next node in the list. Figure 6.13 shows the structure of a dynamic queue.
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
Null

‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬

front rear
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬

Figure 6.13
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

Example 1: Trace the following operations:


76
42

- Enqueue (3)
-2

- Enqueue (1)
8

- Enqueue (4)
1
1-

- Enqueue (1)
-1

- Enqueue (5)
25

- Dqueue ()
20

- Dqueue ()
- Dqueue()

138
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
Enqueue (3) 3

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬‫ﻮ‬
‫ﺟﻴﺎ‬
Enqueue (1) 3 1

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
Enqueue (4)

‫ﺑﻜ‬
3 1 4

‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
Enqueue (1) 3 1 4 1

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
Enqueue (5)

‫ﺭﺓ‬
25

3 1 4 1 5

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

Dequeue ()
‫ﻠﻮ‬

1 4 1 5
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

Dequeue () 4 1 5
76
42
-2
81
1-

Dequeue () 1 5
-1
25
20

Example 2: trace the following operations

- Enqueue (5)

139
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫‪Chapter 6‬‬ ‫‪Queue‬‬

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
‫‪-‬‬ ‫)‪Enqueue (7‬‬

‫ﻭ‬
‫‪-‬‬ ‫)( ‪Dqueue‬‬

‫ﺗﻜ‬‫ﻨ‬
‫‪-‬‬ ‫)‪Enqueue (9‬‬

‫ﻮ‬ ‫ﻮﻟ‬
‫‪-‬‬ ‫)‪Enqueue (12‬‬

‫ﺟﻴﺎ‬
‫‪-‬‬ ‫)( ‪Dqueue‬‬

‫ﺍﻟ‬
‫ﻤﻌ‬
‫‪-‬‬ ‫)‪Enqueue (10‬‬

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
‫)‪Enqueue (5‬‬ ‫‪5‬‬

‫ﺦ‪-‬‬
‫‪76‬‬

‫ﻤﻌ‬
‫‪42‬‬

‫ﻬﺪ‬
‫‪-2‬‬

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
‫‪18‬‬

‫)‪Enqueue (7‬‬

‫ﻰ‬
‫‪5‬‬ ‫‪7‬‬
‫‪1-‬‬

‫ﻟﻺ‬
‫‪-1‬‬

‫ﺩﺍ‬
‫ﺭﺓ‬
‫‪25‬‬

‫ﻭ‬
‫‪20‬‬

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫)( ‪Dequeue‬‬ ‫‪7‬‬ ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
‫ﺦ‪-‬‬
‫‪76‬‬
‫‪42‬‬
‫‪-2‬‬
‫‪1‬‬ ‫‪8‬‬
‫‪1-‬‬
‫‪-1‬‬
‫‪25‬‬
‫‪20‬‬

‫‪140‬‬
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
Enqueue (7) 7 9

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮ‬ ‫ﻮﻟ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
Enqueue (12) 7 9 12

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
Dequeue () 9 12

-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

Enqueue (10) 9 12 10

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

6.9 Summary of queue concepts

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
Data structure called a single-ended queue. Whereas in a stack we add

‫ﺟﻴ‬
and remove at the same end of the pile, in a single-ended queue we add
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

elements at one end and remove them from the other. Since it is always
‫ﻣﺎ‬
‫ﺕ‬

the first item to be put into the queue that is the first item to be removed, a
‫ﺑﻜ‬
‫ﻔﺮ‬

queue is a first-in, first-out or FIFO data structure. Figure 6.14 illustrates


‫ﺍﻟ‬
‫ﺸﻴ‬

the basic queue operations.


-‫ﺦ‬
76
42
-2
18
1-
-1
25
20

Figure 6.14

141
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
A queue is a pile in which items are added a one end and removed from

‫ﻭ‬
‫ﺗﻜ‬
the other. In this respect, a queue is like the line of customers waiting to be

‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
served by a bank teller. As customers arrive, they join the end of the queue

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
while the teller servers the customer at the head of the queue. As a result, a

‫ﻤﻌ‬
‫ﻠﻮ‬
queue is used when a sequence of activities must be done on a first-come,

‫ﻣﺎ‬
‫ﺕ‬
first-served basis.

‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42
-2
1 8
1-
-1
25
20

142
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
[Q4] Review Question

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
Q1- What is the actions allowed on a queue?

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
Q2- What is the difference between static and dynamic queues.

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
Q3- Write the queue specification for the following operations:

‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
A. MakeEmpty

‫ﺸﻴ‬
-‫ﺦ‬
B. IsEmpty

76

‫ﻤﻌ‬
C. Enqueue 42

‫ﻬﺪ‬
-2

D. Dequeue

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Q4- Identify true of false and rewrite the correct version of false one:

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
A. A queue is a first-in-first out (FIFO) data structure.

‫ﻟﻮ‬
‫ﺟﻴ‬
B. Elements of queue ADT are added to front and removed from‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬

rear end.
‫ﻠﻮ‬
‫ﻣﺎ‬

C. A dynamic queue is built on the built on the linked list instead of


‫ﺕ‬
‫ﺑﻜ‬

an array
‫ﻔﺮ‬
‫ﺍﻟ‬

D. Communication software uses queues to hold information


‫ﺸﻴ‬
-‫ﺦ‬

received over networks and dial-up connections.


76
42
-2

Q5-Indicate the suitable data structure for each of the following


1 8
1-

applications:
-1
25
20

A. A program to evaluate arithmetic expressions according to the


specific order of operators
B. A bank simulation of its teller operation to see how waiting times
would be affected by adding another teller.

143
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 6 Queue

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
C. A program to receive data that are to be saved and processed in

‫ﻭ‬
‫ﺗﻜ‬
the reverse order.

‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
D. An address book to be maintained.

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
E. A word processor to have a PF key that causes the preceding

‫ﻤﻌ‬
‫ﻠﻮ‬
command to be redisplayed. Every time the PF key is pressed, the

‫ﻣﺎ‬
‫ﺕ‬
program is to show the command that preceded the one currently

‫ﺑﻜ‬
‫ﻔﺮ‬
displayed.

‫ﺍﻟ‬
‫ﺸﻴ‬
F. A program to keep track of patients as they check into a medical

-‫ﺦ‬
76
clinic, assigning patients to doctors on a first-come, first-served

‫ﻤﻌ‬
42

‫ﻬﺪ‬
basis.
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
Q6- Suppose the following operations are performed on an empty queue:

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
enqueue(5)

‫ﻨﻮ‬
‫ﻟﻮ‬
enqueue(7)

‫ﺟﻴ‬
dequeue() ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
enqueue(9)
‫ﻠﻮ‬

enqueue(12)
‫ﻣﺎ‬
‫ﺕ‬

dequeue()
‫ﺑﻜ‬

enqueue(10)
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

Trace and draw the diagrams of executing the above operations.


-‫ﺦ‬
76
42
-2
8
1
1-
-1
25
20

144
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫ﻌﺎﻟ‬
Chapter 7 Trees and graphs

‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
Chapter 7

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
Trees and Graphs

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
7.1 Introduction

‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
A tree is a data structure consisting of data nodes connected to each

-‫ﺦ‬
other with pointers. In much same spirit as a linked list. However, each
76

‫ﻤﻌ‬
node in a tree may be connected to two or more other nodes, rather than
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
the single node allowed in a linked list. The maximum number of nodes to

‫ﻌﺎﻟ‬
18

‫ﻰ‬
which any single node may be connected is called the order of the tree.
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
The simplest tree is of order 2, and is called a binary tree. A diagram of a

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
binary tree is shown in figure 7.1.

‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42

Figure 7.1
-2
18
1-

Each node in this diagram contains one or more data fields, and two
-1
25

pointers, one to the left child and the other to the right child. The topmost
20

node in the tree is called the root node. Anode with no children is called a
leaf. If we redrew the tree shown above to illustrate the internal structure
of its nodes, it would look as shown in figure 7.2.

145
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫ﻌﺎﻟ‬
Chapter 7 Trees and graphs

‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
Each node is shown with its tree main components in figure 7.2: the

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
data area and the pointers to the left and right children. These pointers are

‫ﻮ‬
‫ﺟﻴﺎ‬
used in the same way as pointers in the linked list. When a new node is to

‫ﺍﻟ‬
‫ﻤﻌ‬
be added to the tree, memory is allocated for the new node using the new

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
pointer, and the address of this location is loaded into the appropriate

‫ﺑﻜ‬
pointer (left or right) in the new node's parent. Just as a null pointer

‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
indicates the end of a linked list, a null pointer in a tree indicates the end

-‫ﺦ‬
of a branch in that direction. Therefore, a leaf is a node, both of whose
76

‫ﻤﻌ‬
pointers are null.
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42

Figure 7.2
-2
1 8
1-

5.2 Tree Operations


-1
25
20

A binary tree as defined above can contain any type of data in any
order, depending on the use to which the tree is to be put. The most
common use of trees however, is as yet another method for sorting and
searching data. Trees, which are used for this purpose, are called search

146
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫ﻌﺎﻟ‬
Chapter 7 Trees and graphs

‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
trees, and binary trees used for sorting and searching data are called binary

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
search trees.

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
Trees (binary and otherwise) have much the same basic types of

‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
operations as other data structures:

‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
• Inserting a new node

‫ﺍﻟ‬
‫ﺸﻴ‬
• Deleting a node

-‫ﺦ‬
• Listing or visiting the nodes of the tree.
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

One way that a binary search tree can be used to sort data is as follows.

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
Let us suppose that we have a list of integers, such as 5, 2, 8, 4, and 1, that
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
we wish to sort into ascending order. We can perform a two-stage sorting

‫ﺭﺓ‬
25

‫ﻭ‬
algorithm. The first stage involves inserting the integers into a binary
20

‫ﺗﻜ‬
‫ﻨﻮ‬
search tree:

‫ﻟﻮ‬
‫ﺟﻴ‬
Inserting Algorithm:
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬

• If the current pointer is 0, create a new node, store the data and return
‫ﺕ‬

the address of the new node.


‫ﺑﻜ‬
‫ﻔﺮ‬

• Otherwise, compare the integer to the data stored at the current node. If
‫ﺍﻟ‬
‫ﺸﻴ‬

the new integer is less than the integer at the current node, insert the
-‫ﺦ‬

new integer into the left child of the current node (by recursively
76
42

applying the same algorithm). Otherwise insert it into the right child of
-2

the current node.


81
1-
-1
25
20

We illustrate the process by inserting the integers as shown in figure 7.3

147
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫ﻌﺎﻟ‬
Chapter 7 Trees and graphs

‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

Figure 7.3 inserting the integers 5,2,8,4, and 1 into a binary search tree

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
The first integer (5) is inserted into an empty tree (where the root
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

pointer is 0), so the root node is created and the integer 5 is stored there.

‫ﻭ‬
20

‫ﺗﻜ‬
The next integer (2) is compared with the value at the root node, found to

‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
be less than it, and inserted as the left child of the root. Similarly the third
‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬
integer (8) is inserted as the right child of the root since it is larger than 5.
‫ﻠﻮ‬
‫ﻣﺎ‬

The fourth integer (4) is compared with the root and found to be less than
‫ﺕ‬
‫ﺑﻜ‬

5, so the algorithm tells us to insert 4 into the left child. The left child is
‫ﻔﺮ‬
‫ﺍﻟ‬

already occupied, however, so we simply apply the algorithm recursively


‫ﺸﻴ‬
-‫ﺦ‬

starting at the left child of the root. The integer 4 is compared with 2,
76

found to be greater than 2, so it is inserted as the right child of 2. The


42
-2

process can be continued as long as we have more data to add to the tree.
8
1
1-

Having inserted the numbers into the binary search tree, it may not be
-1
25

immediately obvious how that has helped us. In order to produce a sorted
20

list of the data, we need to traverse the tree, that is, list the nodes in some
specific order.

148
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫ﻌﺎﻟ‬
Chapter 7 Trees and graphs

‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
Various types of tree traversal exist, but the most common is in order

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
traversal, which means that we follow the algorithm:

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
Traverse Algorithm:

‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
1. If the current pointer is not 0, then:

‫ﺕ‬
‫ﺑﻜ‬
2. Traverse the left child of the current pointer.

‫ﻔﺮ‬
‫ﺍﻟ‬
3. Visit (or print) the data at the current pointer.

‫ﺸﻴ‬
-‫ﺦ‬
4. Traverse the right child of the current pointer.

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
Steps 2 and 4 in this algorithm use recursion: they call the same

‫ﻌﺎﻟ‬
18

‫ﻰ‬
algorithm to process the left and right children of the current pointer.
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

To see how the traversal produces a sorted list, consider the binary

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
search tree that we produced in figure 7.3. We begin the traversal

‫ﻟﻮ‬
‫ﺟﻴ‬
algorithm, as usual, with the root node. The root pointer is not 0, so we‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬

must traverse the left child. Its pointer isn't 0 either (it contains the value
‫ﻠﻮ‬
‫ﻣﺎ‬

2), so we must visit its left child. This child's pointer still isn't 0 (it
‫ﺕ‬
‫ﺑﻜ‬

contains the value 1), so we call the algorithm again for the node
‫ﻔﺮ‬
‫ﺍﻟ‬

containing the value 1. The pointer to the left child of this node is now 0,
‫ﺸﻴ‬
-‫ﺦ‬

so the recursive call to this node will return without doing anything. We
76

have now completed step 2 in the algorithm for the node 1, so we can now
42
-2

proceed to step 3 for that node, which prints out the number 1. This looks
18

promising, since the first value actually printed from the traversal
1-
-1

algorithm is, in fact, the smallest number stored in the tree.


25
20

We then complete the algorithm for node 1 by traversing its right child.
Since the pointer to its right child is 0, the algorithm is complete for node
1. We can now return to the processing of the next node up, which is node
2. Its left child has been fully traversed, so we now print its value, which is
149
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫ﻌﺎﻟ‬
Chapter 7 Trees and graphs

‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
2. We then traverse the right child of node 2, which takes us to node 4.

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
Since node 4 is a leaf (it has no children) it is printed after node 2.

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
The entire left subtree of the root node has now been traversed,

‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
producing the list 1, 2, 4. The root node may now be printed, giving the

‫ﺕ‬
value 5. Finally, the right child of the root is traversed, printing out the

‫ﺑﻜ‬
‫ﻔﺮ‬
value 8. The completed list is 1, 2, 4, 5, 8, which is the correctly sorted

‫ﺍﻟ‬
‫ﺸﻴ‬
list.

-‫ﺦ‬
76

‫ﻤﻌ‬
42
C++ Code for the insert function

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
void IntBinaryTree::insert(TreeNode * &tree, int num)
1-

‫ﻟﻺ‬
{
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
// If the tree is empty, make a new node and make it
20

‫ﺗﻜ‬
‫ﻨﻮ‬
// the root of the tree

‫ﻟﻮ‬
if (!tree)
‫ﺟﻴ‬
{ ‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬
tree = new TreeNode(num);
‫ﻠﻮ‬

return;
‫ﻣﺎ‬
‫ﺕ‬

}
‫ﺑﻜ‬

// If num is already in tree: return


‫ﻔﺮ‬

if (tree->value == num)
‫ﺍﻟ‬
‫ﺸﻴ‬

return;
-‫ﺦ‬

//The tree is not empty: insert the new node into the
76

//left or right subtree


42

if (num < tree->value)


-2

insert(tree->left, num);
else
18

insert(tree->right, num);
1-
-1

}
25
20

Note thaNote that the function is passed a reference to a pointer because


the pointer passed may need to be modified by the function. This is also
the reason the remove and makeDeletion functions are passed their
parameters by reference.

150
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫ﻌﺎﻟ‬
Chapter 7 Trees and graphs

‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
An algorithm for deleting a node from a binary search tree in such a way

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
that the remaining nodes still have the same in order traversal:

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
Deleting Node Algorithm:

‫ﻤﻌ‬
‫ﻠﻮ‬
1. Leaves - these are the easiest, since all we need to do is delete the leaf

‫ﻣﺎ‬
‫ﺕ‬
and set the pointer from its parent (if any) to 0.

‫ﺑﻜ‬
‫ﻔﺮ‬
2. Nodes with a single child - these too are fairly easy, since we just redirect

‫ﺍﻟ‬
‫ﺸﻴ‬
the pointer from the node's parent so that it points to the child.

-‫ﺦ‬
3. Nodes with both children - these can be fairly tricky, since we must

76

‫ﻤﻌ‬
rearrange the tree to some extent. The method we shall use is to replace the
42

‫ﻬﺪ‬
-2

node being deleted by the right most node in its left subtree. (We could

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

equally well have used the leftmost node in the right subtree.) Because of the

‫ﻰ‬
1-

‫ﻟﻺ‬
rules for in order traversals, this method guarantees the same traversal.
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
Assuming the node to be deleted is present in the tree, we are faced with

‫ﺟﻴ‬
two possibilities: the required node is the root node, or it is some other
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

node. These must be treated as separate cases, since the root node has no
‫ﻣﺎ‬
‫ﺕ‬

parent.
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

The C++ code for delete function


-‫ﺦ‬

void IntBinaryTree::makeDeletion(TreeNode *&tree)


76

{
42

// Used to hold node that will be deleted


-2

TreeNode *nodeToDelete = tree;


8

//Used to locate the point where the


1
1-

//left subtree is attached


-1

TreeNode *attachPoint;
25

if (tree->right == NULL)
20

{
// Replace tree with its left subtree
tree = tree->left;
}
else if (tree->left == NULL)

151
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫ﻌﺎﻟ‬
Chapter 7 Trees and graphs

‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
{

‫ﻭ‬
‫ﺗﻜ‬
// Replace tree with its right subtree

‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
tree = tree->right;

‫ﺟﻴﺎ‬
}

‫ﺍﻟ‬
‫ﻤﻌ‬
else

‫ﻠﻮ‬
//The node has two children

‫ﻣﺎ‬
‫ﺕ‬
{

‫ﺑﻜ‬
//Move to right subtree

‫ﻔﺮ‬
attachPoint = tree->right;

‫ﺍﻟ‬
‫ﺸﻴ‬
// Locate the smallest node in the right subtree

-‫ﺦ‬
by moving as far to the left as possible
while (attachPoint->left ! = NULL)
76

‫ﻤﻌ‬
attachPoint = attachPoint->left;
42

‫ﻬﺪ‬
//Attach the left subtree of the original tree
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
// as the left subtree of the smallest node
18

‫ﻰ‬
//in the right subtree
1-

‫ﻟﻺ‬
-1

attachPoint->left = tree->left;

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Replace the original tree with its right subtree

‫ﻭ‬
20

‫ﺗﻜ‬
tree = tree->right;

‫ﻨﻮ‬
}

‫ﻟﻮ‬
‫ﺟﻴ‬
//Delete root of original tree
delete nodeToDelete;
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬

}
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42
-2
8
1
1-
-1
25
20

152
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫ﻌﺎﻟ‬
Chapter 7 Trees and graphs

‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
[Q5] Review questions

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
Q1- Fill in the blank node

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
A. The first node in a binary tree is called the -----------

‫ﻤﻌ‬
‫ﻠﻮ‬
B. A binary tree node's left and right pointers point to the node's --------

‫ﻣﺎ‬
‫ﺕ‬
---

‫ﺑﻜ‬
‫ﻔﺮ‬
C. A node with no children is called -----------

‫ﺍﻟ‬
‫ﺸﻴ‬
D. A ----------- is an entire branch of the tree, from one particular node

-‫ﺦ‬
down
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
Q2- True or False

‫ﻌﺎﻟ‬
18

‫ﻰ‬
A. Each node in a binary tree must have at least two children.
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
B. When a node is inserted into a tree, it must be inserted as a leaf

‫ﺭﺓ‬
25

‫ﻭ‬
node.
20

‫ﺗﻜ‬
‫ﻨﻮ‬
C. Values stored in the current node's left subtree are less than the

‫ﻟﻮ‬
‫ﺟﻴ‬
value stored in the current node ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

D. The shape of a binary tree is determined by the order in which


‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

values are inserted.


‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬

Q3- Writ a pseudocode algorithm for inserting node in a tree.


‫ﺸﻴ‬
-‫ﺦ‬

Q4- Suppose the following values are inserted into a binary tree, in the
76
42

order given:
-2

12,7,9,10,22,24,30,18,3,14,20
81
1-

Draw a diagram of the resulting binary tree


-1
25
20

Q5- Identify true or false and rewrite the correct version of false one:
A. A tree is data structure consisting of data nodes connected to each
other with pointers
B. The simple tree is of order 3 and is called binary tree

153
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
‫ﻌﺎﻟ‬
Chapter 7 Trees and graphs

‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
C. A binary tree node with no children is called a root.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
D. A binary tree is a linear linked list where each node may point to

‫ﻮ‬
‫ﺟﻴﺎ‬
two other nodes.

‫ﺍﻟ‬
‫ﻤﻌ‬
E. Binary trees are suitable for algorithms that must search large

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
amounts of information.

‫ﺑﻜ‬
‫ﻔﺮ‬
Q6- Draw the structure of a binary tree, and identify:-

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
- Root node

76

‫ﻤﻌ‬
- Right childs 42

‫ﻬﺪ‬
-2

- Left childs

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
- Leaf node
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
Q7-Draw a diagram of the resulting binary tree after the following
20

‫ﺗﻜ‬
‫ﻨﻮ‬
operations:-

‫ﻟﻮ‬
‫ﺟﻴ‬
- Insert 5,8,3,12,9 ‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬

- Remove 8,12
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

- Insert 10
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42
-2
1 8
1-
-1
25
20

154
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Chapter 8

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
Algorithm Analysis

‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
8.1 Introduction

‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
What is an algorithm and why do we want to analyze one? An

‫ﺍﻟ‬
‫ﺸﻴ‬
algorithm is a step-by-step procedure for accomplishing some end. An

-‫ﺦ‬
76
algorithm can be given in many ways. For example, it can be written down

‫ﻤﻌ‬
42

‫ﻬﺪ‬
in English (or French, or any other "natural" language). However, we are
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

interested in algorithms, which have been precisely specified using an

‫ﻰ‬
1-

‫ﻟﻺ‬
appropriate mathematical formalism-- such as a programming language.
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Given such as expression of an algorithm, what can we do with it?

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
Well, obviously we can run the program and observe its behavior. This is

‫ﻟﻮ‬
‫ﺟﻴ‬
not likely to be very useful or informative in the general case. If we can
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬

run a particular program on a particular computer with a particular set of


‫ﻠﻮ‬
‫ﻣﺎ‬

inputs, then all know is the behavior of the program in a single instance.
‫ﺕ‬
‫ﺑﻜ‬

Such knowledge is anecdotal and we must be careful when drawing


‫ﻔﺮ‬
‫ﺍﻟ‬

conclusions based upon anecdotal evidence.


‫ﺸﻴ‬
-‫ﺦ‬
76

8.2 The Algorithm Concepts.


42
-2
18

An algorithm is a precise set of instructions for achieving a particular


1-
-1

goal. In a computer program, all data structures have associated algorithm,


25
20

which are used to access and modify the data they contain.
For many operations performed on data structures, there are several
different algorithms that may be used. Different algorithms make different
demands on computer memory and running time. In an industrial setting,

155
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
where the cost of commercial software and staff time become important,

‫ﺩﺍ‬
‫ﺭﺓ‬
the availability and price of off-the shelf routines that implement the

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
algorithm if no commercial package is available, must be considered.

‫ﻮ‬
‫ﺟﻴﺎ‬
Therefore, it is useful to know things like the running time and memory

‫ﺍﻟ‬
‫ﻤﻌ‬
requirements of algorithms so that you can make an informed decision

‫ﻠﻮ‬
‫ﻣﺎ‬
about which algorithms is best for your particular application. The

‫ﺕ‬
‫ﺑﻜ‬
theoretical study of algorithms, known as complexity theory, is a highly

‫ﻔﺮ‬
‫ﺍﻟ‬
mathematical branch of computer science. It involves detailed analysis of

‫ﺸﻴ‬
-‫ﺦ‬
the number of steps required by an algorithm to process varying amounts

76

‫ﻤﻌ‬
of data. For many algorithms exact results have been derived, but for
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
others, only approximations of averages are available.

‫ﻌﺎﻟ‬
18

‫ﻰ‬
Since this course assumes no significant mathematical knowledge on
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
the part of the reader, we won't be going into any detailed studies of the

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
complexities of the algorithms we will be studying. It is still important,

‫ﻨﻮ‬
‫ﻟﻮ‬
however, for you to achieve an understanding of how the complexity of an

‫ﺟﻴ‬
‫ﺎﺍ‬
algorithm is measured, and to develop techniques whereby you can
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

measure these complexities experimentally by actually running programs


‫ﻣﺎ‬
‫ﺕ‬

to calculate them. This is one of the goals of this chapter. We begin our
‫ﺑﻜ‬
‫ﻔﺮ‬

study of algorithms by looking at a couple of methods for searching a list


‫ﺍﻟ‬
‫ﺸﻴ‬

of items. We use searching algorithms as an introduction because they are


-‫ﺦ‬

commonly used as examples in an introductory course in programming, so


76
42

there is a good chance you have met them before. They are also fairly
-2

simple algorithms, both to code and to analyze, so you shouldn't have to


18
1-

wrestle with the concepts behind the algorithms while you are trying to see
-1
25

how their efficiencies can be measured.


20

In this chapter we will study two types of algorithms (searching and


sorting). We will consider two searching algorithms: sequential search and
binary search.

156
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
8.3 Introduction to Search Algorithms

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
A search algorithm is a method of locating a specific item of

‫ﻮ‬
‫ﺟﻴﺎ‬
information in a larger collection of data. This section discusses two

‫ﺍﻟ‬
‫ﻤﻌ‬
algorithms for searching the contents of an array.

‫ﻠﻮ‬
‫ﻣﺎ‬
It's very common for programs not only to store and process

‫ﺕ‬
‫ﺑﻜ‬
information stored in arrays, but to search arrays for specific items. This

‫ﻔﺮ‬
‫ﺍﻟ‬
section will show you two methods of searching an array: the linear search

‫ﺸﻴ‬
-‫ﺦ‬
and the binary search. Each has its advantages and disadvantages.

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
8.4 The linear Search

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
The linear search is a very simple algorithm. Sometimes called a

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
sequential search, it uses a loop to sequentially step through an array,

‫ﻨﻮ‬
‫ﻟﻮ‬
starting with the first element. It compares each element with the value

‫ﺟﻴ‬
‫ﺎﺍ‬
being searched for, stops when either the value is found or the end of the
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

array is encountered. If the value being searched for is not in the array,
‫ﻣﺎ‬
‫ﺕ‬

algorithm will unsuccessfully search to the end of the array. Here is the
‫ﺑﻜ‬
‫ﻔﺮ‬

pseudocode for a function that performs the linear search.


‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

Linear Search pseudocode:


76
42

1. set found to false.


-2

2. set position to -1.


8

3. Set index to 0.
1
1-

4. while found is false and index < number of elements


-1

if list[index] is equal to search value


25

found = true.
20

position = index.
end if
add 1 to index.
return position.

157
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
The function searchList, which follows, is an example of C++ code used

‫ﺩﺍ‬
‫ﺭﺓ‬
to perform a linear search on an integer array. The array list, which has a

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
maximum of size elements, is searched for an occurrence of the number

‫ﻮ‬
‫ﺟﻴﺎ‬
stored in value. If the number is found, its array subscript is returned.

‫ﺍﻟ‬
‫ﻤﻌ‬
Otherwise, -1 is returned, indicating the value did not appear in the array.

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
int searchList(const int list[], int size, int value)

‫ﻔﺮ‬
{

‫ﺍﻟ‬
‫ﺸﻴ‬
int index = O; // Used as a subscript to search array

-‫ﺦ‬
int position = -1; // Used to record position of search value

76
bool found = false; //Flag to indicate if the value was found

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
while (index < size &&!found)

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
if (list[index]== value) //If the value is found
-1

‫ﺩﺍ‬
{

‫ﺭﺓ‬
25

found = true; //Set the flag

‫ﻭ‬
20

‫ﺗﻜ‬
position = index; // Record the value's subscript

‫ﻨﻮ‬
‫ﻟﻮ‬
}

‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
}
‫ﻠﻮ‬

index++; //Go to the next element


‫ﻣﺎ‬
‫ﺕ‬

}
‫ﺑﻜ‬

return position; //Return the position, or -1


‫ﻔﺮ‬

}
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

NOTE: The reason -1 is chosen to indicate that the search value was
76

not found in the array is that -1 is not a valid subscript. Any other non
42

valid subscript value could also have been used to signal this. Program
-2
8

6-1 is a complete program that uses the searchList function. It searches


1
1-
-1

the five element tests array to find a score of 100.


25
20

1 // This program demonstrates the searchList function,


2 // which performs a linear search on an integer array.
3 #include <iostream>
4 using namespace std;
5
158
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
6 II Function prototype

‫ﺩﺍ‬
‫ﺭﺓ‬
7 int searchList(const int [], int, int);

‫ﻭ‬
8

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
9 canst int SIZE = 5;

‫ﻮ‬
‫ﺟﻴﺎ‬
10
11 int main()

‫ﺍﻟ‬
‫ﻤﻌ‬
12 {

‫ﻠﻮ‬
‫ﻣﺎ‬
13 int tests[SIZE] = {87, 75, 98, 100, 82 } ;

‫ﺕ‬
14 int results; II Holds the search results

‫ﺑﻜ‬
15

‫ﻔﺮ‬
16 //Search the array for the value 100

‫ﺍﻟ‬
‫ﺸﻴ‬
17 results = searchList(tests, SIZE, 100);

-‫ﺦ‬
18

76
19 //If searchList returned -1, 100 was not found

‫ﻤﻌ‬
42

‫ﻬﺪ‬
20 if (results == -1)
-2

‫ﺍﻟ‬
21 cout << ''You did not earn 100 points on any test. \n '';

‫ﻌﺎﻟ‬
18

22 else

‫ﻰ‬
1-

‫ﻟﻺ‬
23 { // Otherwise results contains the subscript of
-1

‫ﺩﺍ‬
24 // the first 100 found in the array

‫ﺭﺓ‬
25

25 cout << ''You earned 100 points on test '';

‫ﻭ‬
20

‫ﺗﻜ‬
26 cout <<(results + 1) << ''.\n '';

‫ﻨﻮ‬
‫ﻟﻮ‬
27 }

‫ﺟﻴ‬
28 return 0; ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
29 }
‫ﻠﻮ‬

30
‫ﻣﺎ‬
‫ﺕ‬

31/*********************************************************
‫ﺑﻜ‬

32 * searchList *
‫ﻔﺮ‬

33* This function performs a linear search on an integer array. *


‫ﺍﻟ‬
‫ﺸﻴ‬

34* The list array, which has size elements, is searched for *
-‫ﺦ‬

35* the number stored in value. If the number is found, its array *
36* subscript is returned. Otherwise, -1 is returned. *
76

37*********************************************************/
42

38 int searchList(const int list[], int size, int value)


-2

39 {
18

40 int index = 0; // Used as a subscript to search array


1-
-1

41 int position = -1; // Used to record position of search value


25

42 bool found = false;//Flag to indicate if the value was found


20

43
44 while (index < size &&!found)
45 {
46 if (list[index]== value) // If the value is found
47 {
48 found -- true; //set the flag
159
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
49 position =index; //Record the value's subscript

‫ﺩﺍ‬
‫ﺭﺓ‬
50 }

‫ﻭ‬
51 index++ //Goto the next element

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
52 }

‫ﻮ‬
‫ﺟﻴﺎ‬
53 return position; //Return the position , or -1
54}

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
Program Output

‫ﺕ‬
‫ﺑﻜ‬
You earned 100 points on test 4.

‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
8.5 Efficiency of the Linear Search

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
The advantage of the linear search is its simplicity. It is very easy to
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
understand and implement. Furthermore, it doesn't require the data in the
18

‫ﻰ‬
1-

‫ﻟﻺ‬
array to be stored in any particular order. Its disadvantages, however, is its
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

inefficiency. If the array being searched contains 20,000 elements, and the

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
value is in the last element, the algorithm will have to look at each of the

‫ﻟﻮ‬
‫ﺟﻴ‬
first 19,999 elements in order to find a value stored in the last element (so
‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
that the algorithm actually reads the array 20,000 times).
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

In an average cased, an item is just as likely to be found at the


‫ﺑﻜ‬
‫ﻔﺮ‬

beginning of the array as near the end. Typically, for an array of N items,
‫ﺍﻟ‬
‫ﺸﻴ‬

the linear search will locate an item in N/2 attempts. If an array has 50,000
-‫ﺦ‬

elements, the linear search will make a comparison with 25,000 items of
76
42

them in a typical case. This is assuming, of course, that the search item is
-2

consistently found in the array. (If not found in the array, you will be
18
1-

searching each element; from start to finish.) N/2 is the average number of
-1
25

comparisons, where N is the maximum number of comparisons.


20

When the linear search fails to locate an item, it must make a


comparison with every element in the array. As the number of failed
search attempts increases, so does the average number of comparisons.

160
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Obviously, the linear search should not be used on large arrays if the speed

‫ﺩﺍ‬
‫ﺭﺓ‬
is important.

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
8.6 The Binary Search

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
It’s a clever algorithm that is much more efficient than the linear search.

‫ﻠﻮ‬
‫ﻣﺎ‬
It's only requirement is that the values in the array be sorted in order.

‫ﺕ‬
‫ﺑﻜ‬
Instead of testing the array’s first element, this algorithm starts with

‫ﻔﺮ‬
‫ﺍﻟ‬
element in the middle. If that element happens to contain the desired

‫ﺸﻴ‬
-‫ﺦ‬
value, then the search is over. Otherwise, the value on the middle element

76

‫ﻤﻌ‬
is either greater than or less than value being searched for. If it is greater,
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
then the desired value (if it is in the list) will be found somewhere in the

‫ﻌﺎﻟ‬
18

‫ﻰ‬
first half of the array. If it is less, then the desired value (again, if it is in
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
the list) will be found somewhere in the last half of the array. In either

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
case, half of the array's elements have been eliminated from further

‫ﻨﻮ‬
‫ﻟﻮ‬
searching.

‫ﺟﻴ‬
‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬
If the desired value wasn't found in the middle element, the procedure
‫ﻠﻮ‬
‫ﻣﺎ‬

described above is repeated for the half of the array that potentially
‫ﺕ‬
‫ﺑﻜ‬

contains the value. For instance, if the last half of the array is to be
‫ﻔﺮ‬
‫ﺍﻟ‬

searched, the algorithm immediately tests its middle element. If the


‫ﺸﻴ‬
-‫ﺦ‬

desired value isn’t found there, the search is narrowed to the quarter of the
76

array that resides before or after that element. This procedure continues
42
-2

until the value being searched for is either found, or there are no more
8
1

elements to test. Here is the pseudocode for a function that performs a


1-
-1

binary search on an array:


25
20

161
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
Binary Search pseudocode:

‫ﺭﺓ‬
1. set first index to 0.

‫ﻭ‬
‫ﺗﻜ‬
2. set last index to the last subscript in the array.

‫ﻨ‬
‫ﻮﻟ‬
3. set found to false.

‫ﻮ‬
‫ﺟﻴﺎ‬
4. set position to -1.

‫ﺍﻟ‬
5. while found is not true and first is less than or equal to last

‫ﻤﻌ‬
set middle to the subscript halfway between array(first) and array(last).

‫ﻠﻮ‬
‫ﻣﺎ‬
if array[middle] equals the desired value

‫ﺕ‬
set found to true.

‫ﺑﻜ‬
‫ﻔﺮ‬
set position to middle.
else if array[middle] is greater than the desired value

‫ﺍﻟ‬
‫ﺸﻴ‬
set last to middle -1.

-‫ﺦ‬
else

76

‫ﻤﻌ‬
set first to middle + 1.
42

‫ﻬﺪ‬
end if.
-2

‫ﺍﻟ‬
end while.

‫ﻌﺎﻟ‬
18

6. return position

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
The algorithm shown above uses three index variables: first, last, and

‫ﺭﺓ‬
25

‫ﻭ‬
20

middle. The first and last variables mark the boundaries of the portion of

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
the array being searched. They are initialized with the subscript of the

‫ﺟﻴ‬
array's first and last elements. The subscript of the element half way ‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

between first and last is calculated and stored in the middle variable. If the
‫ﻣﺎ‬
‫ﺕ‬

element in the middle of the array does not contain the search value, the
‫ﺑﻜ‬
‫ﻔﺮ‬

first or last variable are adjusted so that only top or bottom half of the
‫ﺍﻟ‬
‫ﺸﻴ‬

array is searched the during the next iteration. This cuts the portion of the
-‫ﺦ‬

array being searched in half each time the loop fails to locate the search
76
42

value.
-2
18

Example : If you have the following sorted array:


1-
-1
25

{11, 15, 17, 18, 22, 29, 32, 36, 37}


20

Search using binary search algorithm for the value of 36

162
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
Beg = 0
22 36

‫ﻨ‬
‫ﻮﻟ‬
End = 8 Beg = MID +1

‫ﻮ‬
A(4) < item

‫ﺟﻴﺎ‬
MID=((0+8)/2) = 4

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
Beg = 5 32 36

‫ﺍﻟ‬
End = 8 Beg = MID +1

‫ﺸﻴ‬
A(6) < item

-‫ﺦ‬
MID=((5+8)/2) = 6.5 = 6

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
Beg = 7
36 36
1-

‫ﻟﻺ‬
End = 8 Loc = MID =7
-1

‫ﺩﺍ‬
A(7) = item

‫ﺭﺓ‬
25

MID=((7+8)/2) = 7.5 = 7

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

Searching for the value of 19 in the same array:


‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

Beg = 0
76

22 19
End = 8 End = MID -1
42

A(4) > item


MID=((0+8)/2) = 4
-2
1 8
1-
-1
25
20

Beg = 0 15 19
End = 3 Beg = MID +1
A(1) < item
MID=((0+3)/2) = 1

163
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
Beg = 2
17 19

‫ﻨ‬
‫ﻮﻟ‬
End = 3 Beg = MID +1

‫ﻮ‬
A(2) < item

‫ﺟﻴﺎ‬
MID=((2+3)/2) = 2

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
Beg = 3
18 19
End = 3 Beg = MID +1
76

‫ﻤﻌ‬
A(3) < item
42
MID=((3+3)/2) = 3

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
Beg = 4

‫ﻨﻮ‬
Beg > End Search Failed

‫ﻟﻮ‬
End = 3

‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬

The function binarySearch in the following example C++ code is used


‫ﻔﺮ‬
‫ﺍﻟ‬

to perform a binary search on an integer array. The first parameter,


‫ﺸﻴ‬
-‫ﺦ‬

array, which has size elements, is searched for an occurrence of the


76

number stored in value. If the number is found, its array subscript is


42
-2

returned. Otherwise, -1 is returned indicating the value did not appear in


8

the array.
1
1-
-1

int binarySearch(const int array[], int size, int value)


25

{
20

int first = O, // First array element


last = size - 1, // Last array element
middle, // Midpoint of search
position = -1; //Position of search value
bool found = false; // Flag
while (!found && first <= last)
164
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
{

‫ﺩﺍ‬
‫ﺭﺓ‬
middle = (first + last) I 2; // Calculate midpoint

‫ﻭ‬
if (array[middle] == value) // If value is found at mid

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
{

‫ﻮ‬
‫ﺟﻴﺎ‬
found = true;
position = middle;

‫ﺍﻟ‬
‫ﻤﻌ‬
}

‫ﻠﻮ‬
‫ﻣﺎ‬
else if (array[middle] > value) // If value is in lower half

‫ﺕ‬
last = middle - 1;

‫ﺑﻜ‬
else

‫ﻔﺮ‬
first = middle + 1·' II If value is in upper half

‫ﺍﻟ‬
‫ﺸﻴ‬
}

-‫ﺦ‬
return position;

76
}

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

Program 6-2 is a complete program using the binarySearch function. It

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

searches an array of employee ID numbers for a specific value.

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
1 // This program demonstrates the binarySearch function, which

‫ﻟﻮ‬
‫ﺟﻴ‬
2 // performs a binary search on an integer array.
3 #include <iostream> ‫ﺎﺍ‬
‫ﻟ‬
‫ﻤﻌ‬

4 using namespace std;


‫ﻠﻮ‬
‫ﻣﺎ‬

5
‫ﺕ‬

6 // Function prototype
‫ﺑﻜ‬

7 int binarySearch(const int [], int, int);


‫ﻔﺮ‬

8 const int SIZE = 20;


‫ﺍﻟ‬
‫ﺸﻴ‬

9
-‫ﺦ‬

10 int main()
76

11 {
42

12 // Array with employee IDs sorted in ascending order.


-2

13 int idNums[SIZE] = {101, 142, 147, 189, 199, 207, 222,


8

14 234, 289, 296, 310, 319, 388, 394,


1
1-

15 417, 429, 447, 521, 536, 600};


-1

16 int results; // To hold the search results


25

17 int empID; // To hold an employee ID


20

18
19 // Get an employee ID to search for.
20 cout << "Enter the employee ID you wish to search for: ";
21 cin >> empID;
22

165
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
23 // Search for the ID.

‫ﺩﺍ‬
‫ﺭﺓ‬
24 results = binarySearch(idNums, SIZE, empID);

‫ﻭ‬
25

‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
26 // If results contains −1 the ID was not found.

‫ﻮ‬
‫ﺟﻴﺎ‬
27 if (results == −1)
28 cout << "That number does not exist in the array. \n";

‫ﺍﻟ‬
‫ﻤﻌ‬
29 else

‫ﻠﻮ‬
‫ﻣﺎ‬
30 {

‫ﺕ‬
31 // Otherwise results contains the subscript of

‫ﺑﻜ‬
32 // the specified employee ID in the array.

‫ﻔﺮ‬
33 cout << "That ID is found at element " << results;

‫ﺍﻟ‬
‫ﺸﻴ‬
34 cout << " in the array.\n";

-‫ﺦ‬
35 }

76
36 return 0;

‫ﻤﻌ‬
42

‫ﻬﺪ‬
37 }
-2

‫ﺍﻟ‬
//**********************************************************

‫ﻌﺎﻟ‬
18

40 // The binarySearch function performs a binary search on an *

‫ﻰ‬
1-

‫ﻟﻺ‬
41 // integer array. array, which has a maximum of size elements, *
-1

‫ﺩﺍ‬
42 // is searched for the number stored in value. If the number is *

‫ﺭﺓ‬
25

43 // found, its array subscript is returned. Otherwise, −1 is *

‫ﻭ‬
20

‫ﺗﻜ‬
44 // returned indicating the value was not in the array. *

‫ﻨﻮ‬
‫ﻟﻮ‬
45

‫ﺟﻴ‬
//**********************************************************
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
47 int binarySearch(const int array[], int size, int value)
‫ﻠﻮ‬

48 {
‫ﻣﺎ‬
‫ﺕ‬

49 int first = 0, // First array element


‫ﺑﻜ‬

50 last = size − 1, // Last array element


‫ﻔﺮ‬

51 middle, // Midpoint of search


‫ﺍﻟ‬
‫ﺸﻴ‬

52 position = −1; // Position of search value


-‫ﺦ‬

53 bool found = false; // Flag


54
76

55 while (!found && first <= last)


42

56 {
-2

57 middle = (first + last) / 2; // Calculate midpoint


8
1

58 if (array[middle] == value) // If value is found at mid


1-
-1

59 {
25

60 found = true;
20

61 position = middle;
62 }
63 else if (array[middle] > value) // If value is in lower half
64 last = middle − 1;
65 else
66 first = middle + 1; // If value is in upper half
166
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
67 }

‫ﺩﺍ‬
‫ﺭﺓ‬
68 return position;

‫ﻭ‬
69 }

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
‫ﻮ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
‫ﻤﻌ‬
Program Output with Example Input Shown in Bold

‫ﻠﻮ‬
‫ﻣﺎ‬
Enter the employee ID you wish to search for: 199[Enter]

‫ﺕ‬
‫ﺑﻜ‬
ID 199 was found in element 4 of the array.

‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
8.7 Efficiency of the Binary Search

-‫ﺦ‬
76

‫ﻤﻌ‬
Obviously, the binary search is much more efficient than the linear
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
search. Every time it makes a comparison and fails to find the desired

‫ﻌﺎﻟ‬
18

‫ﻰ‬
item, it eliminates half of the remaining portion of the array that must be
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
searched. For example, consider an array with 1000 elements. If the binary

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
search fails to find an item on the first attempt, the number of elements

‫ﻨﻮ‬
‫ﻟﻮ‬
that remains to be searched are [Link] the item is not found on the second

‫ﺟﻴ‬
‫ﺎﺍ‬
attempt, the number of elements that remains to searched is [Link]
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

process continues until the binary search has made no more than 10
‫ﻣﺎ‬
‫ﺕ‬

comparisons. (Compare this to the linear search, which would make an


‫ﺑﻜ‬
‫ﻔﺮ‬

average of 500 comparisons!)


‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

Powers of 2 are used to calculate the maximum number of comparisons


76

the binary search will make on an array of any size.)A power of 2 is 2


42
-2

raised to the power of some number.) Simply find the smallest power of 2
18

that is greater than or equal to the number of elements in the array. For
1-
-1

example, a maximum of 16 comparisons will be made on an array of


25
20

50,000 elements (216=65,536), and a maximum of 20 comparisons will be


made on an array of 1,000,000 elements (220=1,048,576).

167
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
8.8 Sorting Algorithms

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
Sorting algorithms are used to arrange data into some order. Oftentimes

‫ﻨ‬
‫ﻮ‬‫ﻮﻟ‬
the data in an array must be sorted in some order. Customer lists, for

‫ﺟﻴﺎ‬
‫ﺍﻟ‬
instance, are commonly sorted in alphabetical order. Student grades might

‫ﻤﻌ‬
‫ﻠﻮ‬
be sorted from highest to lowest. Product codes could be sorted so all the

‫ﻣﺎ‬
‫ﺕ‬
products of the same color are stored together. To sort data in an array,

‫ﺑﻜ‬
‫ﻔﺮ‬
programmers must use an appropriate sorting algorithm. A sorting

‫ﺍﻟ‬
‫ﺸﻴ‬
algorithm is a technique for scanning through an array and rearranging its

-‫ﺦ‬
76
contents in some specific order. This section will introduce two simple

‫ﻤﻌ‬
42

‫ﻬﺪ‬
sorting algorithms: bubble sort and the selection sort.
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
8.9 Bubble Sort
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

The bubble sort is an easy way to arrange data in ascending or

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
descending order. If an array is sorted in ascending order, it means the

‫ﻟﻮ‬
‫ﺟﻴ‬
values in the array are stored from lowest to highest. If the values are
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
sorted in descending order, they are stored from highest to lowest. let's see
‫ﻠﻮ‬
‫ﻣﺎ‬

how the bubble sort is used in arranging the following array's elements in
‫ﺕ‬
‫ﺑﻜ‬

ascending order:
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76

7 2 3 8 9 1
42

Element Element Element Element Element Element


-2

0 1 2 3 4 5
1 8
1-
-1
25

The bubble sort starts by comparing the first two elements in the array.
20

If element 0 is greater than element 1, they are exchanged. After the


exchange, the array shown above would appear as:
2 7 3 8 9 1

168
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
Element Element Element Element Element Element

‫ﺩﺍ‬
‫ﺭﺓ‬
0 1 2 3 4 5

‫ﻭ‬
‫ﺗﻜ‬
This method is repeated with elements 1 and 2. If an element 1 is

‫ﻨ‬
‫ﻮ‬‫ﻮﻟ‬
‫ﺟﻴﺎ‬
greater than element 2, they are exchanged. The array above would then

‫ﺍﻟ‬
‫ﻤﻌ‬
appear as:

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
2 3 7 8 9 1

‫ﺍﻟ‬
‫ﺸﻴ‬
Element Element Element Element Element Element

-‫ﺦ‬
0 1 2 3 4 5

76

‫ﻤﻌ‬
Next, elements 2 and 3 are compared. In the array above, these two
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
elements are already in the proper order (element 2 is less than element 3),

‫ﻌﺎﻟ‬
18

‫ﻰ‬
so on exchange takes place.
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

As the cycle continues, elements 3 and 4 are compared. Once again, no

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
exchange is necessary because they are already in the proper order.

‫ﻟﻮ‬
‫ﺟﻴ‬
When elements 4 and 5 are compared, however, an exchange must take
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

place because element 4 is greater than element 5. The array now appears
‫ﻣﺎ‬
‫ﺕ‬

as:
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

2 3 7 8 1 9
76
42

Element Element Element Element Element Element


-2

0 1 2 3 4 5
8

At this point, the entire array has been scanned, but its contents aren’t
1
1-
-1

quite in the right order yet. So, the sort starts over again with elements 0
25
20

and 1. Since they are in the proper order, no exchange takes place.
Elements 1 and 2 are compared next, but once again, no exchange takes
place. This continues until elements 3 and 4 are compared. Since element
3 is greater than element 4, they are exchanged. The array now appears as:

169
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
2 3 7 1 8 9

‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
Element Element Element Element Element Element

‫ﻮ‬
‫ﺟﻴﺎ‬
0 1 2 3 4 5

‫ﺍﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
By now you should see how the sort will eventually cause the elements

‫ﻣﺎ‬
‫ﺕ‬
to appear in the correct order. The sort repeatedly passes through the array

‫ﺑﻜ‬
‫ﻔﺮ‬
in the fashion described above until no exchanges are made. Ultimately,

‫ﺍﻟ‬
‫ﺸﻴ‬
the array will appear as.

-‫ﺦ‬
76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
1 2 3 7 8 9

‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
Element Element Element Element Element Element
-1

‫ﺩﺍ‬
0 1 2 3 4 5

‫ﺭﺓ‬
25

‫ﻭ‬
20

Here is the bubble sort in pseudocode:

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
Bubble Sort pseudocode:
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬

Do
‫ﻠﻮ‬
‫ﻣﺎ‬

set swap flag to false.


‫ﺕ‬

for count is set to each subscript in array from 0 through the next-to-last
‫ﺑﻜ‬

subscript
‫ﻔﺮ‬

if array[count] is greater than array[count+1]


‫ﺍﻟ‬
‫ﺸﻴ‬

swap the contents of array[count] and array[count+1].


-‫ﺦ‬

set swap flag to true.


76

end if.
42

end for.
-2

while any elements have be swapped


18
1-
-1

The C++ code below implements the bubble sort as a function. The
25

parameter array is an integer array to be sorted. size contains the number


20

of elements in array.

void sortArray(int array[], int size)

170
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
{

‫ﺩﺍ‬
‫ﺭﺓ‬
bool swap;

‫ﻭ‬
int temp;

‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
do

‫ﻮ‬
‫ﺟﻴﺎ‬
{
swap = false;

‫ﺍﻟ‬
‫ﻤﻌ‬
for (int count = 0; count < (size − 1); count++)

‫ﻠﻮ‬
‫ﻣﺎ‬
{

‫ﺕ‬
if (array[count] > array[count + 1])

‫ﺑﻜ‬
{

‫ﻔﺮ‬
temp = array[count];

‫ﺍﻟ‬
‫ﺸﻴ‬
array[count] = array[count + 1];

-‫ﺦ‬
array[count + 1] = temp;

76
swap = true;

‫ﻤﻌ‬
42

‫ﻬﺪ‬
}
-2

‫ﺍﻟ‬
}

‫ﻌﺎﻟ‬
18

}while (swap);

‫ﻰ‬
1-

‫ﻟﻺ‬
}
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Let's look more closely at the for loop that handles the comparisons and

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
exchanges during a pass. Here is its starting line:

‫ﻟﻮ‬
‫ﺟﻴ‬
for (int count = O; count < (size - l); count++) ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬

The variable count holds the array subscripts. It starts at zero and is
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬

incremented as long as it is less than size - 1. The value of size is the


‫ﺑﻜ‬

number of elements in the array, and count stops just short of reaching
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

this value because the following line compares each element with the one
-‫ﺦ‬

after it:
76

if (array[count] > array[count + l])


42
-2

When array [count] is the next-to-last element, it will be compared to the


18

last element. If the for loop were allowed to increment count past size - 1,
1-
-1

the last element in the array would be compared to a value outside the
25
20

array.
Here is the if statement in its entirety:
if (array[count] > array[count + l])
{

171
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
temp = array[count];

‫ﺩﺍ‬
‫ﺭﺓ‬
array[count] = array[count + l];

‫ﻭ‬
‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
array[count + l] = temp;

‫ﻮ‬
‫ﺟﻴﺎ‬
swap = true;

‫ﺍﻟ‬
‫ﻤﻌ‬
}

‫ﻠﻮ‬
‫ﻣﺎ‬
If array [count] is greater than array [count + 1], the two elements must

‫ﺕ‬
‫ﺑﻜ‬
be exchanged. First, the contents of array [count] is copied into the

‫ﻔﺮ‬
‫ﺍﻟ‬
variable temp. Then the contents of array [count + 1] is copied into array

‫ﺸﻴ‬
-‫ﺦ‬
[count]. The exchange is made complete when temp (which holds the

76

‫ﻤﻌ‬
previous contents of array [count]) is copied to array [count + 1]. Last,
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
the swap flag variable is set to true. This indicates that an exchange has

‫ﻌﺎﻟ‬
18

‫ﻰ‬
been made. Program 6-3 demonstrates the bubble sort function in a
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
complete program.

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
1 // This program uses the bubble sort algorithm to sort an

‫ﺟﻴ‬
2 / array in ascending order. ‫ﻟ‬‫ﺎﺍ‬
‫ﻤﻌ‬
3 #include <iostream>
‫ﻠﻮ‬

4 using namespace std;


‫ﻣﺎ‬
‫ﺕ‬

6 // Function prototypes
‫ﺑﻜ‬

7 void sortArray(int [], int);


‫ﻔﺮ‬

8 void showArray(const int [], int);


‫ﺍﻟ‬
‫ﺸﻴ‬

10 int main()
-‫ﺦ‬

11 {
12 // Array of unsorted values
76

13 int values[6] = {7, 2, 3, 8, 9, 1};


42

14
-2

15 // Display the values.


81

16 cout << "The unsorted values are: \n";


1-
-1

17 showArray(values, 6);
25

18
20

19 // Sort the values.


20 sortArray(values, 6);
21
22 // Display them again.
23 cout << "The sorted values are:\n";
24 showArray(values, 6);
172
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
25 return 0;

‫ﺩﺍ‬
‫ﺭﺓ‬
26 }

‫ﻭ‬
27

‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
//**********************************************************

‫ﻮ‬
‫ﺟﻴﺎ‬
29 // Definition of function sortArray *
30 // This function performs an ascending order bubble sort on *

‫ﺍﻟ‬
‫ﻤﻌ‬
31 // array. size is the number of elements in the array. *

‫ﻠﻮ‬
‫ﻣﺎ‬
32

‫ﺕ‬
//**********************************************************

‫ﺑﻜ‬
33

‫ﻔﺮ‬
34 void sortArray(int array[], int size)

‫ﺍﻟ‬
‫ﺸﻴ‬
35 {

-‫ﺦ‬
36 bool swap;

76
37 int temp;

‫ﻤﻌ‬
42

‫ﻬﺪ‬
38
-2

‫ﺍﻟ‬
39 do

‫ﻌﺎﻟ‬
18

40 {

‫ﻰ‬
1-

‫ﻟﻺ‬
41 swap = false;
-1

‫ﺩﺍ‬
42 for (int count = 0; count < (size − 1); count++)

‫ﺭﺓ‬
25

43 {

‫ﻭ‬
20

‫ﺗﻜ‬
44 if (array[count] > array[count + 1])

‫ﻨﻮ‬
‫ﻟﻮ‬
45 {

‫ﺟﻴ‬
46 temp = array[count]; ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
47 array[count] = array[count + 1];
‫ﻠﻮ‬

48 array[count + 1] = temp;
‫ﻣﺎ‬
‫ﺕ‬

49 swap = true;
‫ﺑﻜ‬

50 }
‫ﻔﺮ‬

51 }
‫ﺍﻟ‬
‫ﺸﻴ‬

52 } while (swap);
-‫ﺦ‬

53 }
54
76

//**********************************************************
42

56 // Definition of function showArray. *


-2

57 // This function displays the contents of array. size is the *


18

58 // number of elements. *
1-
-1

59
25

//**********************************************************
20

60
61 void showArray(const int array[], int size)
62 {
63 for (int count = 0; count < size; count++)
64 cout << array[count] << " ";
65 cout << endl;
173
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
66 }

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
Program Output

‫ﻮ‬
‫ﺟﻴﺎ‬
The unsorted values are:
723891

‫ﺍﻟ‬
‫ﻤﻌ‬
The sorted values are:

‫ﻠﻮ‬
‫ﻣﺎ‬
123789

‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
8.10 The selection sort

‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
The bubble sort is inefficient for large arrays because items only move

76

‫ﻤﻌ‬
by one element at a time. The selection sort, however usually performs
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
fewer exchanges because it moves items immediately to their final

‫ﻌﺎﻟ‬
18

‫ﻰ‬
position in the array. Basically, it works like this: the smallest value in
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
array is located and moved to element [Link] the next smallest value is

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
located and moved to element 1. This process continues until all of the

‫ﻨﻮ‬
‫ﻟﻮ‬
elements have been placed in their proper order.

‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
Let's see how the selection sort works when arranging the elements of
‫ﻠﻮ‬
‫ﻣﺎ‬

the following array.


‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬

5 7 2 8 9 1
-‫ﺦ‬
76

Element Element Element Element Element Element


42

0 1 2 3 4 5
-2
18

The Selection sort scans the array, starting at element 0, and locates the
1-
-1

element with the smallest value. The contents of this element are then
25
20

swapped with the contents of elements [Link] the example above, the 1
stored in element 5 is swapped with the 5 stored in element [Link] the
exchange, the array would appear as :

174
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
1 7 2 8 9 5

‫ﺭﺓ‬
‫ﻭ‬
Element Element Element Element Element Element

‫ﺗﻜ‬‫ﻨ‬
0 1 2 3 4 5

‫ﻮ‬‫ﻮﻟ‬
‫ﺟﻴﺎ‬
‫ﺍﻟ‬
The algorithm then repeats the process, but since element 0 already

‫ﻤﻌ‬
‫ﻠﻮ‬
contains the smallest value in the array, it can be left out of the procedure.

‫ﻣﺎ‬
‫ﺕ‬
This time, the algorithm begins the scan at element 1.

‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
In the example above, the contents of elements 2 are exchanged with

‫ﺸﻴ‬
-‫ﺦ‬
that of element 1. The array would then appear as:

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

1 2 7 8 9 5

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
Element Element Element Element Element Element

‫ﺭﺓ‬
25

0 1 2 3 4 5

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
Once again the process is repeated, but this time the scan begins at

‫ﺟﻴ‬
element 2. The algorithm will find that element 5 contains the next
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

smallest value. This element's content is exchanged with that of element 2,


‫ﻣﺎ‬
‫ﺕ‬

causing the array to appear as:


‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬

1 2 5 8 9 7
76
42

Element Element Element Element Element Element


-2

0 1 2 3 4 5
1 8
1-

Next, the scanning begins at element 3. Its content is exchanged with


-1
25

that of element 5, causing the array to appear as:


20

1 2 5 7 9 8
Element Element Element Element Element Element

175
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
0 1 2 3 4 5

‫ﺩﺍ‬
‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬
At this point there are only two elements left to sort. The algorithm

‫ﻨ‬
‫ﻮ‬‫ﻮﻟ‬
‫ﺟﻴﺎ‬
finds that the value in element 5 is smaller than that of element 4, so the

‫ﺍﻟ‬
‫ﻤﻌ‬
two are swapped. This puts the array in its final arrangement:

‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
1 2 5 7 8 9

‫ﺍﻟ‬
‫ﺸﻴ‬
Element Element Element Element Element Element

-‫ﺦ‬
0 1 2 3 4 5

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
Here is the Selection sort algorithm in pseudocode:
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

Selection Sort pseudocode:

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

for startScan is set to each subscript in array from 0 through the next-to-last

‫ﻭ‬
20

‫ﺗﻜ‬
subscript.

‫ﻨﻮ‬
set index variable to startScan.

‫ﻟﻮ‬
Here issetthe
minIndex
selectionto startScan.
sort algorithm is pseudocode:
‫ﺟﻴ‬
set minValue to array[startScan]. ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
for index is set to each subscript in array from (startScan +1) through the
‫ﻠﻮ‬

last subscript
‫ﻣﺎ‬

if array[index] is less than minValue


‫ﺕ‬

set minValue to array[index].


‫ﺑﻜ‬

set minIndex to index.


‫ﻔﺮ‬

endif.
‫ﺍﻟ‬
‫ﺸﻴ‬

end for.
-‫ﺦ‬

set array[minIndex] to array[startScan].


set array[startScan] to minValue.
76

end for.
42
-2

The following function uses the selection sort to arrange the values in an
1 8

integer array in ascending order. It accepts two arguments. The first


1-

parameter, array, receives the array to be sorted and the second, size,
-1
25

indicates how many values are stored in the array.


20

void selectionSort(int array[], int size)


{
int startScan, minindex, minValue;
for (startScan = O; startScan < (size - l); startScan++)

176
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
{

‫ﺩﺍ‬
‫ﺭﺓ‬
minindex = startScan;

‫ﻭ‬
minValue = array[startScan];

‫ﺗﻜ‬
‫ﻨ‬
‫ﻮﻟ‬
for (int index = startScan + 1; index < size; index++)

‫ﻮ‬
‫ﺟﻴﺎ‬
{
if (array[index] < minValue)

‫ﺍﻟ‬
‫ﻤﻌ‬
{

‫ﻠﻮ‬
‫ﻣﺎ‬
minValue = array[index];

‫ﺕ‬
minindex = index;

‫ﺑﻜ‬
array[minindex] = array[startScan];

‫ﻔﺮ‬
array[startScan] = minValue;

‫ﺍﻟ‬
‫ﺸﻴ‬
}

-‫ﺦ‬
}

76
}

‫ﻤﻌ‬
42

‫ﻬﺪ‬
}
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
As with bubble sort, selection sort uses a pair of nested loops, in this
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
case two for loops. The inner loop sequences through the array, starting at

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
array [start Scan + 1], searching for the element with the smallest value.

‫ﻨﻮ‬
‫ﻟﻮ‬
When the element is found, its subscript is stored in the variable minindex,

‫ﺟﻴ‬
‫ﺎﺍ‬
and its value is stored in minValue. The outer loop then exchanges the
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

contents of this element with array [ startScan] and increments startScan.


‫ﻣﺎ‬
‫ﺕ‬

This procedure repeats until the contents of every element have been
‫ﺑﻜ‬
‫ﻔﺮ‬

moved to their proper location. For N pieces of data this requires N-1
‫ﺍﻟ‬
‫ﺸﻴ‬

passes. Program 6-4 demonstrates the selection sort function in a


-‫ﺦ‬

complete program.
76
42
-2

1 //This program uses the selection sort algorithm to sort


8

2 // an array in ascending order.


1
1-

3 #include <iostream>
-1

4 using namespace std;


25

5
20

6 Function prototypes
7 void selectionSort(int [], int);
8 void showArray(const int [], int);
9
10 int main()

177
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
11 {

‫ﺩﺍ‬
‫ﺭﺓ‬
12 canst int SIZE = 6;

‫ﻭ‬
13

‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
14 // Array of unsorted values

‫ﻮ‬
‫ﺟﻴﺎ‬
15 int values[SIZE] = {5, 7, 2, 8, 9, l};
16

‫ﺍﻟ‬
‫ﻤﻌ‬
17 // Display the values

‫ﻠﻮ‬
‫ﻣﺎ‬
18 cout << '' The unsorted values are\n '';

‫ﺕ‬
19 showArray(values, SIZE);

‫ﺑﻜ‬
20

‫ﻔﺮ‬
21 // Sort the array

‫ﺍﻟ‬
‫ﺸﻴ‬
22 selectionSort(values, SIZE);

-‫ﺦ‬
23

76
24 // Display the values again

‫ﻤﻌ‬
42

‫ﻬﺪ‬
25 cout << ''The sorted values are\n'';
-2

‫ﺍﻟ‬
26 showArray(values, SIZE);

‫ﻌﺎﻟ‬
18

27 return O;

‫ﻰ‬
1-

‫ﻟﻺ‬
28 }
-1

‫ﺩﺍ‬
29

‫ﺭﺓ‬
25

30

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
/***********************************************************

‫ﺟﻴ‬
31 * selectionSort * ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
32 * This function performs an ascending-order selection sort *
‫ﻠﻮ‬

33 * on array. The parameter size holds the number of elements *


‫ﻣﺎ‬
‫ﺕ‬

34 * in the array. *
‫ﺑﻜ‬
‫ﻔﺮ‬

35******************************************************** /
‫ﺍﻟ‬
‫ﺸﻴ‬

36 void selectionSort(int array[], int size)


-‫ﺦ‬

37 {
38 int startScan, minindex, minValue;
76

39
42

40 for (startScan = O; startScan < (size - l); startScan++)


-2

41 {
8
1

42 minindex = startScan;
1-
-1

43 minValue = array[startScan];
25

44 for(int index = startScan + 1; index < size; index++)


20

45 {
46 if (array[index] < minValue)
47 {
48 minValue = array[index];
49 minindex = index;
50 }
178
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
51 }

‫ﺩﺍ‬
‫ﺭﺓ‬
52 array[minindex] = array[startScan];

‫ﻭ‬
53 array[startScan] = minValue;

‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
54 }

‫ﻮ‬
‫ﺟﻴﺎ‬
55 }
56

‫ﺍﻟ‬
‫ﻤﻌ‬
57 /********************************************************

‫ﻠﻮ‬
‫ﻣﺎ‬
58* showArray

‫ﺕ‬
59* This function displays the contents of array. The

‫ﺑﻜ‬
60* parameter size holds the number of elements in the array. *

‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
61 *******************************************************/

-‫ﺦ‬
62 void showArray(const int array[], int size)

76
63 {

‫ﻤﻌ‬
42

‫ﻬﺪ‬
64 for (int count = O; count < size; count++)
-2

‫ﺍﻟ‬
65 cout << array [count] << '' '' ;

‫ﻌﺎﻟ‬
18

66 cout << endl;

‫ﻰ‬
1-

‫ﻟﻺ‬
67 }
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

Program Output

‫ﻭ‬
20

‫ﺗﻜ‬
The unsorted values are

‫ﻨﻮ‬
‫ﻟﻮ‬
572891

‫ﺟﻴ‬
The sorted values are ‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
125789
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42
-2
8
1

[Q6] Review Questions:


1-
-1
25

Q1-Fill in the Blank


20

A. The --------- Search algorithm steps sequentially through an array,


comparing each item with the search value.

179
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
Chapter 8 Algorithm Analysis

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
B. The --------- search algorithm repeatedly divides the portion of an

‫ﺩﺍ‬
‫ﺭﺓ‬
array being searched in search algorithm is adequate for small

‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
arrays , but no large arrays.

‫ﻮ‬
‫ﺟﻴﺎ‬
C. --------- search algorithm requires that the array's contents be

‫ﺍﻟ‬
‫ﻤﻌ‬
sorted.

‫ﻠﻮ‬
‫ﻣﺎ‬
D. If an array is sorted in --------- order, the values are sorted from

‫ﺕ‬
‫ﺑﻜ‬
lowest to highest.

‫ﻔﺮ‬
‫ﺍﻟ‬
E. If an array is sorted in --------- order, the values are stored from

‫ﺸﻴ‬
-‫ﺦ‬
highest to lowest.

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
Q2- Identify true of false and rewrite the correct version of false one:
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
A. If data is sorted in ascending order, it means it is ordered from
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
lowest value to highest value.

‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
B. If data is sorted in descending order, it means it is ordered from

‫ﻨﻮ‬
‫ﻟﻮ‬
lowest value to highest value.

‫ﺟﻴ‬
‫ﺎﺍ‬
C. The average number of comparisons performed by the linear search
‫ﻟ‬
‫ﻤﻌ‬
‫ﻠﻮ‬

on an array of N elements is N/2(assuming the search values are


‫ﻣﺎ‬
‫ﺕ‬

consistently found).
‫ﺑﻜ‬
‫ﻔﺮ‬

D. The maximum number of comparisons performed by the linear


‫ﺍﻟ‬
‫ﺸﻴ‬

search on an array of N elements is N/n(assuming the search values


-‫ﺦ‬

are consistently found).


76
42
-2

Q3- Write the pusedocode for :


8
1
1-

- Linear search
-1

- Binary Search
25

- Bubble Sort
20

- Selection Sort

Q4-Trace the steps of bubble sort to arrange the following array elements
[7,2,3,8,9,1,4].

180
‫‪Chapter 8‬‬

‫]‪elements[7,2,3,8,9,1,4‬‬

‫‪20‬‬ ‫‪20‬‬
‫‪25‬‬ ‫‪25‬‬
‫‪-1‬‬ ‫‪-1‬‬
‫‪1-‬‬ ‫‪1-‬‬
‫‪1‬‬‫‪8‬‬ ‫‪18‬‬
‫‪-2‬‬ ‫‪-2‬‬

‫‪181‬‬
‫‪42‬‬ ‫‪42‬‬
‫‪76‬‬ ‫‪76‬‬
‫ﺦ‪-‬‬ ‫ﺦ‪-‬‬
‫ﺸﻴ‬ ‫ﺸﻴ‬
‫ﺍﻟ‬ ‫ﺍﻟ‬
‫ﻔﺮ‬ ‫ﻔﺮ‬
‫ﺑﻜ‬ ‫ﺑﻜ‬
‫ﺕ‬ ‫ﺕ‬
‫ﻣﺎ‬ ‫ﻣﺎ‬
‫ﻠﻮ‬ ‫ﻠﻮ‬
‫ﻤﻌ‬ ‫ﻤﻌ‬
‫ﻟ‬‫ﺎﺍ‬ ‫ﺍﻟ‬
‫ﺟﻴ‬ ‫ﺟﻴﺎ‬
‫ﻟﻮ‬ ‫ﻮ‬ ‫ﻮﻟ‬
‫ﻨﻮ‬ ‫ﻨ‬
‫ﺗﻜ‬ ‫ﺗﻜ‬
‫ﻭ‬ ‫ﻭ‬
‫‪Algorithm Analysis‬‬

‫‪Q5-Trace the steps of selection sort to arrange the following array‬‬

‫ﺭﺓ‬ ‫ﺭﺓ‬
‫ﺩﺍ‬ ‫ﺩﺍ‬
‫ﻟﻺ‬ ‫ﻟﻺ‬
‫ﻰ‬ ‫ﻰ‬
‫ﻌﺎﻟ‬ ‫ﻌﺎﻟ‬
‫ﺍﻟ‬ ‫ﺍﻟ‬
‫ﻬﺪ‬ ‫ﻬﺪ‬
‫ﻤﻌ‬ ‫ﻤﻌ‬
‫ﻤﻌ‬
‫ﻬﺪ‬
‫ﺍﻟ‬
References

‫ﻌﺎﻟ‬
‫ﻰ‬
‫ﻟﻺ‬
‫ﺩﺍ‬
References

‫ﺭﺓ‬
‫ﻭ‬
‫ﺗﻜ‬‫ﻨ‬
‫ﻮﻟ‬
1. Dale, N. B. (2003). C++ plus data structures. Jones & Bartlett

‫ﻮ‬
‫ﺟﻴﺎ‬
Learning.

‫ﺍﻟ‬
‫ﻤﻌ‬
2. Horowitz, E., Sahni, S., & Anderson-Freed, S. (1976).

‫ﻠﻮ‬
‫ﻣﺎ‬
Fundamentals of data structures (Vol. 1982). Potomac, MD:

‫ﺕ‬
‫ﺑﻜ‬
Computer science press.

‫ﻔﺮ‬
‫ﺍﻟ‬
3. Gaddis, T., Walters, J., & Muganda, G. (2013). Starting out with

‫ﺸﻴ‬
-‫ﺦ‬
C++: early objects. Addison-Wesley Professional.

76

‫ﻤﻌ‬
42

‫ﻬﺪ‬
-2

‫ﺍﻟ‬
‫ﻌﺎﻟ‬
18

‫ﻰ‬
1-

‫ﻟﻺ‬
-1

‫ﺩﺍ‬
‫ﺭﺓ‬
25

‫ﻭ‬
20

‫ﺗﻜ‬
‫ﻨﻮ‬
‫ﻟﻮ‬
‫ﺟﻴ‬
‫ﻟ‬ ‫ﺎﺍ‬
‫ﻤﻌ‬
‫ﻠﻮ‬
‫ﻣﺎ‬
‫ﺕ‬
‫ﺑﻜ‬
‫ﻔﺮ‬
‫ﺍﻟ‬
‫ﺸﻴ‬
-‫ﺦ‬
76
42
-2
8
1
1-
-1
25
20

182

Powered by TCPDF ([Link])

You might also like