Data Structures Digital Notes-1-20
Data Structures Digital Notes-1-20
(R18A0503)
LECTURE NOTES
Course Objectives:
To impart the basic concepts of data structures
Exploring basic data structures such as stacks queues and lists.
Introduces a variety of data structures such as hash tables, search trees, heaps,
graphs.
To understand concepts about searching and sorting techniques
UNIT-I
Introduction: Abstract data types, Singly linked list: Definition, operations: Traversing,
Searching, Insertion and deletion, Doubly linked list: Definition, operations: Traversing,
Searching, Insertion and deletion, Circular Linked List: Definition, operations: Traversing,
Searching, Insertion and deletion.
UNIT-II
Stack: Stack ADT, array and linked list implementation, Applications- expression conversion
and evaluation. Queue: Types of Queue: Simple Queue, Circular Queue, Queue ADT- array
and linked list implementation. Priority Queue, heaps.
UNIT-III
Searching: Linear and binary search methods. Sorting: Selection Sort, Bubble Sort, Insertion
Sort, Quick Sort, Merge Sort, Heap Sort. Time Complexities .Graphs: Basic terminology,
representation of graphs, graph traversal methods DFS, BFS.
UNIT IV
Dictionaries: linear list representation, skip list representation, operations - insertion,
deletion and searching. Hash Table Representation: hash functions, collision resolution-
separate chaining, open addressing-linear probing, quadratic probing, double hashing,
rehashing, extendible hashing.
UNIT-V
Binary Search Trees: Various Binary tree representation, definition, BST ADT,
Implementation, Operations- Searching, Insertion and Deletion, Binary tree traversals,
threaded binary trees,
AVL Trees : Definition, Height of an AVL Tree, Operations – Insertion, Deletion and
Searching
B-Trees: B-Tree of order m, height of a B-Tree, insertion, deletion and searching, B+ Tree.
TEXTBOOKS:
1. Data Structures using C++, Special Edition-MRCET, Tata McGraw-Hill Publishers 2017.
2. Data structures, Algorithms and Applications in C++, S.Sahni, University Press (India)
Pvt.Ltd, 2nd edition, Universities Press Orient Longman Pvt. Ltd. Education.
REFERENCE BOOKS:
1. Data structures and Algorithms in C++, Michael T.Goodrich, R.Tamassia and .Mount,
Wiley student edition, John Wiley and Sons.
2. Data structures and Algorithm Analysis in C++, Mark Allen Weiss, Pearson Education. Ltd.,
Second Edition
OUTCOMES:
At the end of the course the students are able to:
Ability to select the data structures that efficiently model the information in a
problem.
Ability to assess efficiency trade-offs among different data structure
implementations or combinations.
Implement and know the application of algorithms for sorting .
Design programs using a variety of data structures, including hash tables, binary
and general tree structures, search trees, AVL-trees, heaps and graphs.
INDEX
UNIT NO TOPIC PAGE NO
Introduction 01
Singly linked list 02
I
Doubly linked list 14
Circular Linked List 28
Stack ADT 34
Array implementation 35
linked list implementation 38
Queue ADT 42
II Array implementation 43
linked list implementation 45
Circular Queue 47
Priority Queue 52
Heaps 53
Searching: Linear Search 67
Binary search 70
Sorting: Bubble Sort 74
Selection Sort 75
Insertion Sort 77
Quick Sort 78
III
Merge Sort 82
Heap Sort 84
Time Complexities 86
Graphs: Basic terminology 87
Representation of graphs 89
Graph traversal methods 91
Dictionaries: linear list representation 94
Skip list representation 98
IV Hash Table Representation 102
Rehashing, 109
Extendible hashing 111
Binary Search Trees: Basics 115
Binary tree traversals 119
Binary Search Tree 121
V
AVL Trees 126
B-Trees 138
B+ Tree 147
UNIT -1
Introduction: Abstract data types, Singly linked list: Definition, operations: Traversing, Searching,
Insertion and deletion, Doubly linked list: Definition, operations: Traversing, Searching, Insertion and
deletion, Circular Linked List: Definition, operations: Traversing, Searching, Insertion and deletion
Data structure A data structure is a specialized format for organizing and storing data.
General data structure types include the array, the file, the record, the table, the tree, and so on.
Any data structure is designed to organize data to suit a specific purpose so that it can be accessed
and worked with in appropriate ways
Abstract Data Type
In computer science, an abstract data type (ADT) is a mathematical model for data types
where a data type is defined by its behavior (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 behavior of these operations. When a class is used as a type, it is an abstract type that
refers to a hidden representation. In this model an ADT is typically implemented as a class, and
each instance of the ADT is usually a n object of that class.
In ADT all the implementation details are hidden
Linear data structures are the data structures in which data is arranged in a list or in a
sequence.
Non linear data structures are the data structures in which data may be arranged in a
hierarchic al manner
LIST ADT
List is basically the collection of elements arrange d in a sequential manner. In memory
we can store the list in two ways: one way is we can store the elements in sequential
memory locations. That means we can store the list in arrays.
The other way is we can use pointers or links to associate elements sequentially.
This is known as linked list.
LINKED LISTS
The linked list is very different type of collection from an array. Using such lists, we can
store collections of information limited only by the total amount of memory that the OS will allow
us to use.Further more, there is no need to specify our needs in advance. The linked list is very
flexible dynamic data structure : items may be added to it or deleted from it at will. A programmer
need not worry about how many items a program will have to accommodate in advance. This
allows us to write robust programs which require much less maintenance.
Page 1
1
UNIT -1
Structure of a node:
Method -1:
struct node
{ Data link
int data;
struct node *link;
};
Method -2:
class node
{
public:
int data;
node *link;
};
Page 2
2
UNIT -1
temp
head is the pointer variable which contains address of the first node and temp contains address of
new node to be inserted then sample code is
temp->link=head;
head=temp;
After insertion:
Page 3
3
UNIT -1
temp
head is the pointer variable which contains address of the first node and temp contains address of new
node to be inserted then sample code is
t=head;
while(t->link!=NULL)
{
t=t->link;
}
t->link=temp;
Page 4
4
UNIT -1
c=1;
while(c<pos)
{
prev=cur;
cur=cur->link;
c++;
}
prev->link=temp;
temp->link=cur;
Page 5
5
UNIT -1
head=temp;
}
else
{
while(c<pos)
{ c++;
prev=cur;
cur=cur->link;
}
prev->link=temp;
temp->link=cur;
}
}
}
Deletions: Removing an element from the list, without destroying the integrity of the list itself.
To place an element from the list there are 3 cases :
1. Delete a node at beginning of the list
2. Delete a node at end of the list
3. Delete a node at a given position
head
head is the pointer variable which contains address of the first node
sample code is
t=head;
head=head->link;
cout<<"node "<<t->data<<" Deletion is sucess";
delete(t);
head
Page 6
6
UNIT -1
head=head->link;
cout<<"node "<<t->data<<" Deletion is sucess";
delete(t);
}
}
head
struct node<T>*cur,*prev;
cur=prev=head;
while(cur->link!=NULL)
{ prev=cur;
cur=cur->link;
}
prev->link=NULL;
cout<<"node "<<cur->data<<" Deletion is sucess";
free(cur);
head
Page 7
7
UNIT -1
else
{ while(cur->link!=NULL)
{ prev=cur;
cur=cur->link;
}
prev->link=NULL;
cout<<"node "<<cur->data<<" Deletion is sucess";
free(cur);
}
}
}
head
c=1;
while(c<pos)
{ c++;
prev=cur;
cur=cur->link;
}
prev cur
10 20 30 40 NULL
prev cur
10 20 30 40 NULL
Page 8
8
UNIT -1
Traversing the list: Assuming we are given the pointer to the head of the list, how do we get the end
of the list.
if(head==NULL)
{
cout<<"List is Empty\n";
}
else
{ t=head;
while(t!=NULL)
{ cout<<t->data<<"->";
t=t->link;
}
}
}
Page 9
9
UNIT -1
if(head==NULL)
{
cout<<"List is Empty\n";
}
else
{ t=head;
while(t!=NULL)
{ cout<<t->data<<"->";
t=t->link;
}
}
}
Page 10
10
UNIT -1
Page 11
11
UNIT -1
}
else
{ while(t!=NULL)
{ c++;
t=t->link;
}
cout<<"Node Count="<<c<<endl;
}
}
Page 12
12
UNIT -1
}
}
if(head==NULL)
{
cout<<"List is Empty\n";
}
else
{ prev=cur=head;
if(pos==1)
{
head=head->link;
cout<<cur->data <<"is deleted sucesfully";
delete cur;
}
else
{
while(c<pos)
{ c++;
prev=cur;
cur=cur->link;
}
prev->link=cur->link;
cout<<cur->data <<"is deleted sucesfully";
delete cur;
}
}
}
int main()
{
int ncount,ch,pos;
list <int> L;
while(1)
{
cout<<"\n ***Operations on Linked List***"<<endl;
cout<<"\n1.Insert node at End"<<endl;
cout<<"2.Insert node at Front"<<endl;
cout<<"3.Delete node at END"<<endl;
cout<<"4.Delete node at Front"<<endl;
cout<<"5.Insert at a position "<<endl;
cout<<"6.Delete at a position "<<endl;
cout<<"7.Node Count"<<endl;
cout<<"8.Display nodes "<<endl;
cout<<"9.Clear Screen "<<endl;
Page 13
13
UNIT -1
cout<<"10.Exit "<<endl;
cout<<"Enter Your choice:";
cin>>ch;
switch(ch)
{
case 1: L.insert_end();
break;
case 2: L.insert_front();
break;
case 3:L.delete_end();
break;
case 4:L.delete_front();
break;
case 5: cout<<"Enter position to insert";
cin>>pos;
L.Insert_at_pos(pos);
break;
case 6: cout<<"Enter position to insert";
cin>>pos;
L.Delete_at_pos(pos);
break;
case 7: L.Node_count();
break;
case 8: L.display();
break;
case 9:system("cls");
break;
case 10:exit(0);
default:cout<<"Invalid choice";
}
}
}
Page 14
14
UNIT -1
struct node
{
int data;
struct node *prev;
struct node * next;
};
class node
{
public:
int data;
node *prev;
node * next;
};
NULL 10 20 30 NULL
Page 15
15
UNIT -1
NULL 10 20 30 NULL
NULL 40 NULL
temp
head is the pointer variable which contains address of the first node and temp contains address of new
node to be inserted then sample code is
temp->next=head;
head->prev=temp;
head=temp;
head
40 10 20 30 NULL
Page 16
16