Data Structures Using C (Csit124) Lecture Notes: by Dr. Nancy Girdhar
Data Structures Using C (Csit124) Lecture Notes: by Dr. Nancy Girdhar
Data Structures Using C (Csit124) Lecture Notes: by Dr. Nancy Girdhar
LECTURE NOTES
MODULE- I
ARRAY
2
Before we Start….. ASET
• Email: [email protected]
X Batch_Lab: v5t3ftw
Y Batch_Lab: tugcd3b
Theory_Lectures: dogxhmu
3
Course Content
Module 1: Introduction to Data Structures
Module 4: Trees
Definition of Array
Representation of Array in Memory
Operations on array
Address calculation for data stored in an array in 1D and 2D
6
Abstract Data Type
• With an ADT, we know what a specific data type can do, but
how it actually does it is hidden (Simply hiding the
implementation).
Data Structure : Array
9
Definitions
What is an Array?
Array is a data structure used to store homogeneous elements at contiguous
locations.
• Important terms
• Element − each item stored in an array is called an element.
• Index − each location of an element in an array has a numerical
index which is used to identify the element.
Array Representation
Index starts with 0
Array length is 10
Basic Operations
• Traverse − print all the array elements one by one.
• Insertion − add an element at given index.
• Deletion − delete an element at given index.
• Update − update an element at given index.
• Searching − search an element using given index or by value.
• Sorting – Arranging in Ascending or descending order
1-D Arrays
For example,
/* declare and initialize and array*/
For example,
/*store only 3 elements in the array*/
int x[6] = {19, 10, 8};
• Here, the array x has a size of 6. However, we have initialized it with only 3 elements.
Step 2:
Step 3:
Step 1:
Find element to delete
Step 2:
Shift elements to the left to fill the gap
Step 3:
Decrease No. of elements in array by 1
4) Search in Array (Linear Search)
Array Operations: Time Complexity
• Let size of array be 𝑛.
• Accessing Time: O(1) [This is possible because elements are stored at contiguous
locations]
• Insertion Time: O(n) [The worst case occurs when insertion happens at the
Beginning of an array and requires shifting all of the elements]
• Deletion Time: O(n) [The worst case occurs when deletion happens at the
Beginning of an array and requires shifting all of the elements]
2D Array (Matrix)
• The syntax for creating a two-dimensional array is:
Initializing a 2D Array
/* declare and initialize a 2D array*/
3 4 5 2 8 4 1 4 5
0 1 2 3 4 5
0
1
2
3
3
4
0 1 2 3 4 5
0
1
2
3
3
4
In above example matrix, there are only 6 non-zero elements ( those are 9, 8,
4, 2, 5 & 2) and matrix size is 5 ∗ 6.
Here, the first row in the right side table is filled with values 5, 6 & 6 which
indicates that it is a sparse matrix with 5 rows, 6 columns & 6 non-zero
values.
Second row is filled with 0, 4, & 9 which indicates the value in the matrix at
0th row, 4th column is 9. In the same way the remaining non-zero values also
follows the similar pattern.
Linked List Representation
In linked list, each node has four fields. These four fields are defined as:
In above representation, H0, H1,...,H4 indicates the header nodes which are
used to represent indexes.
In this representation, in each row and column, the last node right field
points to it's respective header node.
Strength and Weakness of Arrays
Strength Weakness
Random Access possible (Fast Search Time) Slow Insertion/ Deletion Time
Less memory needed per element Fixed size once declared
Better cache locality Inefficient memory allocation/
utilization
LetsTest
Draw the Sparse matrix for the following matrix using
Triplet representation
LetsTest
Draw the Sparse matrix for the following matrix using
Triplet representation
Fill up the table
Linear Search
O(1) O(n) O(n)
2. A Library has list of books. Each book has unique id. The data base
has the book id in sorted order. Department has given the students
the book id rather the author name. Write a program to help the
students to search whether the particular book is available in the
library or not.
Self Reading
Reference Book:
•Robert L Kruse, “Data Structure and Program Design in C”, Prentice Hall (1991).
•Noel Kalicharan ,“Data Structure in C” ,Ist Edition Create space publisher, 2008.
•Mark Allen Weiss,“Data Structure and algorithm Analysis in C”,2nd Edition AddisonWesley,1996.
•E. Balagurusamy, “Problem Solving through C language”, TMH publication, Fourth Edition, 2008.
•R.S Salaria ,“Data Structures & Algorithms using C”,Khanna Publication,4th Edition,2009
•E.Horowitz and S.Sahni,”Fundamentals of Data Structures in C “,2nd Edition, Universities Press,2008.
41
Thank you !