2. 2
Syllabus
Module 1: Introduction to Data structures, Arrays, Structures
Module 2: Stacks and Queues
Module 3: Linked Lists
Module 4: Trees
Module 5: Graphs and Hasing
3. 3
Course Objectives
• Explain the fundamentals of data structures and their applications essential
for implementing solutions to problems.
• Illustrate linear representation of data structures: Stack, Queues, Lists,
Trees and Graphs.
• Design and Develop Solutions to problems using Arrays, Structures, Stack,
Queues, Linked Lists.
• Explore usage of Trees and Graph for application development.
• Apply the Hashing techniques in mapping key value pairs.
4. 4
Course Outcomes
• Understand fundamentals of data structures and their applications
essential for programming/problem solving
• Apply Linear Data Structures such as Stack, Queues and Recursion in
solving problems.
• Apply Linear Data Structures such as Linked Lists in solving problems.
• Apply Non-Linear Data Structures such as Trees to model and solve the
real-world problem.
• Apply Non-Linear Data Structures such as Graphs model and solve the
real-world problem and Make use of Hashing techniques and resolve
collisions during mapping of key value pairs.
6. 6
Introduction: Data Structures, Classifications (Primitive & Non-Primitive), Data
structure operations (Traversing, inserting, deleting, searching, and sorting).
Review of Arrays
Structures: Array of structures, Self-Referential Structures.
Dynamic Memory Allocation Functions, Representation of Linear Arrays in
Memory, dynamically allocated arrays and Multidimensional Arrays.
Demonstration of representation of Polynomials and Sparse Matrices with arrays.
Module 1
8. 8
• Data structure is representation of the logical relationship existing between
individual elements of data.
• In other words, a data structure is a way of organizing all data items that
considers not only the elements stored but also their relationship to each
other.
• Data structure affects the design of both structural & functional aspects of
a program.
Program=Algorithm + Data Structure
Introduction
9. 9
• Data structure is representation of the logical relationship existing between
individual elements of data.
• In other words, a data structure is a way of organizing all data items that
considers not only the elements stored but also their relationship to each
other.
• Data structure affects the design of both structural & functional aspects of
a program.
Program=Algorithm + Data Structure
Introduction
10. 10
• Data structures includes:
Defining operations that Performed can be on data.
Representing data in the memory.
Determining amount of memory required to store and amount of time
needed to process data.
Introduction
11. 11
• The computers are electronic data Processing machines. In order to we
need to know
How to represent data in computer?
How to access them?
What are steps to be performed to get the needed output.
Need of Data Structures
13. 13
1. Traversing: To access each data item exactly once so that it can be
processed.
2. Searching: To find out the location of the data item if it exists in the
given collection
3. Inserting: To add a new data item in the given collection of data items.
4. Deleting: To delete an existing data item from the given collection of data
items.
5. Sorting: To arrange the data items in some order i.e. in ascending or
descending order
6. Merging: To combine the data items of two sorted files into single file in
Data Structure Operations
15. 15
• Array is a data structure that can store a fixed-size sequential collection of
elements of the same type.
• An array holds several values of the same kind.
• Accessing the elements in an array is very fast. It may not be possible to
add more values than defined at the start, without copying all values into a
new array.
• An array is stored so that the position of each element can be computed
from its index.
Review of Arrays
16. 16
#include <stdio.h>
int main ()
{
int arr[10]; /* n is an array of 10 integers */
int i,j;
/* initialize elements of array n to 0 */
for(i=0;i<10;i++)
arr[i] =i+100; /* set element at location i to i + 100 */
/* output each array element's value */
for (j=0;j<10;j++)
printf("Element[%d] = %dn", j, arr[j] );
}
Declaration assignment, and accessing arrays
17. 17
#include <stdio.h>
int main() {
int arr[10], pos, value, i, n;
printf("Enter number of elements in array: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for(i=0;i<n;i++)
scanf("%d", &arr[i]);
printf("Enter the location where you wish to insert an element: ");
scanf("%d",&pos);
printf("Enter the value to insert: ");
scanf("%d",&value);
Declaration assignment, and accessing arrays
18. 18
for (i=n-1;i>=pos-1;i--)
arr[i+1] = arr[i];
arr[pos-1] = value;
printf("Resultant array : ");
for (i=0;i<=n;i++)
printf("%dt", arr[i]);
}
Declaration assignment, and accessing arrays
19. 19
#include <stdio.h>
int main()
{
int arr[10], pos, i, n;
printf("Enter number of elements in array: ");
scanf("%d",&n);
printf("Enter %d elements: ", n);
for(i=0;i<n;i++)
scanf("%d", &arr[i]);
printf("Enter the location where you wish to delete an element: ");
scanf("%d", &pos);
To delete an element in the middle of an array
20. 20
if (pos>= n+1)
printf("Deletion not possible.n");
else
{
for (i=pos-1;i<n-1;i++)
arr[i] = arr[i+1];
}
printf("Resultant array: ");
for (i=0;i<n-1;i++)
printf("%dt", arr[i]);
return 0;
}
To delete an element in the middle of an array
21. 21
#include <stdio.h>
int main()
{
int arr[10], search, i, n;
printf("Enter the number of elements in array: ");
scanf("%d",&n);
printf("Enter %d integer(s): ", n);
for(i=0;i<n;i++)
scanf("%d", &arr[i]);
printf("Enter a number to search: ");
scanf("%d",&search);
Search an element using Linear Search (Sequential Search)
22. 22
for(i=0;i<n;i++)
{
if (arr[i] == search) /* If required element is found */
{
printf("%d is present at location %d.n", search, i+1);
break;
}
}
if(i==n)
printf("%d isn't present in the array.n", search);
return 0;
}
Search an element using Linear Search (Sequential Search)
23. 23
• Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the
search interval in half.
• Binary search is used to find the position of a key in a sorted array.
• The following describes the binary search algorithm:
We compare the key with the value in the middle of the array.
If the key match found, we return its position, which is the index of the middle element
of the array.
If the key is less than the value of the middle element of the array, we repeat the
above step on the sub-array on the left. If the key is greater than the value of the
middle element of the array, we repeat the above step on the sub-array on the right.
If the sub-array to be searched has zero item, it means the key cannot be found.
Search an element using Binary Search
24. 24
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, arr[10];
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter %d integers in ascending order: ", n);
for(i = 0; i < n; i++)
scanf("%d",&arr[i]);
printf("Enter Search Key: ");
scanf("%d", &key);
low = 0;
high = n - 1;
Search an element using Binary Search
25. 25
while (low <= high) {
mid = (low+high)/2;
if(arr[mid] == key) {
printf("%d found at location %dn", key, mid+1);
break;
}
else if(arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
if(low > high)
printf("%d Not found! in the Array", key); }
Search an element using Binary Search
26. 26
• First Iteration (Compare and Swap)
Starting from the first index, compare the first and the second elements.
If the first element is greater than the second element, they are swapped.
Now, compare the second and the third elements. Swap them if they are not in order.
The above process goes on until the last element.
To sort an element using Bubble Sort
• Remaining Iteration
The same process goes on for the remaining iterations.
After each iteration, the largest element among the unsorted elements is placed at the
end.
• In each iteration, the comparison takes place up to the last unsorted element.
• The array is sorted when all the unsorted elements are placed at their correct positions.
27. 27
#include <stdio.h>
int main()
{
int i, j, n, arr[10];
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter %d integers : ", n);
for(i = 0; i < n; i++)
scanf("%d",&arr[i]);
printf("Array Elements before Sorting : ");
for(i=0;i<n;i++)
printf("%dt",arr[i]);
To sort an element using Bubble Sort
28. 28
for(i=0;i<n-1;i++) {
for (j=0;j<n-i-1;j++) {
if (arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("nArray Elements After Sorting : ");
for(i=0;i<n;i++)
printf("%dt",arr[i]);
}
To sort an element using Bubble Sort
29. 29
for(i=0;i<n-1;i++) {
for (j=0;j<n-i-1;j++) {
if (arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("nArray Elements After Sorting : ");
for(i=0;i<n;i++)
printf("%dt",arr[i]);
}
To sort an element using Bubble Sort