Ds Module 1 Merged
Ds Module 1 Merged
Module -1
Introduction: Data Structures, Classifications (Primitive & Non Primitive), Data structure
Operations, Review of Arrays, Structures, Self-Referential Structures, and Unions. Pointers and
Dynamic Memory Allocation Functions. Representation of Linear Arrays in Memory,
Dynamically allocated arrays,
Array Operations: Traversing, inserting, deleting, searching, and sorting. Multidimensional
Arrays, Polynomials and Sparse Matrices.
Strings: Basic Terminology, Storing, Operations and Pattern Matching algorithms.
Programming Examples.
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.
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.
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.
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.
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))
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++)
Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 5
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
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.
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}};
Example: C Program to search the „key‟ element in an array using linear search algorithm.
#include <stdio.h>
void main()
{ Output:
int array[100], key, i, n; Enter number of elements in
printf("Enter number of elements in array\n"); array:
5
scanf("%d", &n);
Enter elements of array
printf("Enter elements of array\n"); 100
for (i = 0; i < n; i++) 25
scanf("%d", &array [ i ] ); 35
30
printf("Enter a number to search\n"); 56
scanf("%d", &key); Enter a number to search:
30
30 is present at location 4
for (i = 0; i < n; i++)
{
if (array[i] == key)
{
printf("%d is present at location %d.\n", key, i+1);
break;
}
}
if (i == n)
printf("%d isn't present in the array.\n", key);
}
2. Binary Search:
Binary search is a fast searching algorithm. This search algorithm works on the principle of divide and
conquers.
Binary search works on sorted arrays. Binary search begins by comparing the middle element of the array
with the target value. If the target value matches the middle element, its position in the array is returned.
If the target value is less than the middle element, the search continues in the lower half of the array. If
the target value is greater than the middle element, the search continues in the upper half of the array.
Now we compare the value stored at location 4, with the value being searched, i.e. 31. We find that the
value at location 4 is 27, which is not a match. As the value is greater than 27 and we have a sorted array,
so we also know that the target value must be in the upper portion of the array.
We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.
The value stored at location 7 is not a match; rather it is more than what we are looking for. So, the value
must be in the lower part from this location.
We compare the value stored at location 5 with our target value. We find that it is a match.
Example: C Program to search key elements in array using binary search algorithms.
#include<stdio.h>
void main()
{
int n, i, arr[50], key, first, last, middle;
Output:
printf("Enter total number of elements :");
Enter number of elements
scanf("%d",&n);
in array:
printf("Enter array elements:\n");
5
for (i=0; i<n; i++)
Enter elements of array
{
10
scanf("%d",&arr[i]);
25
}
35
printf("Enter a number to find :");
50
scanf("%d", &key);
65
first = 0;
Enter a number to search:
last = n-1;
65
while (first <= last)
65 is present at location 5
{ middle = (first+last)/2;
if ( arr[middle] == key)
{
printf("%d found at location %d\n", key, middle+1);
break;
}
else if ( arr[middle] < key)
{
first = middle + 1;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
printf("Not found! %d is not present in the list.",key);
}
}
Example: C Program to sort the given array elements using Bubble sort Algorithm.
#include<stdio.h>
void main()
{
int a[50],n,i,j,temp;
printf("Enter the size of array: ");
scanf("%d",&n); Output:
Enter the size of array:
printf("Enter the array elements: ");
5
for(i=0;i<n;++i) Enter elements of array
30
scanf("%d",&a[i]);
25
for(i=1;i<n;++i) 35
10
for(j=0;j<(n-i);++j)
56
if(a[j]>a[j+1]) Sorted elements:
10
{
25
temp=a[j]; 30
35
a[j]=a[j+1];
56
a[j+1]=temp;
}
printf("\nArray after sorting: ");
for(i=0;i<n;++i)
printf("%d ",a[i]);
}
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
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
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.
Observation of algorithms
P is an r-character string and T is an s-character string. Algorithm contains two loops, one inside the
other. The outer loop runs through each successive R-character substring WK = T[K] T[K + 1] ...
T[K+R-l] of T. The inner loop compares P with WK, character by character. If any character does
not match, then control transfers to Step 5, which increases K and then leads to the next substring of
T. If all the R characters of P do match those of some WK then P appears in T and K is the INDEX
of P in T. If the outer loop completes all of its cycles, then P does not appear in T and so INDEX =
0.
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
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:
Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 20
DATASTRUCTURE AND APPLICATIONS
Reading:
printf(“Enter name of the student\n”);
scanf(“%s”,s.name);
printf(“Enter marks of the student\n”);
scanf(“%f”,&s.marks);
Writing:
printf(“name of the student is %s\n”,s.name);
printf(“marks of the student is %f\n”,s.marks);
struct
struct
{
{ char name[10];
int month; int age;
int day; float salary;
int year; date dob;
}date; } humanBeing;
humanBeing person1;
A person born on February 11, 1944, would have the values for the date struct set as:
person1.dob.month = 2;
person1.dob.day = 11;
person1.dob.year = 1944;
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;
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.
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).
Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 24
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.
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.
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”);
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.
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.
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
Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 31
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.
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 triplet
QUESTION BANK
10 0 0 25 0
0 23 0 0 45
0 0 0 0 32
42 0 0 31 0
A=
0 0 0 0 0
0 0 30 0 0
20. Write the Knuth Morris Pratt pattern matching algorithm & apply the same to search the
pattern „abcdabcy‟ in the text „abcxabcdabxabcdabcdabcy‟.
main()
{ int fact(int n)
int n,res; {
printf (“Enter a positive integer value: "); if (n == 0)
return 1;
scanf (“%d”, &n);
else
res = fact(n); return n * fact (n-1);
printf ("\n Factorial of %d =%5d\n", n, res); }
}
A non-recursive or iterative version for finding the factorial is as follows:
main()
{
int n,res=1;
printf (“Enter a positive integer value: ");
scanf (“%d”, &n);
for (i=1; i<=n; i++)
{
res = res * i;
}
printf ("\n Factorial of %d =%5d\n", n, res);
}
The operation of the non-recursive version is clear as it uses a loop starting at 1 and ending at the
target value and progressively multiplies each number by the moving product. When a function calls
itself, new local variables and parameters are allocated storage on the stack and the function code is
executed with these new variables from the start. A recursive call does not make a new copy of the
function.
Only the arguments and variables are new. As each recursive call returns, the old local variables and
parameters are removed from the stack and execution resumes at the point of the function call inside
the function.
When writing recursive functions, there must be an exit condition somewhere to force the function to
return without the recursive call being executed. If there is no exit condition, the recursive function
will loop forever until you run out of stack space and indicate error about lack of memory, or stack
overflow.
1.2 Greatest Common Divisor (GCD): The GCD of two given numbers is the largest integer that
divides both of them. GCD is defined only for positive integers not for negative integers and floating
numbers.
#include<stdio.h>
int gcd(int a,int b);
void main()
{ int gcd(int a,int b)
int a,b,res; {
printf(“Enter two numbers”); if (n ==0)
scanf(“%d%d”,&m,&n); return m;
res=gcd(m,n); return gcd(n,m%n);
printf(“GCD of %d and %d is %d”,m,n,res); }
}
1.3 Fibonacci Sequence: The Fibonacci numbers are a series of numbers such that each number is
the sum of the previous two numbers.
A Fibonacci sequence starts with the integers 0 and 1. Successive elements in this sequence are
obtained by summing the preceding two elements in the sequence.
For example, third number in the sequence is 0 + 1 = 1, fourth number is 1 + 1= 2, fifth number is 1
+ 2 = 3 and so on. The sequence of Fibonacci integers is given below:
0 1 1 2 3 5 8 13 21 . . . . . . . . .
A recursive definition for the Fibonacci sequence of integers may be defined as follows:
Fib (n) = n if n = 0 or n = 1
Fib (n) = fib (n-1) + fib (n-2) for n >=2
Prof. Shrikant Pujar, Dept. of CS&E 2
DATA STRUCTURES AND APPLICATIONS
We will now use the definition to compute
fib(5): fib(5) = fib(4) + fib(3)
=fib(3) + fib(2) + fib(3)
=fib(2) + fib(1) + fib(2) + fib(3)
=fib(1) + fib(0) + fib(1) + fib(2) + fib(3)
=1 + 0 + 1 + fib(1) + fib(0) + fib(3)
=1 + 0 + 1 + 1 + 0 + fib(2) + fib(1)
=1 + 0 + 1 + 1 + 0 + fib(1) + fib(0) + fib(1)
=1 + 0 + 1 + 1 + 0 + 1 + 0 + 1 = 5
fib(2) is computed 3 times, and fib(3),is computed 2 times in the above calculations. The values of
fib(2) or fib(3) are saved and reused whenever needed
A recursive function to compute the Fibonacci number in the nth position is given below:
main()
fib (int n)
{
int n,res; {
printf (“Enter a positive integer value: "); if (n==0 | | n==1)
scanf (“%d”, &n); return n;
printf (“ fib(%d) is %d”, n, fib(n)); return fib(n-1) + fib(n-2);
} }
ADT Queue.
1. Queue CreateQ(maxQueueSize) ::=
#define MAX_QUEUE_SIZE 100
typedef struct
{
int ele;
} element;
element queue[MAX_QUEUE_SIZE];
int rear = -1;
int front = 0;
2. Boolean IsEmptyQ(queue) ::= f=r+1
3. Boolean IsFullQ(queue) ::= r =N-1 (r == MAX_QUEUE_SIZE-1)
4. void addq(ele) ::= void addq(element ele)
{
if (r == MAX_QUEUE_SIZE-1)
Prof. Shrikant Pujar, Dept. of CS&E 5
DATA STRUCTURES AND APPLICATIONS
queueFull();
queue [++rear] = item;
}
5. element deleteq( ) ::= element deleteq()
{
if (r==-1)
return queueEmpty( );
return queue[++front];
}
The queueFull function which prints an error message and terminates execution
3. Display Queue items: displaying the content of queue from front end to rare end.
for( i= front; i <= rare; i++)
{
Printf(“%d\t”, q[i]); // A B C D E
}
void main()
{
while(1)
{
printf("press 1 for insertion\n");
Prof. Shrikant Pujar, Dept. of CS&E 7
DATA STRUCTURES AND APPLICATIONS
printf("press 2 for deletion\n");
printf("press 3 for display\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insertQ();
break;
case 2: deleteQ();
break;
case 3: displayQ();
break;
default: printf(invalid choice\n");
}
}
}
void insertQ()
{
if(rear == MAX-1)
{
printf("\n Linear Queue is full");
}
printf("\n Enter element to be inserted: ");
scanf("%d", & ele);
rare = rare + 1;
q[rear] = ele;
}
void deleteQ()
{
if( front>rear) //OR front=rear +1
{
printf("\n\n Queue is Empty..");
}
printf("\n Deleted element from Queue is %d", Q[front]);
front = front + 1;
}
void displayQ()
{
if(front >rear)
{
printf("\n\n\t Queue is Empty");
}
printf("\n Elements in Queue are: ");
for(i = front; i < =rear; i++)
{
printf("%d\t", Q[i]);
}
}
In this above situation, when we try to insert another item, which shows that the queue is full. This
means that the rear index equals to MAX_QUEUE_SIZE -1. But even if the space is available at the
front end, rear insertion cannot be done.
2. delete(): This function is used to delete an element from the circular queue. In a circular queue,
the element is always deleted from front position.
Step 1: Check whether queue is Empty means check (front==-1). If it is empty then display Queue is
empty. If queue is not empty then step 2.
Step 2: Check if (front==rear) if it is true then set front=rear= -1 else check if (front==size-1), if it is
true then set front=0 and return the element.
Example:
To add an element, increment rear one position clockwise and insert at the new position. Here the
MAX_QUEUE_SIZE is 8 and if all 8 elements are added into queue and that can be represented in
below figure (a).
rare = (rear +1) % MAX_QUEUE_SIZE
To delete an element, increment front one position clockwise. The element A is deleted from
queue and if we perform 6 deletions from the queue of Figure (b) in this fashion, then queue
becomes empty and that front =rear.
front = (front+1)% MAX_QUEUE_SIZE;
To get a proper circular queue configuration, slide the elements in the right segment (i.e., elements A
and B) to the right end of the array as in figure (d)
Program gives the code to add to a circular queue using a dynamically allocated array.
Operations on deque:
1. Insert an item from front end
2. Insert an item from rare end
3. Delete an item from front end
4. Delete an item from rear end
5. Display the content of queue
4. Delete an item from rear end: if some item are present in queue, we can delete an item by
decreasing the front index r by 1.
f r
void deleterear()
{
if (count==0)
printf("underflow\n");
else
{
printf("%d is rear value\n",rear);
printf("element deleted is %d",dq[rear]);
if (rear==0)
rear=N-1;
else
rear=rear-1;
count--;
}
}
void display()
{
int i,k;
if (count==0)
printf("empty queue\n");
else
{
k=front;
for(i=0;i<count;i++)
{
printf("%d\t",dq[k]);
k=(k+1)%N;
}
}
}
Deque is a variation of queue data structure, pronounced “deck”, which stands for doubleended queue.
In a deque values can be inserted at either the front or the back, A collection of peas in a straw is a
good example.
Queues and deques are used in a number of ways in computer applications. A printer, for example, can
only print one job at a time. During the time it is printing there may be many different requests for other
output to be printed. To handle this printer will maintain a queue of pending print tasks. Since you want
the results to be produced in the order that they are received, a queue is the appropriate data structure.
2. Delete_front(): which returns the smallest item from the queue front.
3. Display (): which displays the contents of queue.
#include <stdio.h>
#define MAX 5
int pri_que[MAX];
int front=0, rear=-1,i,j;
void insert_by_priority(int data) /* Function to insert value into priority queue */
{
if (rear== MAX - 1)
{
printf("\nQueue overflow no more elements can be inserted");
return;
}
if (rear == -1))
{
rear++;
pri_que[rear] = data;
}
else
If i denotes an individual stack ,to establish multiple stacks, an array of top(tos[ i])and bottom
pointers (b[i])are maintained to keep track of the top and bottom of every stack.
Every stack„s bottom and top pointer is set to B[i]=tos[i]=(size/n)*i-1 which enables dividing the
stack to be divided into equal sized segments.
In the maze matrix, 1 means the block is a dead end and 0 means the block can be used in the path
from source to destination.
Example:
ASSIGNMENT QUESTIONS
1. Define stack. Write ADT for stack. Implement the operations push, pop & display using arrays &
implement Stack Full() & Stack Empty() functions.
2. Write an algorithm to implement a stack using dynamic array whose initial capacity is 1 and
array doubling is used to increase the stack‟s capacity whenever an element is added to a full
stack. Implement the operations push, pop & display.
3. What is expression? List and explain different types of expressions.
4. Write an algorithm and C function for (1) converting infix expression to postfix expression (2)
Evaluate a postfix expression.
5. Convert the following infix expression to postfix expression. And trace for the given data
1. ((a/(b-c+d))*(e-a)*c) where a=6,b=3,c=1,d=2,e=4.
2. (a+b)*d+e/(f+a*d)+c
3. ((6+(3-2)*4) 5+7)
4. A$B$C*D
6. Evaluate the following postfix expression And trace for the given data
1. ab/c-de*+ac* where a=6,b=3,c=1,d=2,e=4.
2. abc+*de/- where a=5,b=6,c=2,d=12,e=4.
7. Define recursion. Write the recursive program for:
(1) factorial of a number (2) tower of Hanoi.
8. Write an algorithm for tower of Hanoi. Trace the same with an example.
9. Write an algorithm for Ackerman‟s function. Evaluate A(1,2) using Ackerman‟s function.
10. Define queues. Implement QInsert and Qdelete functions for queues using arrays.
11. List the disadvantages of linear queue and explain how is it solved in circular queue. Give the
algorithm to implement a circular queue with suitable example.
12. What is double ended queue? Implement the operations of double ended queue.
13. What is priority queue? Explain the operations of priority queue.
14. Explain with example multiple stacks. Implement the program for multiple stacks.
15. Define Maze problem. Explain with example.
Module -2
Stacks and Queues: Definition, Stack Operations, Array Representation of Stacks,
Stacks using Dynamic Arrays, Stack Applications: Polish notation, Infix to postfix conversion,
evaluation of postfix expression, Recursion - Factorial, GCD, Fibonacci Sequence, Tower of
Hanoi, Ackerman's function. Queues: Definition, Array Representation, Queue Operations,
Circular Queues, Circular queues using Dynamic arrays, Dequeues, Priority Queues, A Mazing
Problem. Multiple Stacks and Queues. Programming Examples.
.
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.
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;
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( )
{
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);
}
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;
}
}
}
}
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.
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
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.
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
}
Note: Refer class notes for examples and steps in conversion (detailed).
ASSIGNMENT QUESTIONS
1. Define stack. Implement the operations push, pop functions.
2. What is expression? List and explain different types of expressions.
3. Write an algorithm or C function for (1) converting infix expression to postfix expression (2)
Evaluate a postfix expression.
4. Convert the following infix expression to postfix expression.
1. ((a/(b-c+d))*(e-f)*g)
2. (a+b)*d+e/(f+g*h)+i
3. A+(B*C-(D/E$F)*G)*H
4. ((A+(B-C)*D)$E+F)
5. Evaluate the following postfix expression And trace for the given data
1. ab/c-de*+ac*+ where a=6,b=3,c=1,d=2,e=4.
2. abc+*de/- where a=5,b=6,c=2,d=12,e=4.
MODULE -3
Linked Lists: Definition, Representation of linked lists in Memory, Memory allocation; Garbage
Collection. Linked list operations: Traversing, Searching, Insertion, and Deletion. Doubly Linked
lists, Circular linked lists, and header linked lists. Linked Stacks and Queues. Applications of
Linked lists – Polynomials, Sparse matrix representation. Programming Examples
3.1 Linked List: “A Linked list is a linear collection of data elements called nodes, where each
node is divided into two parts: the first part called info field, contains the information of the element
and second part called link field or next pointer field, contains the address of the next node in the
list”.
Node: Info-field Link-field
Example: first
10 20 30 40 \0
Arrays–drawbacks:
(1)The size of the arrays is fixed: So we must know the upper limit on the number of elements in
advance. (Static in nature)
(2) Inserting and a new element in an array of elements is expensive, because room has to be
created for the new elements and to create room existing elements have to shifted. Same holds good
for deletion also.
Garbage Collection: Whenever a node is deleted, some memory space becomes reusable. This
memory space should be available for future use. One way to do this is called „Garbage
Collection‟.
The operating system of computer may periodically collect all the deleted space into the free
space list. Any technique which does this collection is called garbage collection.
It takes place in two steps.
1. First computer runs through all lists, tagging those cells which are currently in use.
2. Then computer runs through the memory collecting the untagged space on to free storage list.
The garbage collection may take place when there is only some minimum amount of space or no
space at all left in the Free storage list or when CPU is idle and has time to do the collection.
Example: first
10 20 30 40 \0
Struct node
{
int info; //info-integer field contains information
struct node *link; //link- holds address of next node
};
typedef struct node *NODE; //giving alternative name to strcut node to NODE
NODE first; // first- self-referential structure variable
First = NULL; // Empty list by name first is created with NULL value
Create a node:
X = (data_type *) malloc (size);
Malloc(): it is used to allocate memory explicitly as and when required and exact amount of
memory space needed during execution.
X = (NODE) malloc (sizeof(struct node)); // allocate the memory space
NODE getnode()
{
NODE X;
X = (NODE) malloc (sizeof(struct node));
if ( X = = NULL) //nodes don‟t exist
{
Printf(“out of memory”);
}
return X; // allocation successful
}
Step 3: copy the address of the first node which is stored in the variable first into link field of
temp. temp -> link = first;
temp first
50 10 20 30 40 \0
Step 4: temp is inserted and then temp becomes the first node.
return temp;
first
50 10 20 30 40 \0
Step 2: copy item 50 into info field and NULL to link. temp
temp -> info = item; 50 \0
temp -> link = NULL;
Step 4: if the list is existing, then we have to insert temp at the end of list. To insert at the end,
we have to find the address of the last node.
cur = first;
while( cur -> link != NULL)
{
cur = cur ->link;
}
After executing the above code , we will get the address of last node in a list.
First cur temp
10 20 30 40 \0 50 \0
Step 5: copy the address of the temp node to cur -> link, NULL is replaced by temp address.
cur -> link = temp;
first cur temp
10 20 30 40 50 \0
Step 6: first contains the address of the first node of list. So we have to return first.
return first;
first
10 20 30 40 50 \0
10 20 30 40 50 \0
Step 3: update the pointer temp so that it contains address of the second node.
temp = temp -> link;
first temp
10 20 30 40 50 \0
Step 4: now, first points to first node and temp points to second node of the list. De-allocate the
memory of the first node.
free( first);
Step 5: once first node is deleted, and then second node temp is the first node. So, return temp as
first node. return temp;
first
20 30 40 50 \0
Step 2: if the list is existing, then we have to delete the last node of list. To delete at the raer end,
we have to find the address of the last node by using variable cur and prev.
cur = first;
prev= NULL;
while( cur -> link != NULL)
{
prev = cur;
cur = cur ->link;
}
After executing the above code , we will get the address of last node and prev get the address of
last but one node in a list.
First prev cur
10 20 30 40 50 \0
Step 4: once last node is deleted, and then prev node should be the last node. So, copy NULL to
link field of prev node.
prev -> link = NULL;
first prev
10 20 30 40 \0
10 20 30 40 \0
10 20 30 \0 40 50 \0
Step 3: now, attach address of the first node of the second list to cur node.
cur -> link = second;
First cur second
10 20 30 40 50 \0
if (first = = NULL)
return second;
if (second = = NULL)
return first;
cur = first;
while( cur ->link ! = NULL)
{
cur = cur ->link; // point cur to next node
}
Cur -> link =second;
return first;
}
10 20 30 40
Step 3: copy the address of the first node(i.e last -> link ) into link field of temp.
if(last != NULL)
temp -> link = last ->link;
else
temp = last;
temp first last
40 10 20 30
40 10 20 30
Step 3: copy the address of the first node(i.e last -> link ) into link field of temp.
if(last != NULL)
temp -> link = last ->link;
else
temp = last;
first last temp
10 20 30 50
Step 4: make temp as the last node by copying temp into link field of last node.
last->link = temp;
first temp
10 20 30 50
10 20 30 50
10 20 30 50
Step 4: Now remove the first node. De-allocate the memory of the first node.
free( first);
first last
10 20 30 50
10 20 30 50
10 20 30 50
Step 4: Make the last but one node as the last node and De-allocate the memory of the last node.
prev -> link = last ->link;
free( last);
first last
10 20 30 50
3.4.3 Doubly linked list: A doubly linked list is a linear collection of nodes where each node is
divided into three parts:
Info – contains information to be stored.
llink – contains the address of the left or previous node. llink info rlink
rlink - contains the address of the right or next node.
Example:
first
\0 10 20 30 \0
\0 40 10 20 30 \0
\0 40 10 20 30 \0
Step 2: To insert the node at rear end, first find the address of the last node.
cur = first;
while(cur -> rlink ! = NULL)
{
cur = cur -> rlink;
}
first cur temp
\0 10 20 30 \0 \0 40 \0
Step 3: Insert node at the end.
cur -> rlink = temp;
temp -> llink = cur;
first cur temp
\0 10 20 30 40 \0
return first;
first
\0 10 20 30 40 \0
\0 10 20 30 \0
\0 10 \0 20 30 \0
\0 10 \0 20 30 \0
Step 2:if list is existing then find the address of last node and last but one node.
cur = first, prev = NULL;
while(cur -> rlink ! = NULL)
{ prev = cur;
cur = cur -> rlink;
}
first prev cur
\0 10 20 30 40 \0
\0 10 20 30 40 \0
\0 10 20 30 \0
The info field of header node does not contain any information.
The first node starts from link field of header node i.e the node containing the info field 10 is the
first node.
head
4 10 30 40 50 \0
The info field of the header node contains the number of nodes in the list.
Step 3: Obtain the address of the first node which is stored in the head link field of head.
first = head -> link;
head first
10 20 30 40 \0
Step 4: temp node is inserted between the head and first node.
head -> link = temp;
temp temp -> link = first;
50
head first
10 20 30 40 \0
50 10 20 30 40 \0
2. Adding polynomials
Next
Down right
MODULE - 4
Trees: Terminology, Binary Trees, Properties of Binary trees, Array and linked Representation of
Binary Trees, Binary Tree Traversals - Inorder, postorder, preorder; Additional Binary tree
operations. Threaded binary trees, Binary Search Trees – Definition, Insertion, Deletion, Traversal,
Searching, Application of Trees-Evaluation of Expression, Programming Examples
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”.
Every individual element is called as Node. Node in a tree data structure, stores the actual data of
that particular element and link to next element in hierarchical structure.
If there are N number of nodes in a tree structure, then there can be a maximum of N-1 number of
links.
Example:
7. Internal Nodes: In a tree data structure, the node which has atleast one child is called as
INTERNAL Node. In simple words, an internal node is a node with atleast one child. In a tree data
structure, nodes other than leaf nodes are called as Internal Nodes. The root node is also said to be
Internal Node if the tree has more than one node. Internal nodes are also called as 'Non-Terminal'
nodes. Ex: B,C & D.
8. Degree of a node: In a tree data structure, the total number of children of a node is called as
DEGREE of that Node. In simple words, the Degree of a node is total number of children it has. The
highest degree of a node among all the nodes in a tree is called as 'Degree of Tree'. Ex: Degree of B
is 2, A is 2 and of F is 0
10. Height
In a tree data structure, the total number of edges from leaf
node to a particular node in the longest path is called as
HEIGHT of that Node. In a tree, height of the root node is
said to be height of the tree. In a tree, height of all leaf
nodes is '0'.
13. Sub Tree: In a tree data structure, each child from a node forms a subtree recursively. Every
child node will form a subtree on its parent node.
The ancestors of a node are all the nodes along the path from the root to that node. Ex: ancestor of j
is B & A A forest is a set of n 0 disjoint trees. The notion of a forest is very close to that of a tree
because if we remove the root of a tree we get a forest. For example, in figure 1 if we remove A we
get a forest with three trees.
In this representation, we use two types of nodes, one for representing the node with data and
another for representing only references.
We start with a node with data from root node in the tree. Then it is linked to an internal node
through a reference node and is linked to any other node directly. This process repeats for all the
nodes in the tree.
1. Array Representation
In array representation of binary tree, we use a one dimensional array (1-D Array) to represent a
binary tree. Consider the above example of binary tree and it is represented as follows...
To represent a binary tree of depth 'n' using array representation, we need one dimensional array
with a maximum size of 2n+1 - 1.
Advantages of array representation:
Faster access
Easy for implementation
Good for complete binary trees
Disadvantages:
Wastes memory for skewed trees
Implementation of operations requires rearranging (shifting) of array elements.
2. Test for Equality: Check whether two trees are equal or not.
int equal (NODE r1, NODE r2)
{
if(r1 = = NULL && r2 = = NULL) return 1; // two trees are equal
if(r1 = = NULL && r2 ! = NULL) return 0; // two trees are not equal
if(r1 ! = NULL && r2 = = NULL) return 0; // two trees are not equal
Right in threaded binary tree: if the right link of a node is NULL and if it is replaced by the
address of the inorder successor.
In-order Threaded binary tree: left and right links are replaced by the address of the inorder
successor.
4.7 Expression tree: A Binary Expression Tree is a special kind of binary tree in which:
1. Any leaf is an operand.
2. The root and internal nodes and operators
3. The subtrees represent sub expressions with root of the subtree as an operator.
MODULE - 5
Graphs: Definitions, Terminologies, Matrix and Adjacency List Representation Of Graphs,
Elementary Graph operations, Traversal methods: Breadth First Search and Depth First Search.
Sorting and Searching: Insertion Sort, Radix sort, Address Calculation Sort.
Hashing: Hash Table organizations, Hashing Functions, Static and Dynamic Hashing.
Files and Their Organization: Data Hierarchy, File Attributes Text Files and Binary Files, Basic
File Operations, File Organizations and Indexing.
2. Multigraph: A graph with self-loops or parallel edges(multiple edge) between any two vertices is
called a multigraph.
3. Complete graph: A graph is said to be a complete graph, if there exists an edge between every
pair of vertices.
4. Weighted graph: A graph in which a number is assigned to each edge in graph is called weighted
graph. Weight typically shows cost or distance of traversing.
Example: weights are distances between cities.
Unweighted graph: A graph in which a number is not assigned to edge in graph is called
unweighted. Edges simply show connections.
Example: Traverse the following graph by BFS and print all the vertices reachable from start vertex
a.
5.6 Sorting
Sorting means arranging the elements of an array so that they are placed in some relevant order
which may be either ascending or descending.
Example: Sort the following list using insertion sort – 77, 33, 44, 11, 88, 22, 66, 55
To insert an element A[K] in a sorted list A[0], A[1], ..., A[K–1], we need to compare A[K] with
A[K–1], then with A[K–2], A[K–3], and so on until we meet an element A[J] such that A[J] <=
A[K]. In order to insert A[K] in its correct position, we need to move elements A[K– 1], A[K–2], ...,
A[J] by one position and then A[K] is inserted at the (J+1)th location.
Prof. Shrikant Pujar, Dept. of CS&E 8
DATA STRUCTURES AND APPLICATIONS
5.6.2 Radix Sort
Radix sort is a linear sorting algorithm for integers and uses the concept of sorting names in
alphabetical order.
When we have a list of sorted names, the radix is 26 (or 26 buckets) because there are 26 letters in
the English alphabet. So radix sort is also known as bucket sort. Observe that words are first sorted
according to the first letter o/f the name. That is, 26 classes are used to arrange the names, where the
first class stores the names that begin with A, the second class contains the names with B, and so on.
During the second pass, names are grouped according to the second letter. After the second pass,
names are sorted on the first two letters. This process is continued till the nth pass, where n is the
length of the name with maximum number of letters.
After every pass, all the names are collected in order of buckets. That is, first pick up the names in
the first bucket that contains the names beginning with A. In the second pass, collect the name from
the second bucket, and so on.
When radix sort is used on integers, sorting is done on each of the digits in the number. The sorting
procedure proceeds by sorting the least significant to the most significant digit. While sorting the
numbers, we have ten buckets, each for one digit (0, 1, 2, …, 9) and the number of passes will
depend on the length of the number having maximum number of digits.
Example: Sort the following list using Radix sort – 348, 143, 361, 423, 538, 128, 321, 543,366
Sorted numbers are- 128, 143, 321, 348, 361, 366, 423, 538, 543.
Difference between Graph and Tree
2. Mid square method: In this method firstly key is squared and then mid part of the result is taken
as the index.
H(key) = Key2 = mid(key)
For example: consider that if we want to place a record of 3101 and the size of table is 1000. So
3101*3101=9616201 i.e. h (3101) = 162 (middle 3 digit).
Collision
It is a situation in which the hash function returns the same hash key for more than one
record, it is called as collision. Sometimes when we are going to resolve the collision it may
lead to a overflow condition and this overflow and collision condition makes the poor hash
function.
Collision resolution technique
If there is a problem of collision occurs then it can be handled by apply some technique. These
techniques are called as collision resolution techniques.
1) Chaining: It is a method in which additional field with data i.e. chain is introduced. A chain is
maintained at the home bucket. In this when a collision occurs then a linked list is maintained for
colliding data.
Example: Let us consider a hash table of size 10 and we apply a hash function of H(key)=key %
size of table. Let us take the keys to be inserted are 31,33,77,61. In the above diagram we can see at
same bucket 1 there are two records which are maintained by linked list or we can say by chaining
method.
Prof. Shrikant Pujar, Dept. of CS&E 12
DATA STRUCTURES AND APPLICATIONS
2) Linear probing: It is very easy and simple method to resolve or to handle the collision. In this
collision can be solved by placing the second record linearly down, whenever the empty place is
found. In this method there is a problem of clustering which means at some place block of a data is
formed in a hash table.
Example: Let us consider a hash table of size 10 and hash function is defined as H(key)=key %
table size. Consider that following keys are to be inserted that are 56,64,36,71.
In this diagram we can see that 56 and 36 need to be placed at same bucket but by linear probing
technique the records linearly placed downward if place is empty i.e. it can be seen 36 is placed
at index 7.
Dynamic Hashing: This is the process of mapping large amounts of data into a table whose size is
assigned during run time.
File Attributes:
File attributes are settings associated with computer files that grant or deny certain rights to how a
user or the operating system can access that file. For example, IBM compatible computers running
MS-DOS or Microsoft Windows have capabilities of having read, archive, system, and hidden
attributes.
Read-only - Allows a file to be read, but nothing can be written to the file or changed.
Archive - Tells Windows Backup to backup the file.
System - System file.
Hidden - File is not shown when doing a regular directory from DOS.
Prof. Shrikant Pujar, Dept. of CS&E 13
DATA STRUCTURES AND APPLICATIONS
In operating systems like Linux, there are three main file attributes: read (r), write (w), execute (x).
Read - Designated as an "r"; allows a file to be read, but nothing can be written to or changed in
the file.
Write - Designated as a "w"; allows a file to be written to and changed.
Execute - Designated as an "x"; allows a file to be executed by users or the operating system.
QUESTION BANK