An Introduction To Data Structure
An Introduction To Data Structure
هياكل البيانات
المحاضرة االولى وجدان ياسين.م
• A data structure is said to be linear if its elements form a sequence, or, in other words, a linear
list. There are two basic ways of representing such linear structures in memory. these linear
structures are called Arrays and Linked List.
The operations that normally performs on any linear structure, whether it be an array or a
linked list, include the following:
1. Traversing: means to visit all the elements of the array in an operation is called traversing.
2. Sorting: Re-arrangement of values in a list in a specific order
3. Searching: The process of finding the location of a particular element in a list is called
searching.
4. Insertion: Adding a new element to the list.
1
5. Deletion: Removing an element from the list.
6. Merging: Combining two lists into a single list
1- Array:
Array classified in C++ in three category:
1. One – Dimensional Arrays.
2. Two – Dimension Arrays.
3. Multidimensional Arrays.
• The address of the first location is called the base address of the array and is denoted by base
(BA) and the rest of the array elements come after this address.
• Computer does not need to keep track of the address of every array element, but need to track
only the address of the first element of the array Base Address (BA) and to reach to any array
element and the compiler use the following formula to do so.
Loc ( N [I] ) = BA + ( I ) × Size
• Loc N[I] : The location of the element I, BA: Fixed base address, Size: A fixed constant, is
also known as size of the data type.
Example : Consider an one dimension array (N) with size 10 and the base address equal (3002)
and each element of the array occupy 1 byte. find the address the element number six.
Loc ( N [I] ) = BA + ( I ) × Size
then, Loc ( N [5] ) = 3002 + ( 5 ) × 1
Loc ( N [5] ) =3007.
2
الصيغة العامة لإلعالن عن المصفوفة ذات البعد الواحد:
حيث:-
:indexدليل المصفوفة ) عدد عناصرها ( وهي عبارة عن قيمة صحيحة يمكن أن تكون محدده)ثابتة (أو مدخلة من قبل
المستخدم)متغيرة( .
أمثلة:
;] 1. int x [ 50 إعالن عن مصفوفة حجمها 50وبياناتها من النوع الصحيح
;] 2. float y [ 20 إعالن عن مصفوفة حجمها 20وبياناتها من النوع الحقيقي
; ] 3. char name [ 15 إعالن عن مصفوفة حجمها 15وبياناتها من النوع الحرفي
; }4. int x [ 3 ] = { 5,10,15 إعالن عن مصفوفة حجمها 3وبياناتها من النوع الصحيح مع إعطائها قيم ابتدائية
إعالن عن مصفوفة حجمها 4وبياناتها من النوع الحرفي مع إعطائها قيم ابتدائية ; } '5. char y [ 4 ] = { 'a','b','c','d
3
Program -1-: Write C++ program to read one-dimensional array consisting of 10
elements, then give the summation for these elements and print it :
#include <iostream>
using namespace std;
int main ()
{
int a[10];
int sum, i;
sum=0;
for(i=0;i<10;i++)
{
cin>>a[i];
sum +=a[i];
}
cout<< "sum=" << sum <<"\n";
return 0;
}
Program-2: Write a program to read one-dimensional array has 5 real values and
order them in ascending form and print them (?)ترتيب تصاعدي
#include <iostream>
using namespace std;
4
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
cout<<"elements after sorting\n";
for(i=0;i<n;i++)
cout<<x[i]<<"\n";
return 0;
}
Column-Major Order :
• Loc ( N [I][J] ) = BA + ( m × J + I ) × Size
• Where:
• Loc N[I][J] : the location of the element I,J.
• BA: fixed base address.
• m: number of row.
• Size: a fixed constant, is also known as size of the data type
5
Row-Major Order :
• Loc ( N [I][J] ) = BA + ( n × I + J ) × Size
• Where:
• Loc N[I][J] : the location of the element I,J.
• BA: fixed base address.
• n: number of column.
• Size: a fixed constant, is also known as size of the data type
Example : Consider a two dimension array (N) with size (m=3 × n= 5) and the base
address equal (600) and each element of the array occupy 1 byte. find the address to
the element N[2][3].Suppose the programming store 2D using Row-Major.
Loc ( N [I][J] ) = BA + ( n × I + J ) × Size
Loc ( N [2][3] ) = 600 + ( 5 × 2 + 3 ) × 1
Loc ( N [I][J] ) = 613
:أمثلة
1. int y[4][3] اعمدة3 صفوف و4 كمصفوفة ذات بعدين وتتكون منy اعالن عن المصفوفة الصحيحة
2. y[4][3]=23; y لعنصر الصف الرابع والعمود الثالث من المصفوفة23 إسناد القيمة
3. int y[4][3]={{5,0,-4},{-2,3,1},{4,7,6},{9,8,-1}}; اثناء التصريحy إسناد قيم ابتدائية لعناصر المصفوفة
#include <iostream>
using namespace std;
6
int main( ) {
int a[3][3],b[3][3],c[3][3], i, j;
cout<<"input first array a[3][3]:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
cout<<"\n";
}
cout<<"input second array b[3][3]:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>b[i][j];
cout<<"\n";
}
cout<<"print first array a[3][3] :\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
cout<<"the second array b[3][3] look like :\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<b[i][j]<<"\t";
cout<<"\n";
}
cout<<" sum of A & B arrays in C[3][3] :\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
cout<<c[i][j]<<"\t";
}
cout<<"\n";
return 0;}
7
H.W:
• H.W.1: Consider Int X[3][4] ,what is the address of the X[2][2] if the base address equal (300)
and each element of the array occupy 1 byte. Suppose the programming store 2D using Column-
Major.
• H.W.2: Consider Int X[5][3] ,what is the address of the X[3][2] if the base address equal (100)
and each element of the array occupy 2 byte. Suppose the programming store 2D using Row-
Major.
• H.W.3: Write C++ program to give the average for array consist of 5 real numbers.
• H.W.4:
: ثم عمل ما يليA(4x3) اكتب برنامجاً لقراءة عناصر مصفوفة
.طباعة المصفوفة بشكل ثنائيًمرتب- a
طباعة األرقام الموجبة و األرقام السالبة و األرقام الصفري ًة في المصفوف ًة-b
.طباعة عدد األرقام الزوجي ًة والفردية- c