Professional Documents
Culture Documents
PPT1 - Introduction To Data Structure
PPT1 - Introduction To Data Structure
• Additional Textbook
– Ellis Horowitz, Sartaj Sahni, Susan Anderson-Freed. 2007.
Fundamentals of Data Structures in C. COMSP. New
York. ISBN:978-0929306407
Course Description
• This course provides students with data structure basic
concept in which it will be frequently used in software
engineering and programming practices, concept of array,
structure, stack, queue, graph, and trees.
int x = 10;
• The two most important operators used with pointer type are:
data_type *ptr_name;
• For example:
int *pnum;
char *pch;
Float *pfnum;
• then &x returns the address of x and assigns it as the value of px.
int a = 10;
int *p = &a;
printf( “%d\n”, *p );
a = 17;
*p = 20;
printf( “%d\n”, a );
Array
• A collection of similar data elements that have the same data type
(homogenous)
• The elements of the array are stored in consecutive memory locations and are
referenced by an index (subscript)
• In C, array index starts from zero
• Arrays are declared using the following syntax :
• Declaration: Syntax:
int arr[5]; type name[size];
Syntax:
• Declaration: type name[size1][size2];
int arr[3][6];
• The first index of array ranged from 0 to
size1 – 1.
• Accessing: • The second index of array ranged from 0
arr[0][2] = 2; to size2 – 1.
arr[2][1] = 9;
arr[1][5] = 13;
arr[2][4] = 10;
Array Declaration &
Accessing Array
• Multi Dimensional Array
Syntax:
type name[size1][size2][size3][...];
• Declaration:
• The first index of array ranged from 0 to
int arr[4][3][7][10]; size1 – 1.
• The second index of array ranged from 0 to
• Accessing: size2 – 1.
• The third index of array ranged from 0 to
arr[0][2][2][9]= 2; size3 – 1.
• and so on.
arr[2][1][6][0]= 9;
arr[3][0][0][6]= 13;
arr[2][1][3][8]= 10;
Storing Array Values
• Inputting Values
Example: int i, marks[10];
for (i=0; i<10; i++)
scanf(“%d”, &marks[i]);
• Assigning Values
Example: int i, arr1[10], arr2[10];
for(i=0; i<10; i++)
arr2[i] = arr1[i];
Calculating the Address
of Array Elements
• The array name is a symbolic reference for the address to the first byte
of the array.
• When we use the array name, we are actually referring to the first byte
of the array.
• Since an array stores all its data elements in consecutive memory
location, storing just the base address (the first element in the array) is
sufficient.
• The formula to perform this calculation is:
A[k] = BA (A) + w * k
A is the array
k is the index of the element
BA is the base address of the array A
w is the word size of one element in memory (example, size of int is 4)
Operations in Array
• There are a number of operations that can be performed on
arrays.
• They are:
Traversal
Insertion
Searching
Deletion
Merging
Sorting
Data Structure
• A data structure is an arrangement of data, either in the
computer’s memory or on the disk storage.
• Some common examples of data structures include:
Arrays
Linked lists
Queues
Stacks
Binary trees
Hash tables
Types of Data
Structure
• Arrays
A collection of similar data elements
Data elements have the same data type
Types of Data
Structure
• Linked Lists
A very dynamic data structure in which the elements can be
added to or deleted from anywhere at will
Each element is called a node
node
Types of Data
Structure
• Queue
The element that was inserted first is the first one to be taken
out
The elements in a queue are added at one end called the rear
and removed from the other end called the front
Types of Data
Structure
• Stacks
Stacks can be represented as a linear array
Every stack has a variable TOP associated with it
LIFO (Last In First Out) / FILO (First In Last Out)
Types of Data
Structure
• Binary Trees
A data structure which is defined as a collection of elements
called the nodes
Every node contains a left pointer, a right pointer, and a data
element
Data Type
• Data Type is a collection of objects and a set of operations that
act on those objects.
• C/C++ has a concept called class and struct which assist the
programmer in implementing abstract data type.
Example of ADT
• Supposed we want to create an ADT of natural number which has
integer number as objects and several functions as operation.
• structure Number is
objects : an integer x
functions :
bool is_zero() if ( x == 0 ) return TRUE else return FALSE
bool equal(y) if ( x == y ) return TRUE else return FALSE
void set(y) x = y
void add(y) x = x + y
int get () return x
Structure
• Structure is basically a user-defined data type that can store
related information (even of different data types) together,
while an array can store only entities of same data types.
struct tdata {
int age;
char name[100]; structure member
float score;
};
• The code above defines a structure named tdata which has three
members: age (int), name (char[]) and score (float).
• Creating a variable of structure is similar to create a variable of
primitive data type.
• tdata x; // a variable of tdata
• tdata arr[100];// an array of tdata
Structure
Declaration
• You also can define a structure as well as declare variables.
x.age = 17;
strcpy(x.name, “andi”);
x.score = 82.5;
Nested Structure
• You also can have a structure as a member of another structure
struct profile {
student x;
int age;
char name[100]; x.score = 92;
}; x.grade = ‘A’;
x.p.age = 20;
strcpy(x.p.name,”budi”);
struct student {
struct profile p;
int score;
char grade;
};
Array of Structure
• You also can have an array of structure.