Introduction to Data Structure
Teaching Scheme & Evaluation Scheme
• Teaching Scheme: - Credit : 4
– Lecture : 3 hours per week
– Practical : 2 hours per week
• Evaluation Scheme:
– Course policy document
Unit - 1
Linear Data Structures I:
Types of Data Structures, Abstract data
type, Arrays, Structure, Stacks: Array
representation of Stacks, Stack Operations,
Applications of a Stack: Reversing a list,
Conversion and Evaluation of Arithmetic
Expression, Recursion Queues: Array
representation of Queues, Queue
operations: Types of Queues: Simple
Queues, Circular Queues, Priority Queues
Need of data structure
• Railway reservation system
• Tower of Hanoi
– [Link]
q=tower+of+hanoi&docid=608021813366882664
&mid=6737080C1344996B27726737080C1344996
B2772&view=detail&FORM=VIRE
• Job scheduling – Operating System
Topics
• Need of data structure
• Data Structure
• Arrays
• Records & Pointers
• Multidimensional Arrays
• Pointer Arrays
• Record Structure
Data
• Entity – that has certain attributes or properties which may
be assigned values.
• Values may be numeric or non numeric
• Employee –
– Attributes: Name Age Gender SSN
– Values: Arpit 20 M 134-24-5533
• Entity set – Entities with similar attributes
• The way that data are organized into the hierarchy of
fields, records and files reflects the relationship between
attributes, entities and entity sets.
Data
• Data – simple values or sets of values
• Data Item – refers to a single unit of values
• Data item –divided into sub items – are called group items or
composite data.
– For example employee’s name – divided into first name, middle name
and last name
• Data item – not divided into sub items – are called elementary
items or atomic items.
– Social security number – treated as a single item
• Collection of data – organized into a hierarchy of fields, records
and files.
Data Type
• Consists of two parts:
– A set of data
– Operations that can be performed on the data.
• Example : Integer type consists of values (whole numbers in some defined range)
– Operations – add, subtract, multiply, divide etc..
• Data type = permitted value + operations
• Data Structure = collection of permitted values + operations
• If we take a combination of data and fit them into a structure such that we can
define its relating rules, we have made a data structure.
• When we define a data structure0 we are in fact creating a new
data type of our own.
– i.e. using predefined types or previously user defined types.
– Such new types are then used to reference variables type within a
program
Various Data Structure
Data
Data organized as
Fields
Records
File
Fixed Length Variable Length
Records Records
• The data may be organized into many such different ways – the logical and
mathematical model of a particular organization of data is called data structure
• The way information is organized in the memory of a computer is called a data
structure.
• A data structure helps you to understand relationship of one data element with the
other and organize it within the memory.
Introduction to Data Structures
• way of organizing all data items
• considers not only elements stored but also
their relationship to each other.
• Specifies following
– Organization of data.
– Accessing methods.
– Degree of associativity.
– Processing alternatives for information.
Classification of Data structure
Data
Structur
e
Non-primitive
Primitive data
data structure
Structure (type)
(organization)
Floating Charact
Integer Pointers Arrays Lists Files
point er
Non-
Linear
Linear
Lists
Lists
Graphs Trees Stacks Queues
Primitive Data Structure
Basic structures
Directly operated upon by the machine
instructions
Data Structure classification
• Primitive \ Non Primitive
• Linear \ Non Linear
• Homogenous \ Non Homogenous
• Static \ Dynamic
Non primitive Data structure
Derived from primitive data structure.
Emphasize on structuring of a group of
homogeneous or heterogeneous data items
Eg:-
Arrays
Lists
Files
Arrays, Records and Pointers
Data Structure
• Data Structure : classified as either linear or non linear
• Linear data structure: if its elements form a sequence or linear list
• There are two basic ways of representing such linear structures in
memory:
– One way is to have the linear relationship between the elements represented by
means of sequential memory locations e.g. Arrays
– The other way – linear relationship between the elements represented by means of
pointers or links e.g. Linked Lists
Data Structure Operations
• The data in data structures are processed by means of certain
operations
• The four main operations:
– Traversing – Processing each element in the list.
– Searching
– Inserting
– Deleting
– Sorting
– Merging
Arrays
• Array declaration must give, three items of information:
– The name of the array
– The data type of the array
– The index set of the array
• Memory for array can be allocated in two ways:
– Statically : Compile time – size of array is fixed during program execution
– Dynamically: Run time – read value of n at run time and then allocate memory
while program execution.
Arrays
• array – list of finite number n of homogeneous data elements, finite
collection of similar elements stored in adjacent memory locations.
• The elements of the array are referenced respectively by an index set
consisting of n consecutive numbers
• The elements of the array are stored respectively in successive memory
locations.
• n – indicates total number of elements in the array – size or length of
the array.
• Length = Total number of elements = UB –LB +1
• Array elements are denoted as A1, A2,… An or A(1), A(2),… A(n) or
A[1],A[2],….,A[n].
Representation of Linear Arrays in Memory
• Arr[] – linear array
• Loc(Arr[k]) = address of the element Arr[k ] of the array Arr
1000 Base(Arr)
1001
1002
1003
1004
No need to keep track of the address of every element of Arr.
Track only the address of the first element of Arr, denoted by Base (Arr)
Using base address, computer calculates address of any element of Arr by using below
formula:
Loc(Arr[k]) = Base(Arr) + w(K-Lower bound)
Arrays in C++
• Declaration of an Array in C:
– Data type followed by array name.
– Subscript in bracket indicates the number of elements array will hold.
– By declaring an array, the specified number of memory locations are allocated in
the memory
– For example,
int age[20] ;
float sal[10];
char grade[10];
int arr[5]
100 102 104 106 108
arr[0] arr[1] arr[2] arr[3] arr[4]
Arrays in C
• Array initialization:
– Can be initialized at the time of declaration:
int age[5] = [8,10,5,15,20];
float sal[3] = [2000, 2000.50,1000];
2000 2000.50 1000
sal[0] sal[1] sal[2]
• An array of characters is called a string and it is terminated by a null
(‘o’) character.
char name[3]=‘abc’;
Traversing Linear Arrays
• Traversing a Linear Array
LA = Linear Array
LB = Lower Bound
UB = Upper Bound
1. [Initialize Counter] set K:=LB
2. Repeat Steps 3 and 4 while K<=UB
[Visit element] Apply Process to LA[K]
[Increase Counter] set K:=K+1
3. [End of Step 2 loop]
4. Exit
5. Repeat for K=LB to UB
Apply Process to LA[K]
6. [End of Loop]
7. Exit
Inserting into a Linear Arrays
N=4 NAME
• Inserting into a Linear Array
Brown
LA = Linear Array
Davis
LB = Lower Bound
Johnson
UB = Upper Bound
N = array with N elements Smith
K= K is a positive integer such that K<=N.
This algorithm Inserts an element ITEM into the Kth position in LA
INSERT(LA, N, K, Item)
1. [Initialize counter] Set J:=N
2. Repeat Steps 3 and $ while J>= K N=4 NAME
3. [Move Jth element downward] Set LA[J+1]:=LA[J] K=3 Brown
4. [Decrease counter] Set J:=J-1 NAME Item=Ford
Davis
5. [End of step 2 loop] Brown J=N=4
6. [Insert element] set LA[K]:=Item Johnson
7. [Reset N] Set N:= N+1 Davis
8. Exit
Smith
Ford
Johnson
Smith
Multi Dimensional Arrays
• Linear arrays – referenced by one sub scripts
• Multi Dimensional arrays- referenced by more than one subscript two
or three
• A two dimensional M* N array A is a collection of M*N elements such
that each element is specified by pair of integers such as j, k called
subscripts
• The element of A with subscript j and second subscript k is denoted by
Aj,k or A[j][k]
• Two dimensional arrays are called matrices with M rows and N
columns.
Representation of Two Dimensional Arrays in Memory
• A two dimensional m* n array – in memory represented as m*n
sequential memory locations.
• Array A can be stored in either of two ways:
– Column by column- Column major order
– Row by row – Row major order Row Major Order
Column A Subscript A Subscript
Major (1,1) (1,1)
Order (2,1) Column1 (1,2)
Row 1
(3,1) (1,3)
(1,2) (1,4)
(2,2) Column2 (2,1)
(3,2) (2,2)
Row 2
(1,3) (2,3)
(2,3) Column3 (2,4)
(3,3) (3,1)
(1,4) (3,2)
(2,4) Column4 (3,3) Row3
(3,4) (3,4)
Pointers
• Pointers are special variables which contain the address of another
memory location.
• Pointers are useful in accessing any memory location directly.
• An address of a memory location is a long integer – which is stored in
pointer type variable.
• Adding two pointers or subtracting two pointers – gives number of
bytes between two memory addresses.
Pointers
• & - address operator – represents the address of the variable.
• %u – used for obtaining the address = printf(“address of X = %u”, &X);
• Declare base variable
– int X;
• Declare pointer type variable related to base variable
– int *P;
• Establish relation between base variable and pointer variable
– P = &X;
With the help of pointer
Address of Memory where
1011 P one can access the
value for variable X is stored
variable X.
*P = “Value at Pointer P”
25
= Value at memory
Variable name X location 1011 = Value of
variable X = 25
1011
P Pointer Type Variable
Pointers
• Another pointer variable can store the address of a pointer variable
int a=2;
int *b;
int **c; /* c has been declared as a pointer to pointer variable
b=&a; which contains address of pointer variable b */
c=&b;
Address of Memory where
1011 value for variable a is stored
1021
2
1021 c
Variable name a Pointer to Pointer
Type Variable
1011
b Pointer Type Variable
Pointer Arrays
• On array declaration – sufficient amount of storage is allocated by the compiler
• The compiler also defines the name of the array as a pointer to the first element.
• The name arr acts as a pointer pointing to the first element.
arr = &arr[0] =100;
int *ip;
ip = arr or ip=&arr[0];
ip = &arr[0] = 100;
ip+1 = &arr[1] = 102;
ip+2 = &arr[2] = 104;
ip++ = 106 = bytes that pointer data type holds are added
Address of arr[2] = base address + (2*size of int)
= 100 + (2*2)
=104
arr[0] arr[1] arr[2] arr[3]
2 6 4 8
100 102 104 106
Call By Value
• The process of passing the actual values of variables as arguments to a function is called call by
value. Before calling program
2 1
Main()
{ a (1000) b (2000)
int a =2;
int b=1;
Printf(“Before Calling the function, a and b are %d %d “, a, b);
Value(a,b);
Printf(“After calling the function, a and b are %d %d”, a, b);
}
Value(int p, int q) 2
In Function 1
{
p++;
q++; p ( 5000) q (8000)
}
After p++ and q++ in function After calling function
3 2 2 1
p(5000) Q(8000) a(1000) b(2000)
Call By Reference
• Pass the addresses of the variables as parameters to the function.
Before calling program
2 1
Main()
{ a (1000) b (2000)
int a =2;
int b=1;
Printf(“Before Calling the function, a and b are %d %d “, a, b);
Value(&a,&b);
Printf(“After calling the function, a and b are %d %d”, a, b);
}
Value(int *p, int *q) 1000
In Function 2000
{
(*p)++;
(*q)++; p ( 5000) q (8000)
}
After (*p)++ and (*q)++ in function After calling function
1000 2000 3 2
p(5000) Q(8000) a(1000) b(2000)
Records
• Recall that elements of arrays must all be of
the same type
scores
scores: : 85
85 79
79 92
92 57
57 68
68 80
80 . .. .. .
0 1 2 3 4 5 98 99
• In some situations, we wish to group elements
of different types
employee
employee [Link]
Jones 123
123 Elm
Elm 6/12/55
6/12/55 $14.75
$14.75
34
Record Structures
• Collections of data - organized into a hierarchy of fields, records and
files.
• Record – collection of related (Not similar) data items, called as field
or attribute, the data items in a record may have different data types.
• File – collection of similar records.
• The data items in a record are indexed by attribute names.
Record Structures
• Record for new born baby in hospital
1. Newborn
Name
Gender
Birthday
Month
Day
Year
Father
Name
Age
Mother
Name
Age
Can also have array of elements Newborn[20] – indicate a file with 20
records like Newborn[1], Newborn[2]……
Record Structures
1. Student(20)
2. Name
3. Last
3. First
3. Middle
2. Test(3)
2. Final
2. Grade
To access last name of first student – [Link][1]
To access marks of third test of first student – [Link][1][3]