Data Structures & Its Application
Data Structures & Its Application
APPLICATION – 21CS34
&
DATA STRUCTURES –
21AR32
INTRODUCTION
INTRODUCTION TO DATA STRUCTURES
What is a Data?
• The quantities, characters or symbols
• On which operations are performed by a computer
• Which may be
• stored
• transmitted in the form of electrical signals
• Recorded on magnetic, optical or mechanical recorded media
• Example: a = b + c
INTRODUCTION TO DATA STRUCTURES
• Data & Information:
• Data: SERUTCURTS ATAD
• Information: DATA STRUCTURES
• Graphs are used to store the friendship information on a social networking site
CLASSIFICATION
CLASSIFICATIONS
CLASSIFICATIONS
• Primitive Data Structure:
PUSH() POP()
CLASSIFICATIONS
• Linked List: Group of nodes in a sequence which has 2 parts: data and address
CLASSIFICATIONS
• Queue: First In First Out (FIFO) to process data
CLASSIFICATIONS
• Queue: First In First Out (FIFO) to process data
CLASSIFICATIONS
Tree: Elements stored on hierarchical relation among data
CLASSIFICATIONS
• Tree:
CLASSIFICATIONS
• Graph: Define connection between different types of data / nodes
DATA STRUCTURE
OPERATIONS
DATA STRUCTURE OPERATIONS
• Traversing: Accessing each record exactly once so that items in the record may
be processes.
• Searching: Finding the location of the record with a given key value or finding
the locations of all records which satisfy one or more conditions.
• Inserting: Adding new record to the data structure.
• Deleting: Removing a record from the data structure.
• Sorting: Arranging the records in some logical order.
• Merging: Combining the records in two different sorted files into a single sorted
file
DYNAMICALLY ALLOCATED
ARRAYS
DYNAMICALLY ALLOCATED ARRAYS
• There are four dynamic memory allocation function:
1. Malloc
2. Calloc
3. Realloc
4. Free
• Malloc:
• Malloc function allocates memory and leaves the allocated memory uninitialized.
• Allocated memory can only be accessed through pointers
• Malloc returns the pointer of type void which can be casting into pointer of any form.
• Malloc returns null pointer if the memory cannot be allocated.
• Syntax:
• void * malloc(size) //actual syntax
• p = (cast type*)malloc(size) //in general
• int *p;
p = (int *)malloc(size)
DYNAMICALLY ALLOCATED ARRAYS
• Malloc:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int n;
printf(“Enter the size of array”);
scanf(“%d”,&n);
int *a = (int *)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
a[i] = i+1;
}
for(int i=0;i<n;i++)
{
printf(“%d”,a[i]);
}
}
DYNAMICALLY ALLOCATED ARRAYS
• calloc:
• Calloc allocates the memory and initializes all bytes to zero
• Syntax: void* calloc(n, size) //general syntax
datatype *p;
p = (casttype*)calloc(n,sizeof(datatype));
#include<stdio.h>
#include<stdlib.h>
void main()
{
int n;
printf(“Enter the size of array”);
scanf(“%d”,&n);
int *a = (int *)calloc(n,sizeof(int));
for(i=0;i<n;i++)
{
a[i] = i+1;
}
for(int i=0;i<n;i++)
{
printf(“%d”,a[i]);
}
}
DYNAMICALLY ALLOCATED ARRAYS
• realloc:
• If memory allocated using malloc and calloc function is insufficient or more than the required, then we can reallocate the memory using realloc function.
SparseMatrix Transpose(a)::=
return the matrix produced by interchanging the
row and column value of every triple.
DEMONSTRATION OF SPARSE MATRIX WITH ARRAYS
Abstract Data Type of Sparse Matrix:
• ADT SparseMatrix is
objects: a set of triples <row, column, value>, where row and column are integers
and form a unique combination, and value comes from the set item.
functions:
for all a, b € SparseMatrix, x € item, i, j, maxCol, maxRow € index
1 6 0 0 0 0 4 0 5
• Solution:
Fast transpose approach make use of two additional arrays
1. Row Count array – stores the count of number of elements in each row
2. Starting Position array – Indicates the starting index of each row
STRING OPERATIONS
STRINGS
Strings - Introduction:
• A string is a sequence of characters.
• It includes digits and special characters.
• The number of characters in the string is called length of the string.
• The string with zero characters is empty string or null string.
STRINGS
Storing Strings:
• Strings are stored in three types of structures:
1. Record Oriented Fixed – length structures
2. Variable – length structures
3. Linked Structures
STRINGS
1. Record Oriented Fixed Length Structure:
• In this structure, each line will be stored as a fixed length record.
• Disadvantage:
• Time is wasted reading an entire record if most of the storage consists of blank space
• Certain records may require more space than available.
• When the correction consists of more or fewer characters than the original text, changing
a misspelled word require the entire record to be changed.
• Example: printf(“Data”);
int i; p r i n t f ( “ D a t a “ ) ;
0 15
i n t i ;
16 31
STRINGS
2. Variable Length Storage with Fixed Maximum:
• The storage of variable length strings in memory with fixed length can be done
in two ways
1. One can use a marker such as $$ to signal the end of the string
2. One can list the length of the string as an additional item in the pointer array.
Printf(“Data”);$$ 15 Printf(“Data”);$$
int i;$$ 6 int i;$$
. .
. .
STRINGS
3. Linked Storage:
• Fixed length storages cannot be used for word processing which requires
frequent modifications of the strings, for such applications strings are stored by
means of linked lists.
• Linked list is a linearly ordered sequence of memory cell called nodes.
• Each node contains an item and a link which points to the next node in the list.
• Strings can be stored in linked lists as follows:
• Each memory cell is assigned 1 character or a fixed number of characters.
• Link contained in all nodes gives the address of cell containing the next
character or group of characters.
h a i fo ll ow
Linked storage - 1 character per word Linked storage - 2 character per word
STRINGS
String Operations:
• Substring: Group of consecutive elements in string are called as substrings.
• Accessing a substring from a given string requires
1. Name of the string or the string itself
2. Position of the first character of the substring in the given string
3. Length of substring or position of the last character of the string.
4. Substring operation can be written as: SUBSTRING(string, initial, length)
STRINGS
String Operations:
• Indexing: Also called as pattern matching, refers to finding the position when a
string pattern P first appears in a given string text T
• It can be written as index( text, pattern)
• Concatenation: Let S1 and S2 be strings. Then concatenation of S1 and S2
results in a string which contains characters from S1 followed by characters in S2
• Length: The number of characters in a string is called its length.
STRINGS Reverse string
Return: i – length(Pattern)
STRINGS
Pattern matching using Table derived from pattern:
• Algorithm:
Step 1: [initialization] Set i=0 and state=q0
Step 2: Repeat steps 3 to 5 while Si ≠P, i<N
Step 3: Read T[i]
Step 4: Set Si+1=F(Si,Ti)
Step 5: Set i=i+1
[end of Step 2 loop]
Step 6: [Successful?]
if Si=p then,
index = i-length(P)+1
else
index=0
[end if]
STRINGS
Pattern Matching by Checking End Indices First:
• Algorithm: Pattern Matching
Step 1: Find length of pattern
Step 2: Take a pointer end_match and set it to the length of pattern – 1
Step 3: Take a pointer start, which points to the beginning of text
If the last character of pattern and character at index end_match of T matches
the begin comparing from first index till all characters match
Or
End of string
if the last character did not match
then increment start pointer
STACK CONCEPT /
INTRODUCTION TO STACK
STACK CONCEPT
Introduction:
• Stack is an ordered list in which insertions (also called as push) and deletions (also called as
pop) are made at one end called top.
• Stack is LIFO structure i.e., Last In First Out.
• It is very useful data structure in C programming
• Stack can be implemented using Linked List or Array
• When stack is empty, then it does not contain any
element inside it.
• Whenever the stack is empty, the position of top is -1.
ARRAY REPRESENTATION
OF STACK
STACK IMPLEMENTATION
Using One Dimensional Array:
stack [ max_stack_size]
max_stack_size: maximum number of entries in the stack.
stack[0] = 1st or bottom element is stored
stack[i-1] = ith element is stored
top = points to the top element in stack
top – 1 = empty stack.
STACK CONCEPT
ADT of stack:
Objects: A finite list with zero or more elements
Functions:
Stack Creates(maxstacksize) :: create an empty stack, whose maximum size = maxstacksize.
Boolean IsFull(stack,maxstacksize) :: if(number of element in stack == maxstacksize)
return true
else return false
Stack push(stack,item) :: if(IsFull(stack))
stackFull
else insert item into top of stack & return
Boolean IsEmpty(stack) :: IsEmpty(stack)
if(stack = =creates(maxstacksize))
return true
else return false
int fun(int n)
{
if(n==1)
return 1;
else
return 1 + fun(n-1)
}
RECURSION
• Whenever a function is called, its activation record is maintained inside a stack.
int fun(int n)
{
if(n==1)
return 1;
else
return 1 + fun(n-1)
}
Int main()
{
int n = 3;
printf(“%d”, fun(n));
return 0;
}
ACKERMAN FUNCTION
ACKERMANN FUNCTION
• It is a function of two integers m & n
• If m = 0,
then A(m, n) = n+1
• If m ≠ 0 but n = 0,
then A(m, n) = A(m-1, 1)
• If m ≠ 0 and n ≠ 0,
then A(m, n) = A(m-1, A(m, n-1))