Introduction To Data Structure
Introduction To Data Structure
Week 1
Introduction to Data Structure
TextBooks
• Main Textbook
• Reema Thareja, 2014
Data Structures Using C. Oxfort. New Delhi.
ISBN: 978-0-19-809930-7
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.
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 Tree
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
ARRAY
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];
• Accessing:
arr[0] = 7;
arr[1] = 2;
arr[2] = 13;
arr[3] = 13;
arr[4] = 13;
Array Declaration & Accessing
Array
Two Dimensional Array
• Declaration: Syntax:
int arr[3][6]; type name[size1][size2];
• Accessing:
arr[0][2] = 2;
arr[2][1] = 9;
arr[1][5] = 13;
arr[2][4] = 10;
Array Declaration & Accessing
Array
Multi Dimensional Array
• Declaration: Syntax:
int arr[4][3][7][10]; type name[size1][size2][size3][...];
• Accessing:
arr[0][2][2][9]= 2;
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];
Operations in Array
There are a number of operations that can be performed on arrays.
They are:
Insertion
Deletion
Searching
Traversal
Merging
Sorting
POINTER
Pointer Review
• Every variable in C has a name and a value associated with it, and
when a variable is declared, a specific block of memory within the
computer is allocated to hold the value (the size depends of the
data type).
For example:
int x = 10;
• The size of integer may vary from one system to another.
• In 32 bit systems, an integer variable is allocated 4 bytes while in 16
bit systems, it is allocated 2 bytes
Pointer
• Pointer is a variable that contains the memory location of another
variable.
• Therefore, a pointer is a variable that represents the location of a data
item such as a variable or an array element.
• The two most important operators used with pointer type are:
& the address operator
* The dereferencing operator
Pointer
Pointers are frequently used in C, as they have a number of useful
applications. Such as:
• Used to pass information back and forth between a function and its reference
point.
• Enable the programmers to return multiple data items from a function via
function arguments or to pass arrays and strings as function arguments
• Provide an alternate way to access the individual elements of an array
• Used to create complex data structures, such as trees, linked list, linked stack,
linked queue and graphs
• Used for the dynamic memory allocation of a variable
Pointer
• To declaring pointer variables can be given as below:
data_type *ptr_name;
• For example:
int *pnum;
char *pch;
Float *pfnum;
• In each of the above statements, a pointer variable is declared to point to a
variable of the specified data type.
• Although all these pointers point to different data types, they will occupy the
same amount of space in the memory (depends on the platform where the
code is going to run).
Pointer
• If we have the declaration: int x;
int *px;
• 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 );
Struct
Struct
• 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.
• It is a collection of variables under a single name.
• The variables within a structure are of different data types and each
has a name that is used to select it from the structure.
Struct Declaration
Struct
struct tdata {
int age;
char name[100];
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
Struct Declaration
• You also can define a structure as well as declare variables.
Struct Assignments
tdata x;
x.age = 17;
strcpy(x.name, “andi”);
x.score = 82.5;
Nested Struct
Nested Struct
• You also can have a structure as a member of another structure
Array of Struct
• You also can have an array of structure
REFERENCES
Thareja, R. (2014). Data Structures Using C (second). Oxford
University Press. https://doi.org/10.1136/adc.67.4.533
C – Arrays https://www.tutorialspoint.com/cprogramming/c_arrays.htm
Structures In C, http://asic-world.com/scripting/structs_c.html