dsu_ 1lesson
dsu_ 1lesson
Sem.II
NewSyllabus
MSBTE
Asper newrevised K
Scheme w.e.f. academicyear 2024-2025
DATA STRUCTURE
USING C
(Code : 313301)
Second Year Diploma - Semester III- Computer Engineering
Information Technology / Cloud Computing &Big Data /
CT/CSE/ CHM/CS &IT [BD/CM/CO/CW/HA/IF/IH]
Dilip Kumar Sultania
TM
Includes :
"Solved previous MSBTE Question Papers.
"Lab Assignments.
Tech Knowledge
PubliC ations
Data Structure
Using C'
(Code : 313301)
DilipKumar Sultania
B.Tech.(hons.) Computer Science and Engineering
LI.T., Kharagpur
TechKnowledge
PubI icati ons
MDO97A Price 355/
LABORATORYLEARNINGOUTCOME
EXPERIENCES
TUTORIAL
PRACTICAL/ Numberof Relevant
ALIGNED TItles COs
AND Titles/Tutorial
Practlcal
hrs,
Experiment / CO1
Laboratory 4
Create,
Sr.
Operations on Array :
No following
program to perform CO2
Write a C
given Array of
1
Insert, Delete, Display.
from the
particular data
a
Program toSearch C02
2 Write a'C Method. 2
LinearSearch given Array of
numbers using: from the
particular data
Program toSearch a
3 Write a 'C' Method. 2
C02
LinearSearch given Array of
Strings using
particular data from the
program to Search a
4 Write a 'C' Method. C02
BinarySearch 2
numbers using given Array of
particular data from the
to Search a
Write a C Program
Search Method. CO2
Strings using Binary using Bubble Sort
2
Array of numbers
C Program to Sort an
6 Write a
Method. 2 CO2
Method.
Array of Strings using Bubble Sort
Sort an
7 Write aC Program to Sort 2 CO2
Array of numbers using Selection
to Sort an
8 Write a 'C Program
Method. 2 C02
Selection Sort
Program to Sort an Array of Strings using
9 Write a 'C
Method.
2 C02
Insertion Sort
Program to Sort an Array of numbers using
10 Write a C
Method.
Sort 2 CO2
Array of Strings using Insertion
11 Write a 'C Program to Sort an
Method.
CO3
List with Operations 2
12 Write a 'C' Program to Implement Singly Linked
() Insert at beginning, (i)Search, (ii) Display
C03
13 Write a C Program to Implement Singly Linked List with Operations : 2
14 2 C03
Write a CProgram to Create Two Polynomials using a Linked List.
15 Write a 'C Program to add Two Polynomials using a Linked List. 2 C03
16 Write a 'C' Program to perform PUSH and POP Operations on Stack using 2 C04
an Array.
17 Write a 'C' Program toperform PUSH and POP Operations on a Stack using 2 C04
a Linked List.
Sr. Laboratory Experiment /Practical Titles/ Tutorial Titles Number of
No Relevant
hrs. COs
18 Write a C program to perform
recursion. multiplication of two numbers using CO4
|13
Operations on Data Structures: (0) Traversing Measuring the Running Time ofa Program
1.8.2
(ü) Insertion (ü) Deletion .1-12
(Time Complexity).
1.1 Data Representation....n 1-1 Measurement of Growth Rate (Asymptotic
1.8.3
Unit - II
1.1.6 Structure. 1-3
1.3.2 Linear and Non-Linear Data Structures.....1-6 |2.2 Sorting : Sorting of data set in an order using the
following methods :0) Bubble Sort (i) Selection Sort
1.3.2(A) Difference between Linear and Non-linear Data (ii) Insertion Sort (iv) Quick Sort (v) Merge Sort.
Structure. 1-7
2-1
2.1 Searching.
1.4 Classification of Data Structure........ 1-7
2.1.1 Sequential Search (Linear Search). .2-1
nforence between Linear Search and Binary 2.7,4 Advantages and Disadvantages of Merge Sort..
2.1.4
Search .2-11 .2-40
Passes .2-13
Syllabus
2.23
3.1 Difference between Static and Dynamic Mernory
Bubble Sort. .2-13
2.3 Allocation.
Selection Sort. 3.2 Introduction to Linked List, Terminologies : Node,
2.4 .2-16
Address, Pointer, Information field / Data field, Next
2.5 Insertion Sort.... .2-18 pointer, Null Pointer, Empty List.
Sorting an Array of Strings using Insertion Sort 3.3 Type of Lists : Linear List, Circular List, Representation
2.5.1
of Doubly Linked List.
.2-21
3.4 Operations on a Singly Linked List :Creating a Linked
2.5.2 Sorting an Array of Records on the given Key List, Inserting a new node in a Linked List, Deleting a
using Insertion Sort. .2-22
node from a Linked List, Searching a key in Linked
List, Traversing a Singly Linked List.
2.6 Quick Sort. ..2-27 3.5 Applications of Linked List.
2.6.3 Running Time of Quick Sort.... .2-32 3.1.1 Comparison between Array and Linked Lists....
3-1
2.6.3(A) Worst-case Analysis. ...2-32
3.1.2 Representation ... .3-2
2.6.3(B) Best-case Analysis. .2-32
3.1.3 Implementation .3-2
2.6.3(C) Average-case Analysis... .2-32
3.1.4 Types of Linked List.. ..3-4
Tech kaewledgi
Data Structure Using C 3
Table of Content.
3.2 Basic Linked List Operations. 3-6 3.5.1 Polynomials as Linked Lists
3-26,
3.21 Creating a Linked List. 3-6 3.5.2 Addition of Two Polynomials..
327
32.2 Traversing aLinked List. 3-7
Unit - IV
3.2.3 Counting Number of Nodes in a Linked List
through Count Function "..3-8 Chapter 4: Stack 4-1 to 4-44
3.2.4 Printing a List through Print Function...3-9 Syllabus
3.2.5 Inserting an Item.. .3-9 |4.1 Introduction to Stack: Definition, Stack as an ADT
Operations on Stack-(Push, Pop), Stack Operatione
3.2.5(A) inserting an Item at the End of a Linked List..
Conditions Stack Full/ Stack Overfiow, Steck
.3-10
Empty/Stack Underflow.
3.2.5(B) Inserting a Data 'x at a Given Location "LOC' in |4.2 Stack Implementation using Array and representation
a Linked List, Referenced by 'Head'. .3-10 using Linked List.
3.2.5(C) Inserting an Element in a Priority Linked List... |4.3 Applications of Stack : Reversing a List, Polish
Notations, Conversion of Infix to Postfix Expressior.
.3-11
Evaluation of Postfix Expression.
3.2.6 Deleting an Item. .3-12 |44 Recursion: Definition and Applications.
3.2.6(A) Deletion of the Last Node of a Linked List..3-13 4.1 Introduction to Stack.
32.6(B) Deletion of a Node at Location 'LOC" from a 4.2 Stack as an ADT.
Linked List. .3-13 4.3 Operations on StackS 4-2
4.4 Array Representation. 4-4
3.2.6(C) Delete a Linked List, Referenced by the Pointer
Head 3-14
4.4.1 'C Functions for Primitive Operations on a
Stack.. 4-4
3.2.7 Concatenation of TwoLinked Lists .....3-15 4.5 Program Showing Stack Operations ...4-5
3.2.8 Inversion of Linked List. 4.5.1 Operations on Stack Considering Overflow and
.3-15
Underflow [Array Implementation.4-7
3.2.9 Searching a Data 'x' in aLinked List, 4.5.2 Reversing a List.... 4-8
3.2.10 Searching an Element x in aSorted Linked List 4.6.1 Functions for Stack Operations. 4-10
s..... 3-17 4.7 Applications of Stack. 4-12
5-1
Depth / Height of a Tree, In-Degree and Out-Degree,
5.1 Introduction. Path, Ancestor and Descendant Nodes.
5.1.1 Array Representation and Implementation of 6.2 Tree Types and Traversal methods, Types of Trees
Queues... 5-1
General Tree, Binary Tree, Binary Search Tree (BST).
5.1.2 Comparison between STACK and QUEUE....5-2 Binary Tree Traversal: In-Order Traversal, Preorder
5.1.3 Traversal, Post-Order Traversal.
Queue Overflow and Underflow.............5-2
6.3 Expression Tree, Heap.
5.2 Operations on Queue (Implemented using
Array) 5-4 6.1 Introduction to Trees bs.. 6-l
Techknowtedge
Data Structure Using C Table of Contents
6.2 Binary Tree. 6-3 6.7 Binary Search Tree (BST).
.6-17
6.2.1 Representation of a Binary Tree using an Array 6.7.1 Definition ...
.6-17
6-3
6.7.2 Operations on a Binary Search Tree....6-1
6.2.2 Linked Representation of aBinary Tree...6-4
6.7.2(A) Initialize Operation.. 6-18
6.3 General Tree. ..6-5
6.7.2(B) Find Operation... ..6-18
6.3.1 Node Declaration for a Tree. .6-5
6.7.2(c) Makeempty Operation .6-18
6.3.1(A) Comparison between General Tree and Binary
Tree .6-7
6.7.2(D) Insert Operation. 6-18
6.4
6.7.2(E) Examples on Creation of a BST. .6-19
Types of Binary Tree. .6-7
6.7.2(F) Delete Operation... 6-25
6.4.1 Full Binary Tree.... .6-7
6.4.2
6.7.2(G) Create .6-27
Complete Binary Tree. .6-7
6.7.2(H) FindMin .6-27
6.4.3 Skewed Binary Tree... 6-8
6.7.2(1) FindMax.. ...6-28
6.4.4 Strictly Binary Tree........ 6-8
6.7.3 Program for Various Operations on BST...6-28
6.4.5 Extended Binary Tree (2-Tree) 6-8
6.8 Application of Trees. .6-30
6.5 Binary Tree Traversal.. .6-8
6.5.1
6.8.1 Expression Trees. .6-30
Preorder Traversal (Recursive)......6-9
6.8.2 Conversion of an Expression into Binary Tree
6.5.2 Inorder Traversal (Recursive),....6-10
sedesebepsstspeb!.6-31
6.5.3 Postorder Traversal (Recursive)J ...6-11
List of Practicals.... .L-1 to L-31
6.6 Tree Traversal Examples. ..6-11
Introduction to Data Structures
Syllabus
troduction :Concept and Need of Data Structure, Definition, Abstract Data Type
. Tvoes of Data Structures: (i) Linear Data Structures (ii) Non-Linear Data Structures
Signed Urnaigned
TechKaowledge
Using C 1-3 Introduction to Data Structures
Data Structure
Program 1.1.1: Program to accept and display 'int' and 1.1.5 Derived Data Type
'char' type of data. W-15
Q. Explain the term: Derived data type.
#include <stdio.h>
elements is stored in
# include <conio.h> Array :Agroup of similar
today =Thu; *A valid statement */ structure can be used for storing of non-homogeneous
elements.
if (today == Mon) /*A valid statement */
1.1,7 Union
1-4
Lvol 0
Man
Structures
Progrom
Ohiects such as lists, sets and graphs along with 4 Delete(L, P): Delete the element at position P
associated operattons, can be viewed as abstract of the list L.
data type. Integer, char, real are primitive data 5 MAKENULL(L): Creates an emptyiist L.
unes and there are set of operations associated 6 PrintList(L): Print the elements of L in the
with them. For the 'set' ADT (Abstract Data Type), order ofoccurrence.
We might have operations like union, intersection, 1.3 Concept of Data Structures
size and complement. Once the data type set is
Q. Define data struoture. Enlist any two types of non
defined (representation and associated functions)
linear data structure. W-15
then the ADT set can be used in any application
W-16
program. Q. Define data structure.
The abstract data type "List" The data structure is concerned with the following
A
list is a sequence of0 or more elements of agiven things :
type (element type could be integer, float etc.). Such 1 Organization of data
a list is often represented as: 2 Associativity among data elements
3 Accessing methods
n= Number of elements in the list 4. Operations on data
a = i element of the list 5 Processing alternatives for data.
When n =0, the list is empty having no element.
While representing data, relationship between
Each element other than a has a predecessor individual elements of the data should. be
Each element, other than a, has a successor. considered. The data structure deals with
In order to form an ADT from the mathematical representation of data considering not only the
notation of a list, we must represent the list and elements stored but also their relationship to each
define a set of operations on objects of type List. other.
AList of maximum of 50 integer type elements A data structure can also be defined as an instance
of ADT. A data structure is formally defined to be a
n is the actual number of elements in the List.
triplet (D, F, A)
A
set of representative operations on a list where, D
stands for a set of domains
1. Insert(L,X, P): Insert Xat position Pin list L. F stands for a set of operations
2 Locate(X, L): This function returns the position Astands for a set of axioms.
of element X on list L.
1.3.1 Need of Data Structure
3 Retrieve(L, P): This function returns the element
at position P on list L. Q. State the need of data structure. S-16
TechKnouledge
Introductionto Data
Data Structure Using C 1-6Representation of linear data structures throug Structure
There is a close connection between the
structuring linked structure
of data and the synthesis of algorithm. For each
data
item, there are number of operations. Data should
be represented in a way that makes efficient
A Ast of
implementation of operation. Elerments
Addrogs of the ext
plement
If a linear
data is represented using linked Top
organization then modification is less time
Stack
consuming whereas accessing a large list becomes
inefficient. In sequential organization, accessing is Front
Rçat
efficient but modifications are costly.
Cuoue
1.3.2 Linear and Non-Linear Data Structures
Q. Explain linear and non-linear data 2, Non-linear
structures.
W-15, S-23, W-23 relation.
All one-many, many-one or many-Many
Q. Define linear data structure and non-linear data
are handled through non-linear data structure
structure. | W-18, S-22 Every data element can have a number
predecessors as well as successors. Tree graphs ant
Types of Data Structure
tables are examples of non-linear data structures.
1. Linear
2. Non- Linear
th
0element (n-1) eBement
Top
9
Stack 23
Front Rsar
Tech Kaouiedg!
Data Structure Using C . no
osflernerts
1-7 Introduction to Data Structures
- r oe
o ttorrent
root
toot
St. Linear Non-Linear
child
of
o f
c h i l d
No.
elements
left
rigta
3 Data elements are Data are
(b) Representation of1the binary tree through an array 4 Memory is not Memory is utilized in an
Fia. 1.3.3:Representation of tree of Fig. 1.3.2(a) utilized in an efficient way.
efficient way.
1.3.2(A) Difference between Linear and Non
linear Data Structure 1.4 Classification of Data Structure
O. Differentiate between lineár and non-linear data W-16, W-19
Q. Give classification of data structure.
structure. (any four points). W-22
a. Descirbe classification of data structure with example
Sr. Linear Non-Linear of each. W-17, S-18
No.
Data structures are normally divided into two
1 Elements are Elements are
categories:
sequentialy hierarchically
connected connected. 1 Primitive data structure
Primitive Non-Primitive(Derived)
Linear Non-Linear
Primitive
The integers, reals,logical data, character data, pointers and reference are primitive data structures. These data
types are available in most programming languages as built in type. Data objects of primitive data types can be
operated upon by machine level instructions.
Tech Knoaledqë
Structures
Introductionto
Data
Data Structure Using 1-8 structure,
data
datainthe
Non-primitive 3. Inserting is adding a new structure.
from the data
Deleting is removingaa data
These data structures are derived from prímitive 4
somelogicalorder.
data in
data structures. A set of homogeneous and Sorting is arranging of
data structures.
5
heterogeneous data elements are stored together. combining oftwo similar
6 Mergingis
Examples of Non-primitive data structures : Introduction to Algorithm
1.6
Array, structure, union, linked-list, stack, queue, S-16, S-18
on a data structure.
Many operations are performed (2) Output
structure are:
Typicaloperations on a data (3) Finiteness
Operation of Data
Structure
(4) Definiteness
1. Traversing
(5) Efectiveness
2. Searching
Fig. 1.6.1 : Properties of Algorithm
3 insertion
(zero or
(1) Input: Input data, supplied externally
4, DéletiGn
more).
5. Sorting (2) Output : Result of the program.
algorithm terminates
6. Merging (3) Finiteness : In every case,
after a finite number of steps.
Fig. 1.5.1:Opeation of Data Structure should be clear and
(4) Definiteness: The steps
1 Traversing a data structure is accessing each data unambiguous.
should be written
and accessing only once. (5) Effectlveness : An algorithm
should be feasible to
2. Searching is finding the location of a data in within using basic instructions. It
the given data structure. convert the algorithm in a computer program.
TechKnowledge
Using C
Data Structure 1-9
Take the problem of finding the GCD (Greatest Introduction to Data Structures
Common Divisor) of two positive integers an Table 1.6.1
example. Steps Value of the
Inputs to the algorithm are two positive integers. performed varlables
Output is a positive integer which is GCD of two
positive integers given as input. Step 1 15
input data supplied
1Number
Algorithm
externally
tor finding
Step 2 15 1
Number GOD GCD
Step 3 15 9 6 1
Step 4 9 6 1
Step 6 9 6 6 1
Step 5 read x
Table 1.6.2: Symbols used in flowcharts
Step 6 Assign SUM +XTo SUM
Symbol Purpose
Step 7 : Assign I+ 1 to !
Terninal SymtoB
(Start/ Stop) Step 8 Go to step 4
Step9 : Print SUM
Assignment Statements,
express0on eto. Step 10: Stop
Soln. :
Statement Frequency Computation
tlme Statement No. Frequency
1
to be added"):
2 n+1
scanf("%d", &n) 1
3
for (i =1 ;i<=n;it+) n+1 (n+1) t, n
4
scanf ("%d", &x) n nt,
1,8.2 Measuring the Running Time of a
Sum sum +X n nt, Program (Time Complexity)
|printf("\n sum =%d", sum) t; Running time of a program can be judged on the
basis of factors such as:
Total computation time
1. Input to the program.
: t;+ty+t, +n(t+ ts tte) +t,tt,
2 Size of the program.
T= n(t+ t;+ te) +(t; +t;+tgt t4tt) 3 Machine language instructionset.
For large n, Tcan be approximated to 4 The machine we are executing on.
T n (t,+ t;+ t) =kn 5 Time required to execute each machine instruction.
where 6. The time complexity of the algorithm of the
k = tt ts + 7 . . aconstant value. program.
Ex, 1.8.1: Determine the frequency count for all statements ,{(n) =100 n', f2 (n) =5 n'
in the foliowing program segment Representing time complexities of two solutions of
i= 1; a problem.
2. whise (i <e n) f,(n) ,(n)
100
5 2500 625
4 10 10000 5000
20 40000 40000
Tech Knouledge
Using C 1-13 introduction to DataStructures
Data Structure
s 20 1.9 The Notation O: (Pronounced as
f, (n) 2f, (n) for n
We would still prefer the solution having f,(n) as
big-oh), (O(n) is Pronounced as big
time complexity because, fi(n)sfh(n) for all
n> 20. oh of n')]
1.8.3(B) Constant Factor in Complexity Q Define Big 'O Notation. W-16
Measure
Provides asymptotic upper bound for a given
Let us consider an algorithm having a timing functions. Let f(x) and g(x) be two functions then f(x) is
complexity given by the function f(n)=100n' said to be O(g(x)) if there exist two positive integer/real
number constants c andk such that
where [100- a constant, n - size of the problem].
The time required for solving a problem, depends f(x) < cg(x) for all x 2k
not only on the size of the problem but also, on the The constant k is due to asymptotic consideration
hardware and software used to execute the and the constant c for hardware/software environment.
solution. The effect of hardware and software on
Ex. 1.9.1:f(x) =x +5x
the time required may closely be approximated by a
constant.
g(x) =x let us takec=2
Soln. :
Suppose, a new Computer executes aprogram tWo
X x²+ 5x 2x?
times aster than another computer. Then
irrespective of the size of the problem, the new 1 5 2
TechKaowledge
Data Structure UsingC 1-14 Introduction to Data Structurec
Caiculating the running time of a program statement will be executed is called its frequencu
count. Frequency count may vary from data set to
Ex. 1.9.3: Arrange the folowing computing tunctions as per
data set.
their growth rate. log, n, n, nlog, n, n,n',2".
Soln.: The constant factor incomplexity measure
Common computing functions and their growth Let us consider an algorithm having a timing
rates
complexity given by the function f (n) =100 n?
log, n n nlog,n n 2 where, [100- a constant, n - size of the problem].
1 1 1
The time requíred for solving a problem, depends
2
not only on the size of the problem but also on the
1 2 2 4 8 4 hardware and software used to execute the
solution. The effect of hardware and software on
4 16 64 16 the time required may closely be approximated by a
constant.
8 24 64 512 256
Suppose, a new computer executes a program two
4 16 64 256 4096 65536 times faster than another computer. Then
32 160 1024 32768|2, 147, 483, 648 irrespective of the size of the problem, the new
computer solves the problem roughly two times
Thus, 0(2n) >0(n°) >0(n) >Ofn log, n) > 0(n) > faster than thecomputer. Thus we conclude that the
O(log, n) time requirement for execution of a solution,
Ex. 1.9.4 : Give time complexity of any four sorting
changes by a constant factor on change in hardware
or software.
aigorithms.
Soln. : An important consequence of the above discussion
is that if the time taken by one machine in executing
1 Bubble sort - 0(n) asolution is of the order of n (say), then time take
2 Selection sort -0(n) by every machine is of the order of n', Thus,
function different from each other by constant
3 insertion sort- 0(n)
factor, when treated as time complexities, should
4 Merge sort - 0(n log n) not be treated as different. i.e. should be treated as
Ex. 1.9.5 : What do you mean by frequency count ? Why complexity wise same.
only frequency count is important in deciding the time Following functions have the same time
complexity of an algorithm ? complexities :
Soin.:
, (n) = Sn?
Total time taken by a statement in execution
depends on : f,(n) = 100n?
(i) The amount of time a single execution will take. f, (n) = 1000n²
(i) Number of times the given statement will be f, (n) = n?
executed. Time complexity of f,(n)= Time complexity of f, (n)
The product of these numbers will be the total time = Time complexity of f; (n)
taken by the statement. Number of times a
=Time conmplexity of f, (n)