Unit 1 DS
Unit 1 DS
Subject Teacher
Prof. Ms. Y. A. Thakare
(Department of Computer Science and
Engineering, SIPNA COET, Amravati)
Proposed Scheme
SANT GADGE BABA AMRAVATI UNIVERSITY,AMRAVATI
Total Hours/Week
Max Max
Subject Code
Credit
Duratio
Marks Marks Min Min
Tutorial
Lecture
Sr No
n of
P/D
Subject Name Theor College Total Passing External Internal Total Passing
paper
y Assess Marks Marks
(Hr)
Paper ment
Theory
1 3KS04 Data Structures 3 3 3 3 80 20 100 40
Practicals
1 3KS07 Data Structures Lab 2 2 1 25 25 50 25
* As per the Ordinance No. 42 of 2005
# C Skill Lab I - based on technology like -Python/Django etc. to be decided by Individual Dept. of respective College
Course Information
Text Book:
Seymour Lipschutz: Data Structures with C, Schaum’s Outline Series, Mc Graw-Hill,
International Editions.
References:
1. Forouzan, Gilberg, Mahalle, Jogalekar: Data Structures and Algorithms, CENGAGE
Learning.
2. Reema Thareja: Data Structures using C, Oxford University Press, 2011.
3. Arpita Gopal: Magnifying Data structures, PHI(EEE), 2010.
4. Ellis Horowitz, Sartaj Sahni: Fundamentals of Data Structures, CBS Publications.
5. Trembley, Sorenson: An Introduction to Data Structures with Applications, McGraw Hill.
6. Standish: Data Structures in Java, Pearson Education.
OBJECTIVES:
• Study the representation and use of primitive data types and built
in data structures.
• Study how the data structures are allocated and used in memory.
• As applications are getting complexed and amount of data is increasing day by day, there may arrise
the following problems:
• Processor speed: To handle very large amount of data, high speed processing is required, but as the
data is growing day by day to the billions of files per entity, processor may fail to deal with that much
amount of data.
• Data Search: Consider an inventory size of 106 items in a store, If our application needs to search for
a particular item, it needs to traverse 106 items every time, results in slowing down the search
process.
• Multiple requests: If thousands of users are searching the data simultaneously on a web server, then
there are the chances that a very large server can be failed during that process
• in order to solve the above problems, data structures are used. Data is organized to form a data
structure in such a way that all items are not required to be searched and required data can be searched
instantly.
What is Data Structure:
• Data Structure is a way to store and organize data so that it can be
used efficiently.
Linked List:
• It can be seen as the collection of nodes stored at non-contiguous
memory locations.
• Each node of the list contains two fields, one for storing data or
information and other for storing address of next element.
Head
PUSH POP
Linear Data Structure cont..
Queue:
• Queue is a linear list in which elements can be inserted only at one end called rear
and deleted only at the other end called front.
• Queue is opened at both end therefore it follows First-In-First-Out (FIFO)
methodology for storing the data items.
• The people standing in a railway reservation row are an example of queue.
• Each new person comes and stands at the end of the row and person getting their
reservation confirmed get out of the row from the front end.
• The bellow show figure how the operations take place on a stack:
10 20 30 40 50
front rear
Non-Linear Data Structure cont..
Trees:
• Trees are multilevel data structures with a hierarchical relationship among its
elements known as nodes.
• The bottommost nodes in the hierarchy are called leaf node while the topmost
node is called root node. Each node contains pointers to point adjacent nodes.
• A tree can be defined as finite set of data items (nodes).
• The tree structure organizes the data into branches, which related the
information.
A root
B C
D E F G
Non-Linear Data Structure cont..
Graphs:
• Graphs can be defined as the pictorial representation of the set of elements
(represented by vertices) connected by the links known as edges.
• A graph is different from tree in the sense that a graph can have cycle while the tree
can not have the one.
• Definition: A graph G(V,E) is a set of vertices V and a set of edges E.
• Vertices on the graph are shown as circles and edges are drawn as line segment.
6
v2 v5
v1 v3
10
v1 8 11
15
9 v2
v3 v4 v4
Q2. Consider the linear array NAME in fig which is sorted alphabetically
1 2 3 4 5 6 7 8
• Algorithm:
Module A
Module B
Module C
2) Selection Logic / Conditional Flow
Single Alternative Double Alternative Multiple Alternative
If condition, then: If condition, then: If condition(1), then:
[ Module A ] [ Module A ] [ Module A1 ]
[ End of If structure ] Else: Else If condition(2), then:
[ Module B ] [ Module A2 ]
NO [ End of If structure ] .
Condition
.
Yes Condition
NO .
Module A Else If condition(M), then:
Yes [ Module Am ]
Module A Module B Else:
[ Module B ]
[ End of If structure ]
3) Iteration Logic / Repetitive Flow
Repeat For Loop Repeat While Loop
K R
Condition?
Yes
Is K>S
Module (Body of Loop)
No
Module
K K+T
• Write an algorithm for finding largest element in Array…..
1. Set K := 1, LOC := 1, MAX : = DATA[1]
2. Repeat steps 3 and 4 while k <= N
3. If MAX < DATA[K] then:
Set LOC := K, MAX := DATA[K]
[End of If Structure]
4. Set K := K + 1
[End of step 2 loop]
5. Write LOC, MAX
6. Exit.
String operations
Substrings
Indexing
Concatenation
Length
Text Processing Operations
Insertion
Deletion
Replacing
Substring
A substring is the basic unit of access in a string.
Unlike other array elements, substrings have their
own meaning. To access a substring we need the
following information,
Name of the string.
Position of the first character of the substring in the
given string.
length of the substring.
If string A=“WELCOME”, then after
• Indexing
• Indexing operation is used to find the position of
the first appearance of a string pattern in a given
string. It is also called pattern matching. INDEX
operation returns a 0 if there is no such pattern in
that string.
• if A=’MORNING’ then INDEX(A,’NI’) will return, 4,
the first appearance of pattern ‘NI’ in the given
string ‘MORNING’.
• Concatenation
• if A and B are two strings, then the concatenation
of two strings, A//B is the string that consisting of
the characters of A followed by the characters of
B.
• if A=’GOOD’ and B=’MORNING’ then A//B will be
‘GOODMORNING’
• A//[]//B= GOOD MORNING
• Length
• The number of characters in a string is called the
length of that string.
• if A=”DEC EMBER” then LENGTH(A) will return 9.
• Text Processing Operations
• The processing of printed matter such as letters
and articles are called word processing. Basic
operations associated with word processing are,
• Replacement
• Insertion
• Deletion
• Word processing operations can be done using
string operations.
• Insertion
• INSERT(TEXT,POSITION,STRING) operation
inserts STRING in TEXT so that STRING begins in
POSITION.
• For example INSERT(‘ABCDE’,2,’PQR’) =
• ‘APQRBCDE’
• INSERT operation can be implemented using string
operations as follows,
• INSERT(T,K,S) = SUBSTRING(T,1,K-1) // S //
SUBSTRING(T,K,LENGTH(T)-K+1)
• That is the substring before the position K is
• Deletion
• DELETE(TEXT,POSITION,LENGTH) delete the
substring which begins at POSITION and has
length LENGTH.
• For example DELETE(‘ABCDEF’,2,3) = ‘AEF’
• DELETE operation can be implemented using
string operations as follows,
• DELETE(T,K,L) = SUBSTRING(T,1,K-1) //
SUBSTRING(T,K+L,LENGTH(T)-K-L+1)
• =A//(T,5,(6-2-3+1))=(T,5,2)=A//EF=AEF
• That is the substring before position k is
• Replacing
• REPLACE(TEXT,PATTERN1,PATTERN2) replaces
the first occurrence of PATTERN1 by PATTERNP2 in
TEXT.
• For example REPLACE(‘PQRSTU’,’RS’,’X’) = PQXTU
University Asked Question
Consider the following algorithm.
Set K := INDEX(T,P)
Repeat while k != 0
Set T := DELETE(T,INDEX(T,P),LENGTH(P))
Set K := INDEX(T,P)
Write :T
Exit.
• C)P=aaba T=a20
For each of the following patterns P and text T , find
the number of Comparison to find INDEX of P in T
using Slow algorithm
a) P=abc T= ababababab
b) P= abc T= aaaabbbbcccc
c) P= aaa T= aabbaabbaabb
e) P=aaa T = abaabbaaabbbaaaabbbb
Q. Using first pattern matching algo solve P=abc T= ababababab
ans:
Max=Length(P)-Length(T)+1 (i.e.Maximum substrings possible)
Max=10-3+1=8
Wk=SUBSTRING(T,K,Length(P))
W1=SUBSTRING(ababababab,1,3)=aba ----- here aba!=Pattern abc ----------------------------------------- =3(No of Comparision
made to match P with substring of T)
W2=SUBSTRING(ababababab,2,3)=bab---- bab!=abc --------------------------------------------------------- =1
W3=SUBSTRING(ababababab,3,3)=aba---- aba!=abc --------------------------------------------------------- =3
W4=SUBSTRING(ababababab,4,3)=bab---- bab!=abc --------------------------------------------------------- =1
W5=SUBSTRING(ababababab,5,3)=aba---- aba!=abc --------------------------------------------------------- =3
W6=SUBSTRING(ababababab,6,3)=bab---- bab!=abc --------------------------------------------------------- =1
W7=SUBSTRING(ababababab,7,3)=aba---- aba!=abc --------------------------------------------------------- =3
W8=SUBSTRING(ababababab,8,3)=bab---- bab!=abc --------------------------------------------------------- =1
Therefore INDEX(T,P)=0 (Pattern P is not Present in Text T)
No of comparison=3+1+3+1+3+1+3+1=16
Consider the pattern P=abc. Use ‘slow’ pattern
matching algorithm to find number of comparisons
to find INDEX of P in each of the following texts T.
a) a20 b) (abc) 10 c) (caab) 10 d) d10
First Pattern Matching Algorithm