Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

DATA STRUCTURES USING C

DS NOTES
CLASS: II SEM BCA 22-BATCH

UNIT – I: Introduction to Data Structures and Arrays

Data Structures: Definition, Elementary data organization, Data Structures,


operations, Abstract data types, algorithms, complexity, time-space trade
off. Preliminaries: Mathematical notations and functions, Algorithmic
notations, control structures, Complexity of algorithms, asymptotic
notations for complexity of algorithms.

Arrays: Definition, Representation and analysis, Single and


Multidimensional Arrays, Linear arrays, arrays as ADT, Traversing Linear
arrays, Inserting and deleting, Matrices and Sparse matrices, application of
arrays.

2marks Questions
1. Definition of Data Structures.
2. Why data structure is required?
3. What is Primitive data structure
4. What is Non-Primitive data structure
5. What is an Arrays
6. What are Stacks and Queues?
7. What is linked list?
8. What are trees and graph?
9. What is Abstract Data Types ?
10. What are the 2 main measures for efficiency of an algorithm?
Time and space complexity are the two main measures for calculating algorithm
efficiency, determining how many resources are needed on a machine to process it.
Where time measures how long it takes to process the algorithm, space measures how
much memory is used
11. What is the efficiency of algorithm?

DS UNIT1 NOTES (INTRODUCTION to DS and Arrays) Faculty: Ms. Anitha Page 1


An algorithm's efficiency is referred to as the number of computational resources used
by the algorithm.
12. What is OPERATION COUNTS .
13. What is step count?
14. List the area of applications of Data Structure.

6 MARKS QUESTIONS
1. Explain classification on data structures.
2. What is best , worst and average case time complexity? explain with example.
3. Explain Asymptotic notations?
4. Explain types of arrays.
5. What is Traversing the linear array? Write its algorithm.
6. Write an algorithm to insert an element into an array.
7. Write an algorithm to Delete element from array.

10 MARKS QUESTIONS
1. Write the differences between Linear and non-linear data structures. 5M
2. Explain various operations performed on data structures. 5M
3. DEFINE TIME COMPLEXITY AND SPACE COMPLEXITY.
4. Explain Array as abstract data type?
5. Define sparse matrix. Explain its representation.
6. Explain the 2 Representations of SPARSE MATRIX
7. What ARE THE Application of arrays.

Definition of Data Structures. 2m


• “The organized collection of data is known as data structure”.
• The data structure deals with the study of how data is organized in the memory.
• Data Structure = Organized Data + Operations.

Some examples of Data Structures are Arrays, Linked Lists, Stack, Queue, Trees, etc.
Data Structures are widely used in almost every aspect of Computer Science, i.e.,
Compiler Design, Operating Systems, Graphics, Artificial Intelligence, and many more

Why data structure is required?


• Gives more efficient programs.
• performance of a program will be high
• More powerful computers encourage more complex applications.
• More complex applications demand more calculations.
• Complex computing tasks are unlike our everyday experience.

Explain classification on data structures. 6m


What are linear data structures? Name any two linear data structure. 5m/6m

DS UNIT1 NOTES (INTRODUCTION to DS and Arrays) Faculty: Ms. Anitha Page 2


Primitive data structure :
The primitive data structures are known as basic data structures. They are directly operated by
the machine instructions. It has different representation on different computers.
Example
 Integer
 Float
 Character
 Pointer
 boolean

Integer :
The integers are signed or unsigned whole numbers with the specified range . They have no
fractional parts. Integers can be positive or negative.
Float :
Float refers floating point or real number. It can hold a real number or a number having
a fractional part. The decimal point signals that it is a floating point number, not an integer. The
number 15 is an integer but 15.0 is a floating point number.
Character :
It can store any member of the basic character set which is stored in a character variable. It can
hold one letter/symbol like a, B, d etc. It takes 1 byte.
Pointer :
Pointer is a variable which holds address of another variable.

