DSA Notes M-1
DSA Notes M-1
MODULE-I
Based on the structure and arrangement of data, non-primitive data structures is further classified into
➢ Linear Data Structure
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 2
DATA STRUCTURES AND APPLICATIONS 18CS32
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 3
DATA STRUCTURES AND APPLICATIONS 18CS32
➢ Compiler allocates 5 consecutive memory-locations for each of the variables 'list' and 'plist'.
➢ Address of first element list [0] is called base-address.
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 4
DATA STRUCTURES AND APPLICATIONS 18CS32
main ()
{
{
int i;
float tempsum = 0;
for (i = 0; i < n; i++)
tempsum += list[i];
return tempsum;
}
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 5
DATA STRUCTURES AND APPLICATIONS 18CS32
Program to print both address of ith element of given array & the value found at that address.
printf(“Address Contents\n”);
for (i=0; i < rows; i++)
void main ()
{
Output
Arrays are generally used when we want to store large amount of similar type of data. But they have
the following limitations:
➢ Arrays are of fixed size.
➢ Data elements are stored in contiguous memory locations which may not be always available.
➢ Insertion and deletion of elements can be problematic because of shifting of elements from
their positions.
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 6
DATA STRUCTURES AND APPLICATIONS 18CS32
STRUCTURES
In C,a way to group data that permits the data to vary in type. This mechanism is called the structure,
for short struct.
A structure (a record) is a collection of data items, where each item is identified as to its type and
name.
Syntax:
struct
{
data_type member 1;
data_type member 2;
………………………
………………………
data_type member n;
} variable_name;
Ex: struct {
char name [10];
int age;
float salary;
} Person;
The above example creates a structure and variable name is Person and 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
Assign values to fields
To assign values to the fields, use. (dot) as the structure member operator. This operator is used to
select a particular member of the structure
Ex: strcpy(Person.name,“james”);
Person.age = 10;
Person.salary = 35000;
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 7
DATA STRUCTURES AND APPLICATIONS 18CS32
Type-Defined Structure
The structure definition associated with keyword typedef is called Type-Defined Structure.
Syntax 1:
typedef struct
{
data_type member 1;
data_type member 2;
………………………
………………………
data_type member n;
} TypeName;
Where,
➢ typedef is the keyword used at the beginning of the definition and by using typedef user
defined data type can be obtained.
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 8
DATA STRUCTURES AND APPLICATIONS 18CS32
Ex:
typedef struct
{
char name [10];
int age;
float salary;
} human Being;
In above example, humanBeing is the name of the type and it is a user defined data type.
Declarations of structure variables:
humanBeing person1, person2;
This statement declares the variable person1 and person2 are of type humanBeing.
Structure Operation
The various operations can be performed on structures and structure members.
1. Structure Equality Check:
Here, the equality or inequality check of two structure variable of same type or dissimilar type is not
allowed
typedef struct
{
char name [10];
int age;
float salary;
} humanBeing;
humanBeing person1, person2;
if (person1 = = person2) is invalid.
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 9
DATA STRUCTURES AND APPLICATIONS 18CS32
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 10
DATA STRUCTURES AND APPLICATIONS 18CS32
typedef struct
{
int month;
int day;
int year;
} date;
typedef struct
{
char name [10];
int age;
float salary;
date dob;
} 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:
typedef struct
{
char name [10];
int age;
float salary;
struct
{ int month;
int day;
int year;
} date;
} humanBeing;
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 11
DATA STRUCTURES AND APPLICATIONS 18CS32
SELF-REFERENTIAL STRUCTURES
➢ A self-referential structure is one in which one or more of its components is a pointer to itself.
➢ These require dynamic storage management routines (malloc & free) to explicitly obtain and
release memory.
typedef struct
{
char data;
struct list *link; //list is a pointer to a list structure
} list;
➢ Consider three structures and values assigned to their respective fields:
list item1, item2, item3;
item1.data='a';
item2.data='b';
item3.data='c';
item1.link=item2.link=item3.link=NULL;
➢ We can attach these structures together as follows
item1.link=&item2;
tem2.link=&item3;
Union
A union is a special data type available in C that allows us to store different data types in the same
memory location. You can define a union with many members, but only one member can contain a
value at any given time. Unions provide an efficient way of using the same memory location for
multiple-purpose.
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 12
DATA STRUCTURES AND APPLICATIONS 18CS32
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str [20];
};
int main ()
{
union Data data;
printf (“Memory size occupied by data: %d\n", sizeof(data));
return 0;
}
The memory occupied by a union will be large enough to hold the largest member of the union. For
example, in the above example, Data type will occupy 20 bytes of memory space because this is the
maximum space which can be occupied by a character string.
POINTERS
➢ In computer science, a pointer is a programming language data type whose value refers directly
to (or "points to") another value stored elsewhere in the computer memory using its address.
➢ This is a memory-location which holds the address of another memory-location.
➢ The 2 most important operators used w.r.t pointer are:
& (address operator)
* (dereferencing/indirection operator)
#include<stdio.h>
void main ()
{
int a=10, b=20; //Declare a data variable
int *p, *q; //Declare a pointer variable
int p=&a, q=&b; //Initialize a pointer variable
int x=*p + *q;
printf("%d+%d=%d",*p,*q, x); //Access data using pointer variable
}
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 13
DATA STRUCTURES AND APPLICATIONS 18CS32
NULL POINTER
➢ The null pointer points to no object or function.
i.e. it does not point to any part of the memory.
if(p==NULL)
printf("p does not point to any memory");
else
printf("access the value of p");
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 14
DATA STRUCTURES AND APPLICATIONS 18CS32
#define MALLOC(p,s) \
if (! ((p)==malloc(s))) \
{ \
printf("insufficient memory"); \
exit (0); \
}
MALLOC (pf,sizeof(float));
DANGLING REFERENCE
➢ Whenever all pointers to a dynamically allocated area of storage are lost, the storage is lost to
the program. This is called a dangling reference.
1) Set all pointers to NULL when they are not actually pointing to an object. This makes sure that you
will not attempt to access an area of memory that is either
→ out of range of your program or
→ that does not contain a pointer reference to a legitimate object.
3) Pointers have same size as data type 'int'. Since int is the default type specifier, some programmers
omit return type when defining a function. The return type defaults to ‘int’ which can later be
interpreted as a pointer. Therefore, programmer has to define explicit return types for
functions.
void swap (int *p, int *q) //both parameters are pointers to ints
{
int temp=*p; //declares temp as an int and assigns to it the contents of what p points to *p=*q;
//stores what q points to into the location where p points
*q=temp; //places the contents temp in location pointed to by q
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 15
DATA STRUCTURES AND APPLICATIONS 18CS32
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 16
DATA STRUCTURES AND APPLICATIONS 18CS32
• The above code would allocate an array of exactly the required size and hence would not result in
any wastage.
TWO DIMENSIONAL ARRAYS
Array-of-arrays representation
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 17
DATA STRUCTURES AND APPLICATIONS 18CS32
int *p;
p=(int*) calloc (n, sizeof(int)); //where n=array size
• To create clean and readable programs, a CALLOC macro can be created as shown below:
REALLOC
#define CALLOC(p,n,s) \
if((p=calloc(n,s))==NULL) \
{ \
printf("insufficient memory"); \
exit (0); \
}
• These functions resize memory previously allocated by either malloc or calloc.
For example,
realloc(p,s); //this changes the size of memory-block pointed at by p to s.
• When s>oldSize, the additional s-oldSize have an unspecified value and
when s<oldSize, the rightmost oldSize-s bytes of old block are freed.
• On successful resizing, it returns a pointer to the start of the new block. On failure, it returns the
value NULL.
• To create clean and readable programs, the REALLOC macro can be created as shown below
#define REALLOC(p,s) \
if((p=realloc(p,s))==NULL) \
{ \
printf("insufficient memory"); \
exit (0); \
}
ARRAY OPERATIONS
1. Traversing
➢ Let A be a collection of data elements stored in the memory of the computer. Suppose if the
contents of the each elements of array A needs to be printed or to count the numbers of
elements of A with a given property can be accomplished by Traversing.
➢ Traversing is a accessing and processing each element in the array exactly once.
Algorithm 1: (Traversing a Linear Array)
Hear LA is a linear array with the lower bound LB and upper bound UB. This algorithm traverses LA
applying an operation PROCESS to each element of LA using while loop.
1. [Initialize Counter] set K:= LB
Polynomials
• A polynomial is a sum of terms, where each term has a form axe,
Where x=variable, a=coefficient and e=exponent.
For ex,
A(x)=3x20+2x5+4 and B(x)=x4+10x3+3x2+1
• The largest (or leading) exponent of a polynomial is called its degree.
• Assume that we have 2 polynomials,
A(x)= ∑ai xi &
B(x)= ∑bi xi then A(x)+B(x)= ∑ (ai + bi) xi
polynomial a;
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 27
DATA STRUCTURES AND APPLICATIONS 18CS32
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 28
DATA STRUCTURES AND APPLICATIONS 18CS32
• startA & startB give the index of first term of A and B respectively.
• finishA & finishB give the index of the last term of A & B respectively.
avail gives the index of next free location in the array.
• Any polynomial A that has ‘n’ non-zero terms has startA & finishA such that finishA=startA+n-1
• Advantage: This representation solves the problem of many 0 terms since A(x)-2x1000+1 uses only 6
units of storage (one for startA, one for finishA, 2 for the coefficients and 2 for the exponents)
• Disadvantage: However, when all the terms are non-zero, the current representation requires
about twice as much space as the first one.
POLYNOMIAL ADDITION
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 29
DATA STRUCTURES AND APPLICATIONS 18CS32
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 30
DATA STRUCTURES AND APPLICATIONS 18CS32
int col;
int row;
int value;
} term;
term a[MAX_TERMS];
• a[0].row contains the number of rows; a[0].col contains number of columns and
a[0].value contains the total number of nonzero entries.
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 31
DATA STRUCTURES AND APPLICATIONS 18CS32
TRANSPOSING A MATRIX
• To transpose a matrix, we must interchange the rows and columns.
• Each element a[i][j] in the original matrix becomes element b[j][i] in the transpose matrix.
• Algorithm To transpose a matrix:
for all elements in column j
place element <i,j,value>
in element <j,i,value>
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 32
DATA STRUCTURES AND APPLICATIONS 18CS32
Strings
➢ A string is a sequence of characters. In computer science, strings are more often used than
numbers. We have all used text editors for editing programs and documents. Some of the
Important Operations which are used on strings are: searching for a word, find -and -replace
operations, etc.
➢ There are many functions which can be defined on strings. Some important functions are
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 33
DATA STRUCTURES AND APPLICATIONS 18CS32
The following declaration and initialization create a string consisting of the word "Hello". To hold the
null character at the end of the array, the size of the character array containing the string is one more
than the number of characters in the word "Hello."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
If you follow the rule of array initialization then you can write the above statement as follows −
char greeting [] = "Hello";
Note: Actually, you do not place the null character at the end of a string constant. The C compiler
automatically places the '\0' at the end of the string when it initializes the array.
Basic Terminology
Basic Terminology each programming language contains a character set that is used to communicate
with the computer. This set usually includes the following:
A finite sequence S of zero or more characters is called a string. The number of characters in a string is
called its length. The string with zero characters is called the empty string or the null string. Specific
strings will be denoted by enclosing their characters in single quotation marks. The quotation marks
will also serve as string delimiters. Hence
‘THE END’ ‘HELLO’ ‘WELCOME’
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 34
DATA STRUCTURES AND APPLICATIONS 18CS32
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 40
DATA STRUCTURES AND APPLICATIONS 18CS32
Complexity
The complexity of this pattern matching algorithm is equal to O (n2)
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 42
DATA STRUCTURES AND APPLICATIONS 18CS32
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 45
DATA STRUCTURES AND APPLICATIONS 18CS32
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 46
DATA STRUCTURES AND APPLICATIONS 18CS32
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 47
DATA STRUCTURES AND APPLICATIONS 18CS32
SATHISHA M S and SURESHA D DEPT of CSE CANARA ENGINEERING COLLEGE, MANGALURU Page 48
DATA STRUCTURES AND APPLICATIONS 18CS32
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.
STACK OPERATIONS
Implementation of the stack operations as follows.
1. Stack Create
Stack CreateS(maxStackSize )::=
#define MAX_STACK_ SIZE 100 /* maximum stack size*/
typedef struct
{
int key;
/* other fields */
} element;
element stack[MAX_STACK_SIZE]; int top = -1;
The element which is used to insert or delete is specified as a structure that consists of only a
key field.
4. 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].
5. 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.
element pop ( )
{ /*delete and return the top element from the stack */
if (top == -1)
return stackEmpty(); /*returns an error key */
return stack[top--];
}
6. stackFull( )
The stackFull which prints an error message and terminates execution.
void stackFull()
{
fprintf(stderr, "Stack is full, cannot add element");
exit(EXIT_FAILURE);
}
4. push()
Here the MAX_STACK_SIZE is replaced with capacity
void push(element item)
{ /* add an item to the global stack */
if (top >= capacity-1)
stackFull();
stack[++top] = item;
}
5. pop( )
In this function, no changes are made.
element pop ( )
{ /* delete and return the top element from the stack */
if (top == -1)
return stackEmpty(); /* returns an error key */
return stack[top--];
}
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;
}
Stack full with array doubling
Analysis
In the worst case, the realloc function needs to allocate 2*capacity*sizeof (*stack) bytes of
memory and copy capacity *sizeof (*stack)) bytes of memory from the old array into the new
one. Under the assumptions that memory may be allocated in O(1) time and that a stack element
can be copied in O(1) time, the time required by array doubling is O(capacity).
Initially, capacity is 1.
Suppose that, if all elements are pushed in stack and the capacity is 2k for some k, k>O, then
the total time spent over all array doublings is O(∑𝒌𝒊=𝟏 𝟐𝒊) = O(2 k+l ) = O(2 k ).
Since the total number of pushes is more than 2k-1, the total time spend in array doubling is
O(n), where n is the total number of pushes. Hence, even with the time spent on array doubling
added in, the total run time of push over all n pushes is O(n).
• The operators are arranged from highest precedence to lowest. Operators with highest
• The analysis of the examples suggests a precedence-based scheme for stacking and
unstacking operators.
• The left parenthesis complicates matters because it behaves like a low-precedence operator
when it is on the stack and a high-precedence one when it is not. It is placed in the stack
whenever it is found in the expression, but it is unstacked only when its matching right
parenthesis is found.
• There are two types of precedence, in-stack precedence (isp) and incoming precedence (icp).
Typedef enum { lparen rparen, plus, minus, times, divide, mod, eos, operand} precedence;
Precendence getToken(char *symbol, int *n)
{/*get the next token, symbol is the character representation, which is returned, the token is
represented by its enumerated value, which is returned in the function name*/
*symbol=expr[(*n)++];
switch(*symbol)
{
case ‘(‘: return lparen;
case ‘)‘: return rparen;
case ‘+‘: return lparen;
case ‘-‘: return minus;
case ‘/‘: return divide;
case ‘*‘: return times;
case ‘%‘: return mod;
case ‘ ‘: return eos;
default: return operand;
}
}
stack[0] = eos;
for (token = getToken(&symbol, &n); token != eos; token = getToken(&symbol,& n ))
{
if (token == operand)
printf("%c", symbol);
else if (token == rparen)
{
while (stack[top] != lparen)
printToken(pop( ));
pop( );
}
else {
while(isp[stack[top]] >= icp[token])
printToken(pop());
push(token);
}
}
while((token = pop ())!= eos)
printToken(token);
printf("\n");
}
Program: Function to convert from infix to postfix
Analysis of postfix: Let n be the number of tokens in the expression. Ө (n) time is spent
extracting tokens and outputting them. Time is spent in the two while loops, is Ө (n) as the
number of tokens that get stacked and unstacked is linear in n. So, the complexity of function
postfix is Ө (n).
this fashion until the end of the expression. We then remove the answer from the top of the
stack.
int eval(void)
{
precedence token;
char symbol;
int opl,op2, n=0;
int top= -1;
token = getToken(&symbol, &n);
while(token! = eos)
{
if (token == operand)
push(symbol-'0'); /* stack insert */
else {
op2 = pop(); /* stack delete */
opl = pop();
switch(token) {
case plus: push(opl+op2);
break;
case minus: push(opl-op2);
break;
case times: push(opl*op2);
break;
case divide: push(opl/op2);
break;
case mod: push(opl%op2);
}
}
token = getToken(&symbol, &n);
}
return pop(); /* return result */
}
Program: Function to evaluate a postfix expression