Course Objectives
Understand, Practice and Assimilate fundamentals of data
structures and their applications essential for programming /
problem solving
Describe, Analyze, Design and Evaluate the Linear Data
Structures: Stack, Queues, Lists
Describe, Analyze, Design and Evaluate the Non-Linear Data
Structures: Trees, Graphs
Describe, Analyze, Design and Evaluate the sorting & searching
algorithms
Assess appropriate data structure during program development
/Problem Solving
Introduction to data structures
MODULE 1
Outline
Introduction to Data Structures
Classification of Data Structures
Data structure Operations
Review of Arrays, Structures, Self Referential Structures and
Unions
Pointers and Dynamic Memory Management Functions
Arrays
Strings
Programming Examples
What is data type?
A data type is a collection of objects (data) and set of
operations that act on those objects
For example : if data is integer the operations may be add,
subtract, multiply, divide and any other operations
appropriate for the data
What is data structures?
Data structure is a method for storing and organizing data in
the computer
Classification of data structures
DATA STRUCTURES
PRIMITIVE NON PRIMITIVE
LINEAR NON LINEAR
Primitive and non primitive DS
Primitive data structures are the basic data types
Examples: int, float, char, double viod
Non primitive data structures are the data structures created
using primitive DS
Examples: arrays, structures, union, stacks, list, queue etc
Linear and non linear DS
Non primitive data structure can be classified into linear and
non linear based on how the elements are stored
If the values or elements are stored in sequential order, then
they are called LINEAR DS
Examples: arrays, lists, stack, queue
If the values or elements are stored in non sequential order
then it is called NON LINEAR DS
Examples: trees, graphs
Data structure operations
Create: declaration and initialization of the data structure
Insert: adding new records to the structure
Delete: removing a record from the structure
Search: finding the location of a particular record or all the
records
Sort: arranging the records in some logical order
Merge: combining the records from two structures into a
single structure
Traversal: accessing each record exactly once
Structures
Arrays are collections of data of the same type
Structure (record) is a collection of data items, where each
item can be of same or different data type
struct structure_name{
data-type member_1;
data-type member_2;
.
.
data-type member_n;
};
struct {
char name[10];
int age;
float salary;
} person;
strcpy([Link], “james”);
[Link]=10;
[Link]=35000;
Create structure data type
typedef struct human_being {
char name[10];
int age;
float salary;
};
or
typedef struct {
char name[10];
int age;
float salary
} human_being;
human_being person1, person2;
Structure within structure
typedef struct {
int month;
int day;
int year;
} date;
typedef struct {
char name[10];
int age;
float salary;
date dob;
} human_being;
[Link]=2;
[Link]=11;
[Link]=1944;
Pointers
Pointer is a variable that holds the address of another variable
That is, the actual value of a pointer type is an address of
memory
Two important operators
& the address operator
* the dereferencing (indirection) operator
Declaration:
data_type *pointer_variable_name;
Example :
int *p;
Here p is a pointer variable that can hold the address of
(only) an integer variable
int i , *p;
p = &i;
Here &i returns the address of i and assigns it as the value of
p
To assign the value to i we can write,
i = 10;
Or
*p = 10;
Self Referential Structures
One or more of its components is a pointer to itself.
typedef struct list {
char data; Construct a list with three nodes
list *link; [Link]=&item2;
} [Link]=&item3;
malloc: obtain a node
list item1, item2, item3;
[Link]=‘a’;
a b c
[Link]=‘b’;
[Link]=‘c’;
[Link]=[Link]=[Link]=NULL;
Arrays
Arrays : collection of homogeneous elements in the form of
index and value
For each index, there is a value associated with that index
Stored in continuous memory locations ( may not be also)
Array is a collection of variables of the same data types that
share a common name
General form : data_type array_name[size];
int list[5], *plist[5];
list[5]: five integers
list[0], list[1], list[2], list[3], list[4]
*plist[5]: five pointers to integers
plist[0], plist[1], plist[2], plist[3], plist[4]
implementation of 1-D array
list[0] base address =
list[1] + sizeof(int)
list[2] + 2*sizeof(int)
list[3] + 3*sizeof(int)
list[4] + 4*size(int)
Compare int *list1 and int list2[5] in C.
Same: list1 and list2 are pointers.
Difference: list2 reserves five locations.
Notations:
list2 - a pointer to list2[0]
(list2 + i) - a pointer to list2[i] (&list2[i])
*(list2 + i) - list2[i]
Example: 1-dimension array addressing
int one[ ] = {0, 1, 2, 3, 4};
Goal: print out address and value
void print1(int *ptr, int rows)
{
/* print out a one-dimensional array using a pointer */
int i;
printf(“Address Contents\n”);
for (i=0; i < rows; i++)
printf(“%8u%5d\n”, ptr+i, *(ptr+i));
printf(“\n”);
}
call print1(&one[0], 5)
Address Contents
1228 0
1230 1
1232 2
1234 3
1236 4
Operations
Following are the basic operations supported by an array.
Insertion − add an element at given index.
Deletion − delete an element at given index.
Traverse − print all the array elements one by one.
Search − search an element using given index or by value.
Update − update an element at given index.
Merge – combining the elements from two arrays into a
single structure
Sort – arranging the records in some logical order
Insertion
Insert operation is to insert one or more data elements into
an array.
New element can be added –
At the beginning
At the end or
At any given index of array.
Insertion at the beginning
199
10 20 30 40 50
What need
0 1 2 3 4 5 to be done
…..?????
Algorithm
1. Start
2. Set J = N
3. Set N = N+1
4. Repeat steps 5 and 6 until J >=0
5. Set LA[ J ]=LA[ J-1 ]
6. Set J = J-1
7. Set LA[ 0 ] = ITEM
8. Stop
Insertion at the end
1. Start
2. Set J = N
3. Set N = N+1
4. Set LA [ J ] = ITEM
5. Stop
Insertion at a given index
199
10 20 30 40 50
0 1 2 3 4 5
What need
to be done
…..?????
Algorithm
1. Start
2. Set J = N N –no of elements in
the array
3. Set N = N+1 K –index at which new
4. Repeat steps 5 and 6 until J >= K ITEM will be inserted
5. Set LA[ J ]=LA[ J-1 ]
6. Set J = J-1
7. Set LA[ K ] = ITEM
8. Stop
Deletion
Element can be deleted –
From the beginning
At the end or
At any given index of array.
Deletion : front of the array
10 20 30 40 50
0 1 2 3 4 5
What need to be
done …..?????
Deletion : at the end
Deletion : at given index
10 20 30 40 50
0 1 2 3 4 5
What need to be
done …..?????
Search
KEY to be searched is 30
30
10 20 30 40 50
0 1 2 3 4 5
How to
search…..?????
Merge
10 30 20 40
Sort
20 > 10 40 30