Professional Documents
Culture Documents
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
strcpy(person.name, “james”);
person.age=10;
person.salary=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;
typedef struct {
char name[10];
int age;
float salary;
date dob;
} human_being;
person1.dob.month=2;
person1.dob.day=11;
person1.dob.year=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.
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
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
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