Professional Documents
Culture Documents
HIMAGIRI
Data Structures
----------------------------------------------------------------------------------------------------------
UNIT-1:
Basic concepts - Algorithm Specification, Data Abstraction , Performance analysis - time
complexity and space complexity, Asymptotic Notation - Big O, Omega and Theta notations,
Introduction to Linear and Non Linear data structures.
Linear list – Singly linked list implementation, insertion, deletion and searching operations on
linear list, Circular linked list implementation, Doubly linked list implementation, insertion,
deletion and searching operations. Applications of linked lists.
---------------------------------------------------------------------------------------------------------------------
Data Structure:
Data Structure is a way to store and organize data in a computer so that it can be used
efficiently.
A data structure is seen as a logical concept that address two fundamental concerns.
i) How the data will be stored, and
ii) What operations will be performed on it.
There are two types of data structures-Linear and Non-Linear Data Structures.
Examples:Arrays,Stack,Queues,Trees, etc..
1
Prepared by D.HIMAGIRI
Algorithm Specification
Algorithm:
An Algorithm is a finite set of instructions that, if followed, accomplishes a particular
task.
Characteristics of an Algorithm:
iv. FINITENESS : If we trace out the instructions of an algorithm, then for all cases,
the algorithm terminates after a finite number of steps.
2
Prepared by D.HIMAGIRI
Algorithm Specification....
Graphic representation called flowchart: This method will work well when the
intended for human reading rather than machine reading. There is no need to follow all
Data Abstraction:
Abstraction means displaying only essential information and hiding the details. Data
abstraction refers to providing only essential information about the data to the outside world,
hiding the background details or implementation. 3
Prepared by D.HIMAGIRI
Performance Analysis
Performance analysis of an algorithm depends upon two factors i.e. amount of memory used
and amount of compute time consumed on any CPU.
i) Space Complexity:
4
Prepared by D.HIMAGIRI
Performance Analysis....
Time Complexity:
1. Algorithm Sum(a,n) No. of elementary steps
2.{
3. s=0.0; -------------------------------- 1
4. for i=1 to n do ------------------------- n+1
5. s=s+a[i]; ------------------------------ n
6. return s; ------------------------------- 1
7. }
Total---------------------------- 2n+3
6
Prepared by D.HIMAGIRI
Performance Analysis....
Example 2:
1. Algorithm Add(A,B,n) No. of elementary steps
2.{
3. for(i=0;i< n;i++) ------------------- n+1
4. {
5. for(j=0;j< n;j++)------------------- n*(n+1)
6. {
7. C[i,j]=A[i,j]+B[i,j] ------------ n*n
8. }
9. }
10. }
Total-------------------------n+1+n*(n+1)+n*n=2n2+ 2n+1
Time Complexity=O(n2)
Space Complexity is : Fixed components are i,j,n------each 1 word=3 words
Variable components are A-n*n,B-n*n and C-n*n=3n2
Total=3n2+3
Space complexity=O(n2)
7
Prepared by D.HIMAGIRI
Asymptotic Notations
8
Prepared by D.HIMAGIRI
Asymptotic Notations...
if f(n) ≤ cg(n) ∀ n≥n0 where c>0 and n0 >=1 then we can say that f(n) = O(g(n)) .
9
Prepared by D.HIMAGIRI
Asymptotic Notations...
Big-Omega Notation(Ω)
It describes the best case scenerio,it represents the lower bound running time
complexity of an algorithm .
if f(n)>=c.g(n) ∀ n≥n0 where c>0 and n0 >=1 then we can say that f(n) = Ω (g(n)) .
10
Prepared by D.HIMAGIRI
Asymptotic Notations...
if c1g(n)<=f(n)<=c2.g(n) ∀ n≥n0 where c1,c2>0 and n0 >=1 then we can say that
f(n) = Θ (g(n)) .
11
Prepared by D.HIMAGIRI
Linear & Non-linear Data Structure
12
Prepared by D.HIMAGIRI
Linear & Non-linear Data Structure....
Difference between Linear and Non-Linear Data structures :
Sr. No. Key Linear Data Structures Non-linear Data Structures
In single linked list every node contains two fields, data field and link field -a pointer to
the next node/address of next node.
14
Prepared by D.HIMAGIRI
Single Linked List....
The node in a single linked list is declared as
struct node
{
int data;
struct node *next;
};
The last node address field in the single linked list contains NULL.
I. Insertion
II. Deletion
III. Searching
IV. Traversing
15
Prepared by D.HIMAGIRI
Single Linked List....
Insertion:
Case 1: The new node is inserted at the beginning
16
Prepared by D.HIMAGIRI
Single Linked List....
Case 2: The new node is inserted at the end
17
Prepared by D.HIMAGIRI
Single Linked List....
Case 3: The new node is inserted after a given node
18
Prepared by D.HIMAGIRI
Single Linked List....
Case 4: The new node is inserted before a given node
19
Prepared by D.HIMAGIRI
Single Linked List....
Deletion : Case 1: The first node is deleted
20
Prepared by D.HIMAGIRI
Single Linked List....
Case 3: The node after a given node is deleted
21
Prepared by D.HIMAGIRI
Single Linked List....
Searching:
22
Prepared by D.HIMAGIRI
Single Linked List....
Traversing:
Traversing a linked list means accessing the nodes of the list in order to perform some
processing on them.
23
Prepared by D.HIMAGIRI
Doubly Linked List
A doubly linked list or a two-way linked list is a more complex type of linked list which
contains a pointer to the next as well as the previous node in the sequence.
It is a collection of node , in which each node will contain three fields- a pointer to the
previous node ,data, a pointer to the next node.
II. Deletion
III. Searching
IV. Traversing
Insertion:
Case 1: The new node is inserted at the beginning.
25
Prepared by D.HIMAGIRI
Doubly Linked List....
Case 2: The new node is inserted at the end.
26
Prepared by D.HIMAGIRI
Doubly Linked List....
Case 3: The new node is inserted after a given node.
27
Prepared by D.HIMAGIRI
Doubly Linked List....
Case 4: The new node is inserted before a given node.
28
Prepared by D.HIMAGIRI
Doubly Linked List....
Deletion:
Case 1: The first node is deleted.
29
Prepared by D.HIMAGIRI
Doubly Linked List....
Deletion:
Case 2: The last node is deleted.
30
Prepared by D.HIMAGIRI
Doubly Linked List....
Deletion:
Case 3: The node after a given node is deleted.
31
Prepared by D.HIMAGIRI
Doubly Linked List....
Deletion:
Case 4: The node before a given node is deleted.
32
Prepared by D.HIMAGIRI
Circular Linked List
Circular Linked List Types:
I. Circular Single Linked List
II. Circular Doubly Linked List
Circular Single Linked List:
In a circular single linked list, the last node contains a pointer to the first node of the
list i,e the last node address field contains the address of the first node.
II. Deletion
III. Searching
IV. Traversing
33
Prepared by D.HIMAGIRI
Circular Single Linked List.....
Insertion:
Case 1: The new node is inserted at the beginning of the circular linked list.
34
Prepared by D.HIMAGIRI
Circular Single Linked List.....
Insertion:
Case 2: The new node is inserted at the end of the circular linked list.
35
Prepared by D.HIMAGIRI
Circular Single Linked List.....
Deletion:
Case 1: The first node is deleted.
36
Prepared by D.HIMAGIRI
Circular Single Linked List.....
Deletion:
Case 2: The last node is deleted.
37
Prepared by D.HIMAGIRI
Circular Doubly Linked List
Circular Doubly Linked List:
Circular doubly linked list doesn't contain NULL in any of the node.
The last node NEXT field of the list contains the address of the first node of the list.
The first node PREV field of the list contain address of the last node of the list.
Operations:
I. Insertion
II. Deletion
III. Searching
IV. Traversing
Implementation is more complex than other linked lists.
38
Prepared by D.HIMAGIRI
Applications of Linked Lists
Polynomial Representation
************
39