DS Module 1
DS Module 1
Module -1
Introduction
1.1 Data Structures
“A data structure is a method of storing and organizing the data in a computer so that it
can be used efficiently”.
Data Structure is a way of collecting and organizing data in such a way that we can perform
operations on these data in an effective way.
Data Structures is about rendering data elements in terms of some relationship, for better
organization and storage.
If the data contains a single value, then it can be represented using primitive data types.
If the data contains set of values, then it can be represented using non-primitive data types.
Dept. of CS&E,JIT-DVG 1
DATASTRUCTURE AND APPLICATIONS
Primitive Data Structures are the basic data structures that directly operate upon the machine
instructions. They have different representations on different computers. Integers, floating point
numbers, character constants, string constants and pointers come under this category.
Non-primitive data structures are more complicated data structures and are derived from
primitive data structures. They emphasize on grouping same or different data items with
relationship between each data item. Arrays, lists and files come under this category.
A data structure is said to be linear if its elements form a sequence or a linear list. The linear
data structures like an array, stacks, queues and linked lists organize data in linear order.
There are basically four ways of representing such linear structure in memory.
1. Arrays: An array is a collection of similar type of items (elements) stored sequentially
(continuously) one after the other in memory.
2. Stack: A stack is an ordered list in which insertions and deletions are made at one end called
the top.
3. Queue: A queue is an ordered list in which insertions and deletions take place at different
ends.”
4. Linked list: Linked list is a linear data structure that consists of a sequence of elements where
each element comprises of two items - the data and a reference (link) to the next node
A data structure is said to be non-linear if the data are not arranged in sequence or linear. The
insertion and deletion of data is not possible in linear fashion. i.e.,elements form a hierarchical
classification where, data items appear at various levels.
Trees: Tree is a non-linear data structure which organizes data in hierarchical fashion and
the tree structure follows a recursive pattern of organizing and storing data.
Graph: It is basically a collection of vertices (also called nodes) and edges that connect these
vertices.
Dept. of CS&E,JIT-DVG 2
DATASTRUCTURE AND APPLICATIONS
A[0] 10
A[1] 20
A[2] 30
A[3] 40
A[4] 50
The Elements in the Array A can be accessed using the common name A, but with different index.
The Element „10‟ is called 0th Element and it can be accessed using the Subscript 0 (called Index „0‟)
along with name of the array „A‟.
An „Index‟ is also called as Subscript ([ ]). It is used to indicate the position of an element in the
Array.
Dept. of CS&E,JIT-DVG 3
DATASTRUCTURE AND APPLICATIONS
1000 35 Marks[0]
data type array_name 1002 45 Marks[1]
1004 65 Marks[2]
1006 55 Marks[3]
75
char name[5]; 1008 Marks[4]
5 memory locations are reserved. sizeof(int) is 2 bytes, 2*5=10 bytes are reserved.
5 memory locations are reserved. sizeof(char) is 1 bytes 1*5=5 bytes are reserved.
Dept. of CS&E,JIT-DVG 4
DATASTRUCTURE AND APPLICATIONS
a[0] 10
a[1] 20
a[2] 30
a[3] 40
a[4] 50
list[i] = α + i * sizeof(int)
For example, an array of 10 integer variables, with indices 0 through 9, may be stored as 10 words at
memory addresses 2000, 2004, 2008, ........ 2036, so that the element with index i has the address 2000
+ i× 4.[hereα is base address whose value is 2000 and sizeof (int) is4]
For i=0,1,2… (size of int is 4 bytes(32bit machine))
Dept. of CS&E,JIT-DVG 5
DATASTRUCTURE AND APPLICATIONS
a. The elements of the array are referenced respectively by an index set consisting of n
consecutive numbers.
b. The elements of the array are respectively in successive memory locations.
The number n of elements is called the length or size of the array. The length or the numbers of
elements of the array can be obtained from the index set by the formula
When LB = 0,
Length = UB – LB + 1
When LB = 1,
Length = UB
Where,
UB is the largest index called the Upper Bound
LB is the smallest index, called the Lower Bound
If you want to read n data items from the keyboard, the following statement can be used:
for(i=0;i<5;i++)
DATASTRUCTURE AND APPLICATIONS
{
scanf(“%d”,&a[i]);
}
If you want to print „n‟ data items from the keyboard, the following statement can be used:
for(i=0;i<5;i++)
{
printf(“%d\n”, a[i]);
}
1.5.2 Inserting
Let A be a collection of data elements stored in the memory of the computer. Inserting refers to the
operation of adding another element to the array A.
Inserting an element at the “end” of the linear array can be easily done provided the memory space
allocated for the array is large enough to accommodate the additional element.
Inserting an element in the middle of the array, then on average, half of the elements must be moved
downwards to new locations to accommodate the new element and keep the order of the other
elements.
Algorithm:
1. Start
2. Read pos, elem.
3. Create space for item to insert at position
for(i=n-1;i>=pos;i- -)
{
A[i+1] = A[i];
}
4. Insert item at the specified position
A[pos] = elem;
5. Update number of elements in the array
n=n+1;
6. Stop
1.5.3 Deletion
Deleting refers to the operation of removing one element from the array A with specified position.
Algorithm:
1. Start
2. Read pos.
3. Move the elements towards left
for(i=pos ; i< n-1; i++)
{
A[i] = A[i+1];
}
4. Display deleted element at the specified position
elem = a[pos];
5. Decrement the number of elements in the array
n=n-1;
6. Stop
Dept. of CS&E,JIT-DVG 9
DATASTRUCTURE AND APPLICATIONS
The size used during declaration of the array is useful to reserve the specified memory locations.
The array „a‟ is a 2-dimensional array with 2 rows and 4 columns. This declaration informs the
compiler to reserve 8 locations (2*4=8 locations, 2*8=16 bytes in total) continuously one after the
other.
Dept. of CS&E,JIT-DVG 1
DATASTRUCTURE AND APPLICATIONS
0 1 2
0 11 22 33
1 44 55 66
Rows 2 77 88 99
3 10 20 30
2. Column major order: the elements are stored column by column one column at a time.
Ex: int a[4][3] = {{11,22,33},{44,55,66},{77,88,99}};
Dept. of CS&E,JIT-DVG 1
DATASTRUCTURE AND APPLICATIONS
3.1 STRINGS
“A string is a sequence of characters enclosed within double quotes”.
or
“String is an array of characters and terminated by NULL character which is denoted by „\0‟.
D A V A N G E R E \0 Null character
0 1 2 3 4 5 6 7 8 9
Where,
char: data type used to declare the strings or characters.
string_name: It specifies the name of the given string.
size: The size or maximum length (number of characters including „\0‟) of the string is specified in square
brackets.
Length of the String: The „length‟ is the number of characters stored in the string up to but not
including the null character.
Example
1. char name[21];
Size of the string is 21, means that it can store up to 20 characters plus one null character.
2. char str[10];
Size of the string is 10, means that it can store up to 10 characters plus one null character.
a C O M P U T E R \0
0 1 2 3 4 5 6 7 8
Dept. of CS&E,JIT-DVG 12
DATASTRUCTURE AND APPLICATIONS
2. char b[ ]={„C‟, „O‟, „M‟, „P‟, „U‟, „T‟, „E‟, „R‟, „\0‟};
For this declaration, the compiler will set the array size to the total number of initial values. i.e. 9. The
characters will be stored in these memory locations in the order specified as shown below:
b C O M P U T E R \0
0 1 2 3 4 5 6 7 8
3. char b[ ]= “COMPUTER”;
Here, the string length is 8 bytes. But string size is 9 bytes. So, the compiler reserves 8+1 memory
locations and these locations are initialized with the characters in the order specified. The string is
terminated by „\0‟ by the compiler.
b C O M P U T E R \0
0 1 2 3 4 5 6 7 8
Dept. of CS&E,JIT-DVG 13
DATASTRUCTURE AND APPLICATIONS
Dept. of CS&E,JIT-DVG 14
DATASTRUCTURE AND APPLICATIONS
Dept. of CS&E,JIT-DVG 15
DATASTRUCTURE AND APPLICATIONS
Analysis: The while loop is iterated until the end of either the string or the pattern is reached. Since I
is never decreased, the lines that increase i cannot be executed more than m=strlen(string) times. The
resetting of j to failure [j-1]+1 decreases the value of j. so, this can‟t be done more times than j is
incremented by the statement j++ as otherwise, j falls off the pattern. Each time the statement j++ is
executed, i is also incremented. So, j can‟t be incremented more than m times. No statement of code is
executed more than m times.
return ((j == lenp) ? (i - lenp) : -1 )
This statement checks to see whether or not we found the pattern. If we didn‟t find the pattern, the
pattern index j is not equal to the length of the pattern and we return -1. If we found the pattern, then
the starting position is i the length of the pattern.
4.1 Structures
Structure is a user defined data type that can hold data items of same/different data
types. All data items grouped are logically related & can be accessed by using variables.
Declaration:
Syntax:
struct tag-name
{
data_type member 1;
data_type member 2;
………………………
………………………
data_type member n;
};
In this declaration, struct is a required keyword, tag-name is a name of the structure defined.
The individual members can be ordinary variables, pointers, arrays or other structures. The member
names within a particular structure must be distinct from one another.
The above example declares a structure called person that has three fields:
name = a name that is a character array
age = an integer value representing the age of the person
salary = a float value representing the salary of the individual
Dept. of CS&E,JIT-DVG 16
DATASTRUCTURE AND APPLICATIONS
Ex:
Struct student
{
char name[10]; 10 bytes
int roll_no; 4 bytes
float marks; 8 bytes
}; 22 bytes
To allocate the memory for the structure, we have to declare the variable as shown below:
Struct student s1,s2; [ size of variables s1,s2 is 22bytes each]
Two ways to declare variables:
Reading:
Dept. of CS&E,JIT-DVG 17
DATASTRUCTURE AND APPLICATIONS
Dept. of CS&E,JIT-DVG 18
DATASTRUCTURE AND APPLICATIONS
2. The complete definition of a structure is placed inside the definition of another structure.
Example:
typedefstruct
{
char name[10];
int age;
float salary;
struct
{
int month;
int day;
int year;
} date;
} humanBeing;
Dept. of CS&E,JIT-DVG 20
DATASTRUCTURE AND APPLICATIONS
Consider these statements, which create three structures and assign values to their respective fields:
list item1, item2, item3;
item1.data = 'a';
item2.data = 'b';
item3.data = 'c';
item1.link = item2.1ink = item3.link = NULL;
Structure variables item1, item2 and item3 each contain the data items a, b, and c respectively, and
the null pointer. These structures can be attached together by replacing the null link field in item 2
with one that points to item 3 and by replacing the null link field in item 1 with one that points to
item 2.
item1.link = &item2;
item2.1ink = &item3;
4.4 Unions
A union is a collection of data of similar data types or dissimilar data types. A union
declaration is similar to a structure, but the fields of a union must share their memory space. This
means that only one field of the union is "active" at any given time.
Syntax:
union tag-name
{
data_type member 1;
data_type member 2;
………………………
………………………
data_type member n;
};
Example:
unionstudent
{
char name[10];
intage;;
float salary;
};
The major difference between a union and a structure is that unlike structure members which are
stored in separate memory locations; all the members of union must share the same memory space.
This means that only one field of the union is "active" at any given time.
Dept. of CS&E,JIT-DVG 21
DATASTRUCTURE AND APPLICATIONS
C Structure C Union
keyword struct is used to define a structure keyword union is used to define a union
Structure allocates storage space for all its Union allocates one common storage space for
members separately. all its members. Union finds that which of its
member needs high storage space over other
members and allocates that much space.
Structure occupies larger memory space. Union occupies lower memory space over
structure.
We can access all members of structure at a time. We can access only one member of union at a
time.
Address of each member will be in ascending Address is same for all union members.
order (different address).
Dept. of CS&E,JIT-DVG 22
DATASTRUCTURE AND APPLICATIONS
Definition
“A pointer is a variable which contains the address of another variable as its value”.
Example:
1. int *ptr; // declares a pointer variable ptr of integer type.
2. float *temp; // declares a pointer variable temp of floating type.
int a=3;
int *ptr;
ptr=&a;
ptr a
Memory layout:
65530 3
Address: 65530
„ptr = &a‟ copies the address of „a‟ to the pointer variable „ptr‟.
Example Program: Write a C program to print value and address of the variable using
pointers.
#include<stdio.h>
#include<conio.h>
void main () Output:
{ The address of a=65530 and value of a=20
int a=20, *ptr1;
clrscr ();
ptr1 = &a; //ptr1 is a pointer to variable a
printf(“The address of a=%d and value of a=%d\n”,ptr1,*ptr1);
getch();
}
ptr1 a
Memory layout:
65530 20
Address: 65530
Initializing a Pointer Variable
We can initialize the pointer variables by assigning the address of other variable to them.
However these variables must be declared in the program.
Syntax
data_type *pointer_variable_name = address_of_variable;
where,
data_type:. It can be int, float, char etc.
Asterisk (*): It tells the compiler that we are declaring a pointer variable.
pointer_variable_name: It is the name of the pointer variable.
address_of_variable: It is the address of another variable.
Example:
1. int a;
int *ptr;
ptr=&a;
or
int a;
int *ptr=&a;
Both are equivalent.
Dept. of CS&E,JIT-DVG 24
DATASTRUCTURE AND APPLICATIONS
Static Allocation
If memory space is allocated for variables during compilation time, then it is called „Static
Memory allocation‟.
Size of memory space is „fixed‟; it can‟t be altered during execution time.
Example: int a [10];
During compilation, the compiler will allocate 10 memory locations for the array variable „a‟.
Inserting less than 10 elements leads to underutilization of allocated space and more than 10
elements cannot be inserted.
Dynamic Allocation
“Dynamic memory allocation is the process of allocating memory space during the
execution time (Run time).”
The various predefined memory management functions that are used to allocate or deallocate
memory are:
1. malloc( )
2. calloc( )
3. realloc( )
4. free( )
Syntax:
ptr = (data_type *) malloc (size);
where,
ptr: ptr is a pointer variable of type int, float, char, double etc.
data_type: It can be any of the basic data type or user defined data type.
size: size is the number of bytes to be reserved for a block.
Example:
1. ptr = (int *) malloc(10);
Allocates a block of Memory of 10 bytes.
Dept. of CS&E,JIT-DVG 25
DATASTRUCTURE AND APPLICATIONS
Syntax:
ptr = (data_type *) calloc (n,size);
where,
ptr: ptr is a pointer variable of type int, float, char, double etc.
data_type: It can be any of the basic data type or user defined data type.
n: n is the number of blocks to be allocated.
size: size is the number of bytes in each block.
Example:
1. ptr = (int *) calloc(10, 2);
Allocates 10 blocks of Memory each of 2 bytes.
Syntax
ptr = (data_type *)realloc (ptr, new_size);
where,
ptr: it is a pointer to a block of previously allocated memory either using malloc( ) or calloc( ).
data_type: It can be any of the basic data type or user defined data type.
new_size: it is the new size of the block.
Example:
char *str;
str = (char *) malloc(10); // malloc function allocates 10 memory blocks
strcpy(str, “Computer”);
str = (char *) realloc (str, 40); //realloc function allocates new memory blocks from 10 to 40
strcpy(str, “Computer Science and Engineering”);
Dept. of CS&E,JIT-DVG 26
DATASTRUCTURE AND APPLICATIONS
Ex:int *ptr;
ptr = (int *) malloc(100*sizeof(int));
free(ptr);
5.4 Polynomials
“A polynomial is a sum of terms, where each term has a form axe, where x is the variable, a is
the coefficient and e is the exponent.”
Example polynomials are:
A(x) =3x20 + 2x5 + 4
B(x) =x4 + 10x3 + 3x2 +1
The largest (or leading) exponent of a polynomial is called its degree. Coefficients that are zero are
not displayed. The term with exponent equal to zero does not show the variable since x raised to a
power of zero is 1.
Dept. of CS&E,JIT-DVG 27
DATASTRUCTURE AND APPLICATIONS
Polynomial operations
1. Representation
2. Addition
3. Multiplication
The above figure shows how these polynomials are stored in the array terms.
The index of the first term of A and B is given by startA and startB, while finishA and finishB give
the index of the last term of A and B.
The index of the next free location in the array is given by avail.
For above example, startA=0, finishA=1, startB=2, finishB=5, & avail=6.
Dept. of CS&E,JIT-DVG 28
DATASTRUCTURE AND APPLICATIONS
float coefficient;
*startD = avail;
while (startA <= finishA && startB <= finishB)
switch(COMPARE(terms[startA].expon, terms[startB].expon))
{
case -1: /* a expon< b expon */
attach (terms [startB].coef, terms[startB].expon);
startB++;
break;
case 0: /* equal exponents */
coefficient = terms[startA].coef + terms[startB].coef;
if (coefficient)
attach (coefficient, terms[startA].expon);
startA++;
startB++;
break;
case 1: /* a expon> b expon */
attach (terms [startA].coef, terms[startA].expon);
startA++;
}
/* add in remaining terms of A(x) */
for(; startA <= finishA; startA++)
attach (terms[startA].coef, terms[startA].expon);
*finishD = avail-i;
}
Example: A(x) = 2xl000+ 2x2 +1 and B(x) = x4 + 10x3 + 3x2 + 1 find D=A+B
Addition of 2 polynomial D = 2xl000+ x4 + 10x3 + 5x2 +2
Dept. of CS&E,JIT-DVG 29
DATASTRUCTURE AND APPLICATIONS
Note: A sparse matrix can be represented in 1-Dimension, 2- Dimension and 3- Dimensional array.
When a sparse matrix is represented as a two-dimensional array as shown in Figure B, more space is
wasted.
Example: Consider the space requirements necessary to store a 1000 x 1000 matrix that has only
2000 non-zero elements. The corresponding two-dimensional array requires space for 1,000,000
elements. The better choice is by using a representation in which only the nonzero elements are
stored.
Dept. of CS&E,JIT-DVG 30
DATASTRUCTURE AND APPLICATIONS
Positions 1 through 8 store the triples representing the nonzero entries. The row index is in the field
row, the column index is in the field col, and the value is in the field value. The triples are ordered
by row and within rows by columns.
Row Col Value
a. Spare matrix stored as triplet and b. transpose of spare matrix stored as triple
Dept. of CS&E,JIT-DVG 31
DATA STRUCTURES AND APPLICATIONS
JIT, DAVANAGERE 1
DATA STRUCTURES AND APPLICATIONS
1. STACKS:
Definition
“A stack is an ordered list in which insertions (pushes) and deletions (pops) are made at one end
called the top.”
Given a stack S= (a0, ... ,an-1), where a0 is the bottom element, an-1 is the top element, and
ai is on top of element ai-1, 0 < i < n.
As shown in above figure, the elements are added in the stack in the order A, B, C, D, E, then
E is the first element that is deleted from the stack and the last element is deleted from stack is A.
Figure illustrates this sequence of operations.
Since the last element inserted into a stack is the first element removed, a stack is also known
as a Last-In-First-Out (LIFO) list.
JIT, DAVANAGERE 2
DATA STRUCTURES AND APPLICATIONS
1. Stack Create
Stack CreateS(maxStackSize )::=
#define MAX_STACK_SIZE 100 /* maximum stack size*/
typedef struct
{
int item; /* other fields */
} element; ADT for stack
element stack[MAX_STACK_SIZE]; (Abstract Data Type)
int top = -1;
2. Boolean IsEmpty(Stack)::= top < 0;
3. Boolean IsFull(Stack)::= top >= MAX_STACK_SIZE-1;
JIT, DAVANAGERE
DATA STRUCTURES AND APPLICATIONS
1.1.1 Push( )
Function push checks whether stack is full. If it is, it calls stackFull( ), which prints an error message
and terminates execution. When the stack is not full, increment top and assign item to stack [top].
void push()
{
if (top >= MAX_STACK_SIZE-1)
{
stackFull();
}
else
{
printf(“enter the element to be pushed on to stack\n”);
scanf(“%d”,&item);
top= top+1;
stack[top] = item;
}
}
1.1.2 Pop( )
Deleting an element from the stack is called pop operation. The element is deleted only from the top
of the stack and only one element is deleted at a time
void pop ( )
{
if (top == -1)
{
return stackEmpty();
}
else
{
item= stack[top];
top= top-1;
1.1.3 Display( )
void Display( )
{
JIT, DAVANAGERE
DATA STRUCTURES AND APPLICATIONS
if (top == -1)
{
return stackEmpty();
}
else
{
printf(“elements on stack are\n”);
for(i=0;i>=top;i++)
{
printf(“%d\n”,stack[i]);
}
} // end of else
}
1.1.4 stackFull( )-The stackFull which prints an error message and terminates execution.
void stackFull()
{
fprintf(stderr, "Stack is full, cannot add element");exit(0);
}
void stackEmpty
{
fprintf(stderr, "Stack is Empty cannot delete/display elements\n ");exit(0);
}
JIT, DAVANAGERE
DATA STRUCTURES AND APPLICATIONS
int item;
} element;
element *stack; MALLOC(stack,
sizeof(*stack));
int capacity= 1;
int top= -1;
2. Boolean IsEmpty(Stack)::= top < 0;
3. Boolean IsFull(Stack)::= top >= capacity-1;
4. push()
Here the MAX_STACK_SIZE is replaced with capacity
void push(element item)
{
if (top >= capacity-1)
{
stackFull();
}
else
{
printf(“enter the element to be pushed on to stack\n”);
scanf(“%d”,&item);
top= top+1;
stack[top] = item;
}
}
JIT, DAVANAGERE
DATA STRUCTURES AND APPLICATIONS
}
}
6. stackFull( )
The new code shown below, attempts to increase the capacity of the array stack so that new element
can be added into the stack. Before increasing the capacity of an array, decide what the new capacity
should be. In array doubling, array capacity is doubled whenever it becomes necessary to increase
the capacity of an array.
void stackFull()
{
REALLOC (stack, 2*capacity*sizeof(*stack));capacity
*= 2;
}
2. EXPRESSIONS:
It is sequence of operators and operands that reduces to a single value after evaluation is called an
expression.
X=a/b–c+d*e–a*c
above expression contains operators (+, –, /, *) operands (a, b, c, d, e).
1.4.1 Types of Expressions
Prefix Expression or Polish notation
Infix Expression
Postfix Expression or Reverse Polish notation
Infix Expression:
In this expression, the binary operator is placed in-between the operand. The expression can be
parenthesized or un- parenthesized.
JIT, DAVANAGERE
DATA STRUCTURES AND APPLICATIONS
Example: A + B
Here, A & B are operands and + is operand
Prefix or Polish Expression:
In this expression, the operator appears before its operand.
Example: + A B
Here, A & B are operands and + is operand
Postfix or Reverse Polish Expression:
In this expression, the operator appears after its operand.
Example: A B +
Here, A & B are operands and + is operand
JIT, DAVANAGERE
DATA STRUCTURES AND APPLICATIONS
The operators are arranged from highest precedence to lowest. Operators with highest
precedence are evaluated first.
The associativity column indicates how to evaluate operators with the same precedence.
For example, the multiplicative operators have left-to-right associativity. This means that the
expression a * b / c % d / e is equivalent to ( ( ( ( a * b ) / c ) % d ) / e )
Parentheses are used to override precedence, and expressions are always evaluated from the
innermost parenthesized expression first.
JIT, DAVANAGERE
DATA STRUCTURES AND APPLICATIONS
if(F(s[top]) != G(symbol))
s[++top] = symbol; //push the input symbol
else
top--; //discard „(„ from stack
}
while(s[top] != '#')
{
postfix[j++] = s[top--]; //pop and place in postfix
}
postfix[j] = '\0'; //terminated by null
}
JIT, DAVANAGERE
DATA STRUCTURES AND APPLICATIONS
Note: Refer class notes for examples and steps in conversion (detailed).
JIT, DAVANAGERE
DATA STRUCTURES AND APPLICATIONS
JIT, DAVANAGERE