Non-Primitive data structure :


 derived from primitive data structure.
 They are complex data structures.
 Responsible for organizing group of homogeneous and heterogeneous data elements.
Example- Arrays, Linked Lists, Stacks, queue, Trees, graphs
Linear data structures

 are arranged sequentially or linearly .


 In linear data structure, single level is involved.
DS UNIT1 NOTES (INTRODUCTION to DS and Arrays) Faculty: Ms. Anitha Page 3
 Therefore, we can traverse all the elements in single run only
 Examples - array, stack, queue, linked list, etc.

Non-Linear Data structures


 are not arranged sequentially or linearly.
 single level is not involved.
 Therefore, we can not traverse all the elements in single run .
 not easy to implement in compared to linear data structure.
 Examples - trees and graphs.

Arrays :
 “Array is collection of homogeneous elements stored in continuous memory
locations with same data type”.
 It s non-primitive linear data structure.
 Example: int a[10];

Stack :
 Stack is a non-primitive, linear data structure
 set of elements are stored in LIFO(last in first out ) order.
 Elements are inserted and deleted at one end called TOP.
 It has 2 operations PUSH and POP.

Queues :
 Queues are non-primitive linear data structure
 set of elements are stored in FIFO(First in First out) order.
 Elements are inserted at REAR and deleted at FRONT one end .
 It has 2 operations INSERT and DELETE.

We can also implement queues using 2 ways :


 Using arrays
 Using pointers

Linked Lists
 non-primitive linear data structure

DS UNIT1 NOTES (INTRODUCTION to DS and Arrays) Faculty: Ms. Anitha Page 4


• Linked Lists are dynamic data structures
– They grow or shrink one element at a time.
• A linked list is a series of connected nodes
• Each node contains at least
– A piece of data (any type)
– Pointer to the next node in the list
• Head: pointer to the first node
• The last node points to NULL.

: Trees
 Trees are non-primitive , non-linear data structures .
 They contain a finite set of data items referred as nodes.
 We can represent a hierarchical relationship between the data elements using trees.

Graph :
 Graph is non-primitive, non-linear data structure.

 Is a combination of set of vertices V and set of edges E.

DS UNIT1 NOTES (INTRODUCTION to DS and Arrays) Faculty: Ms. Anitha Page 5


Explain various operations performed on data structures. 5M

 Inserting- Adding the new element to the existing data structure.


 Deleting- Deleting the element from the data structure.
 Traversing- Accessing each element exactly once is called traversing.
 Searching- Searches a particular element in the list of elements.
 Sorting- Arranging the elements in ascending or descending order.
 Merging- Combining the two different sorted lists into single sorted list.

What is Abstract Data Types ? 2m

The Data Type is basically a type of data that can be used in different computer program.
Integer takes 2 bytes, float takes 4 bytes etc, character will take 1-byte of space etc.
The abstract datatype is special kind of datatype, whose behaviour is defined by a set of
values and set of operations.
The keyword “Abstract” is used as we can use these datatypes, we can perform different
operations. But how those operations are working that is totally hidden from the user. The ADT
is made of with primitive datatypes, but operation logics are hidden.
Some examples of ADT are Stack, Queue, List etc.

 Stack − isFull(), isEmpty(),


o isFull(), This is used to check whether stack is full or not
o isEmpty(), This is used to check whether stack is empty or not
o push(x), This is used to push x into the stack
o pop(), This is used to delete one element from top of the stack
o peek(), This is used to get the top most element of the stack
o size(), this function is used to get number of elements present into the stack
 Queue − insert(x), delete(),
o isFull(), This is used to check whether queue is full or not
o isEmpty(), This is used to check whether queue is empty or not
o insert(x), This is used to add x into the queue at the rear end
o delete(), This is used to delete one element from the front end of the queue
o size(), this function is used to get number of elements present into the queue
 List − size(),insert(x), remove(x),
o size(), this function is used to get number of elements present into the list
o insert(x), this function is used to insert one element into the list
o remove(x), this function is used to remove given element from the list
o get(i), this function is used to get element at position i
o replace(x, y), this function is used to replace x with y value

DS UNIT1 NOTES (INTRODUCTION to DS and Arrays) Faculty: Ms. Anitha Page 6


Efficiency of an algorithm - What is best , worst and average case time complexity?
• Worst case : the efficiency of an algorithm for the input of size n for which the algorithm
takes longest time to execute among all possible inputs.
• Best case : the efficiency of an algorithm for the input of size n for which the algorithm
takes least time during execution among all possible inputs of that size is called best
case.
• Average case: The running time for any given size input will be the average number of
operations over all problem instances for a given size.

An example to illustrate best,worst and average case performance for Linear Search.

Algorithm Lin_srch(A,N,key)

// A is an array of size N & key is the element to be searched


{
for i=1 to N do
{
if a[i] == key then
return i;
}
}

ANALYSIS

1. Best case: occurs when the element to be searched is found at the first location.
No. of comparison =1
Tbest(n) = 1

2. Worst case: It occurs when the element to be searched is found at the last position or
element to be searched is not found at any location.
No. of comparisons = n
Tworst(n) = n
3. Average case: It is the probability of finding an item at any position is same and is 1/n.
Therefore no of comparisons = 1. 1/n + 2. 2/n + …… + n. 1/n
= 1/n. (1+2+3……+n)
= 1/n . (n(n+1)/2)
= (n+1)/2

DEFINE TIME COMPLEXITY AND SPACE COMPLEXITY 5M

“Time complexity of an algorithm is the amount of computer time a program needs to


complete its execution”.

DS UNIT1 NOTES (INTRODUCTION to DS and Arrays) Faculty: Ms. Anitha Page 7


The time T(P) taken by a program P is the sum of the compile time and the execution time.

The compile time does not depends on the instance characteristics. Bcoz, a compiled program
can be run several times without recompilation.

Therefore, run time is denoted by tp (instance characteristics).

How do we measure?

1. OPERATION COUNTS
2. STEP COUNT
3. ASYMPTOTIC NOTATIONS

OPERATION COUNTS 2m

It is the simplest way to analyze .In this method count the number of basic operations in an
algorithm .

Basic operation

• The operation that contributes towards the running time of the algorithm is called basic
operation.
• A statement that executes maximum number of times in a function is called basic
operation.
Examples
A statement present in the innermost loop.
for (i=0;i<n;i++)
if(a[i]<a[j]) -------- basic operation (1 basic operation)

What is step count? 2m

“It is a method of measuring time complexity by introducing COUNT statements in the given
algorithm”

DS UNIT1 NOTES (INTRODUCTION to DS and Arrays) Faculty: Ms. Anitha Page 8


Common orders of functions

notation name

• O(1) constant
• O(log n) logarithmic
• O([log n]c) polylogarithmic
• O(n) linear
• O(n · log n) sometimes called "linearithmic" or
• "supralinear"
• O(n2) quadratic
• O(nc) polynomial, sometimes "geometric"
• O(cn) exponential
• O(n!) factorial

Explain Asymptotic notations? 6m/10m

DS UNIT1 NOTES (INTRODUCTION to DS and Arrays) Faculty: Ms. Anitha Page 9


DS UNIT1 NOTES (INTRODUCTION to DS and Arrays) Faculty: Ms. Anitha Page 10
Try these examples:

f(n)=5n+5 then given g(n)=n

f(n)=n2+2n+1 g(n)=n2

f(n)=n2+30n+1

DS UNIT1 NOTES (INTRODUCTION to DS and Arrays) Faculty: Ms. Anitha Page 11


ARRAYS : DEFINITION

 “Array is collection of homogeneous elements stored in continuous memory


locations with same data type”.
 It s non-primitive linear data structure.
 Example: int a[10];

Types of arrays:

1. Linear array/ one dimensional array.


2. 2-D dimensional array.
3. Multidimensional array.
Linear arrays

An array is a linear data structure that collects elements of the same data type and stores them
in contiguous and adjacent memory locations. Arrays work on an index system starting from 0
to (n-1), where n is the size of the array

Abstract Data Types. Abstract Data type (ADT)

An abstract data type (ADT) is a mathematical model for data types, where a data type is
defined by its behaviour (semantics) from the point of view of a user of the data, specifically in
terms of possible values, possible operations on data of this type, and the behaviour of these
operations.

Explain Array as abstract data type? 5m

• The array is a basic abstract data type that holds an ordered collection of items
accessible by an integer index. These items can be anything from primitive types such as
integers to more complex types like structures.

• The basic operations include direct access to each element in the array by specifying its
position.

DS UNIT1 NOTES (INTRODUCTION to DS and Arrays) Faculty: Ms. Anitha Page 12


What is Traversing the linear array? Write its algorithm. 5m

Traversing means moving from one location to all the other locations in sequential way and
accessing the elements for various operations like display, insert, delete, search etc

Algorithm: traverse(A,N)

//input: A is a linear array , N is the size of the array.

//output: process the elements

for I = 0 to N

Apply process to A[I]

end loop

Write an algorithm to insert an element into an array. 5m

Algorithm : Insert (A , N, pos, item)


// input: A is linear array,N is the size of the array, pos is the position,
// item is the value to be added.
//output: value is added
i=N
while i > = pos
A[i+1] = A[i]
I = I-1
end loop
A[pos] = item
N =N +1
Write an algorithm to Delete element from array. 5m
Algorithm : Delete (A , N, pos, item)

DS UNIT1 NOTES (INTRODUCTION to DS and Arrays) Faculty: Ms. Anitha Page 13


// input: A is linear array,N is the size of the array, pos is the position item is the value to be
added.
//output: value is removed from the array
item= A[pos]
For I = pos to N
A[I] =A[I +1]
end loop
N = N-1

Define sparse matrix. Explain its representation. 5m


A sparse matrix is a matrix in which many or most of the elements have a value of zero.

A matrix is a two-dimensional data object made of m rows and n columns, therefore having
total m x n values. If most of the elements of the matrix have 0 value, then it is called a sparse
matrix.

Why to use Sparse Matrix instead of simple matrix ?


 Storage: There are lesser non-zero elements than zeros and thus lesser memory can be
used to store only those elements.
 Computing time: Computing time can be saved by logically designing a data structure
traversing only non-zero elements..

Explain the 2 Representations of SPARSE MATRIX


1. Array representation
2. Linked list representation

1. Array representation
2D array is used to represent a sparse matrix in which there are three rows named as
 Row: Index of row, where non-zero element is located
 Column: Index of column, where non-zero element is located
 Value: Value of the non zero element located at index – (row,column)

DS UNIT1 NOTES (INTRODUCTION to DS and Arrays) Faculty: Ms. Anitha Page 14


2. Linked list representation

In linked list, each node has four fields. These four fields are defined as
 Row: Index of row, where non-zero element is located
 Column: Index of column, where non-zero element is located
 Value: Value of the non zero element located at index – (row,column)
 Next node: Address of the next node

What ARE THE Application of arrays.6M


1. Arrays are used to implement data structures like a stack, queue, etc.
2. Arrays are used for matrices and other mathematical implementations.
3. Arrays are used in lookup tables in computers.
4. Arrays can be used for CPU scheduling.
5. Arrays are commonly used to store large amounts of data.
6. Arrays are used in the implementation of various graph and tree data structures.

List the area of applications of Data Structure.5m


• Data structures are applied extensively in the following areas of computer science:
• Compiler Design,
• Operating System,
• Database Management System,
• Statistical analysis package,
• Numerical Analysis,
• Graphics,
• Artificial Intelligence,
• Simulation

DS UNIT1 NOTES (INTRODUCTION to DS and Arrays) Faculty: Ms. Anitha Page 15

You might also like