0% found this document useful (0 votes)
2 views

Data Structures Digital Notes-1-20

The document contains lecture notes for a Data Structures course for B.Tech II Year students, detailing course objectives, prerequisites, and various data structures such as linked lists, stacks, queues, and trees. It includes a comprehensive breakdown of topics across five units, covering definitions, operations, and implementations, as well as algorithms for searching and sorting. Additionally, it lists textbooks and reference materials, along with expected outcomes for students upon completion of the course.

Uploaded by

repoxo5995
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data Structures Digital Notes-1-20

The document contains lecture notes for a Data Structures course for B.Tech II Year students, detailing course objectives, prerequisites, and various data structures such as linked lists, stacks, queues, and trees. It includes a comprehensive breakdown of topics across five units, covering definitions, operations, and implementations, as well as algorithms for searching and sorting. Additionally, it lists textbooks and reference materials, along with expected outcomes for students upon completion of the course.

Uploaded by

repoxo5995
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

DATA STRUCTURES

(R18A0503)

LECTURE NOTES

B.TECH II YEAR – I SEM (R18)


(2019-20)

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

MALLA REDDY COLLEGE OF ENGINEERING & TECHNOLOGY


(Autonomous Institution – UGC, Govt. of India)
(Recognized under 2(f) and 12 (B) of UGC ACT 1956)
(Affiliated to JNTUH, Hyderabad, Approved by AICTE - Accredited by NBA & NAAC – ‘A’ Grade - ISO 9001:2015 Certified)
Maisammaguda, Dhulapally (Post Via. Hakimpet), Secunderabad – 500100, Telangana State, India
II Year B. Tech CSE ‐ I Sem L T/P/D C
3 -/-/- 3
(R18A0503) DATA STRUCTURES

Prerequisites: A course on “Programming for Problem Solving”.

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

The linked allocation has the following draw backs:


1. No direct access to a particular element.
2. Additional memory required for pointers.

Linked list are of 3 types:


1. Singly Linked List
2. Doubly Linked List
3. Circularly Linked List

SINGLY LINKED LIST


A singly linked list, or simply a linked list, is a linear collection of data items. The linear order is
given by means of POINTERS. These types of lists are often referred to as linear linked list.
* Each item in the list is called a node.
* Each node of the list has two fields:
1. Information- contains the item being stored in the list.
2. Next address- contains the address of the next item in the list.
* The last node in the list contains NULL pointer to indicate that it is the end of the list.

Conceptual view of Singly Linked List

Operations on Singly linked list:


 Insertion of a node
 Deletions of a node
 Traversing the list

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

Insertions: To place an elements in the list there are 3 cases :


1. At the beginning
2. End of the list
3. At a given position

case 1:Insert at the beginning

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:

Code for insert front:-


template <class T>
void list<T>::insert_front()
{
struct node <T>*t,*temp;
cout<<"Enter data into node:";
cin>>item;
temp=create_node(item);
if(head==NULL)
head=temp;
else
{ temp->link=head;
head=temp;
}
}

Page 3
3
UNIT -1

case 2:Inserting end of the list

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;

After insertion the linked list is

Code for insert End:-

template <class T>


void list<T>::insert_end()
{
struct node<T> *t,*temp;
int n;
cout<<"Enter data into node:";
cin>>n;
temp=create_node(n);
if(head==NULL)
head=temp;
else
{ t=head;
while(t->link!=NULL)
t=t->link;
t->link=temp;
}
}

Page 4
4
UNIT -1

case 3: Insert at a position

insert node at position 3


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

c=1;
while(c<pos)
{
prev=cur;
cur=cur->link;
c++;
}
prev->link=temp;
temp->link=cur;

Code for inserting a node at a given position:-

template <class T>


void list<T>::Insert_at_pos(int pos)
{struct node<T>*cur,*prev,*temp;
int c=1;
cout<<"Enter data into node:";
cin>>item
temp=create_node(item);
if(head==NULL)
head=temp;
else
{
prev=cur=head;
if(pos==1)
{
temp->link=head;

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

Case 1: Delete a node at beginning of the list

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

code for deleting a node at front

template <class T>


void list<T>::delete_front()
{
struct node<T>*t;
if(head==NULL)
cout<<"List is Empty\n";
else
{ t=head;

Page 6
6
UNIT -1

head=head->link;
cout<<"node "<<t->data<<" Deletion is sucess";
delete(t);
}
}

Case 2. Delete a node at end of the list

head

To delete last node , find the node using following code

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

code for deleting a node at end of the list


template <class T>
void list<T>::delete_end()
{
struct node<T>*cur,*prev;
cur=prev=head;
if(head==NULL)
cout<<"List is Empty\n";
else
{ cur=prev=head;
if(head->link==NULL)
{
cout<<"node "<<cur->data<<" Deletion is sucess";
free(cur);
head=NULL;
}

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);
}
}
}

CASE 3. Delete a node at a given position

head

Delete node at position 3


head is the pointer variable which contains address of the first node. Node to be deleted is node
containing value 30.
Finding node at position 3

c=1;
while(c<pos)
{ c++;
prev=cur;
cur=cur->link;
}

prev cur

10 20 30 40 NULL

cur is the node to be deleted . before deleting update links

code to update links


prev->link=cur->link;
cout<<cur->data <<"is deleted successfully";
delete cur;

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.

template <class T>


void list<T>:: display()
{
struct node<T>*t;

if(head==NULL)
{
cout<<"List is Empty\n";
}
else
{ t=head;
while(t!=NULL)
{ cout<<t->data<<"->";
t=t->link;
}
}
}

Dynamic Implementation of list ADT


#include<iostream.h>
#include<stdlib.h>
template <class T>
struct node
{
T data;
struct node<T> *link;
};
template <class T>
class list
{
int item;
struct node<T>*head;
public:
list();
void display();
struct node<T>*create_node(int n);
void insert_end();
void insert_front();
void Insert_at_pos(int pos);
void delete_end();
void delete_front();
void Delete_at_pos(int pos);
void Node_count();
};

Page 9
9
UNIT -1

template <class T>


list<T>::list()
{
head=NULL;
}

template <class T>


void list<T>:: display()
{
struct node<T>*t;

if(head==NULL)
{
cout<<"List is Empty\n";
}
else
{ t=head;
while(t!=NULL)
{ cout<<t->data<<"->";
t=t->link;
}
}
}

template <class T>


struct node<T>* list<T>::create_node(int n)
{struct node<T> *t;
t=new struct node<T>;
t->data=n;
t->link=NULL;
return t;
}

template <class T>


void list<T>::insert_end()
{struct node<T> *t,*temp;
int n;
cout<<"Enter data into node:";
cin>>n;
temp=create_node(n);
if(head==NULL)
head=temp;
else
{ t=head;
while(t->link!=NULL)
t=t->link;
t->link=temp;
}
}

Page 10
10
UNIT -1

template <class T>


void list<T>::insert_front()
{
struct node <T>*t,*temp;
cout<<"Enter data into node:";
cin>>item;
temp=create_node(item);
if(head==NULL)
head=temp;
else
{ temp->link=head;
head=temp;
}
}

template <class T>


void list<T>::delete_end()
{
struct node<T>*cur,*prev;
cur=prev=head;
if(head==NULL)
cout<<"List is Empty\n";
else
{ cur=prev=head;
if(head->link==NULL)
{
cout<<"node "<<cur->data<<" Deletion is sucess";
free(cur);
head=NULL;
}
else
{ while(cur->link!=NULL)
{ prev=cur;
cur=cur->link;
}
prev->link=NULL;
cout<<"node "<<cur->data<<" Deletion is sucess";
free(cur);
}
}
}

template <class T>


void list<T>::delete_front()
{
struct node<T>*t;
if(head==NULL)
cout<<"List is Empty\n";
else
{ t=head;
head=head->link;

Page 11
11
UNIT -1

cout<<"node "<<t->data<<" Deletion is sucess";


delete(t);
}
}

template <class T>


void list<T>::Node_count()
{
struct node<T>*t;
int c=0;
t=head;
if(head==NULL)
{
cout<<"List is Empty\n";

}
else
{ while(t!=NULL)
{ c++;
t=t->link;
}
cout<<"Node Count="<<c<<endl;
}
}

template <class T>


void list<T>::Insert_at_pos(int pos)
{struct node<T>*cur,*prev,*temp;
int c=1;
cout<<"Enter data into node:";
cin>>item
temp=create_node(item);
if(head==NULL)
head=temp;
else
{ prev=cur=head;
if(pos==1)
{
temp->link=head;
head=temp;
}
else
{
while(c<pos)
{ c++;
prev=cur;
cur=cur->link;
}
prev->link=temp;
temp->link=cur;
}

Page 12
12
UNIT -1

}
}

template <class T>


void list<T>::Delete_at_pos(int pos)
{
struct node<T>*cur,*prev,*temp;
int c=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";
}
}
}

DOUBLY LINKED LIST


A singly linked list has the disadvantage that we can only traverse it in one direction. Many
applications require searching backwards and forwards through sections of a list. A useful refinement
that can be made to the singly linked list is to create a doubly linked list. The distinction made
between the two list types is that while singly linked list have pointers going in one direction, doubly
linked list have pointer both to the next and to the previous element in the list. The main advantage of
a doubly linked list is that, they permit traversing or searching of the list in both directions.

In this linked list each node contains three fields.


a) One to store data
b) Remaining are self referential pointers which points to previous and next nodes in the list

prev data next

Page 14
14
UNIT -1

Implementation of node using structure


Method -1:

struct node
{
int data;
struct node *prev;
struct node * next;
};

Implementation of node using class


Method -2:

class node
{
public:
int data;
node *prev;
node * next;
};

NULL 10 20 30 NULL

Operations on Doubly linked list:


 Insertion of a node
 Deletions of a node
 Traversing the list

Doubly linked list ADT:

template <class T>


class dlist
{
int data;
struct dnode<T>*head;
public:
dlist()
{
head=NULL;
}
void display();
struct dnode<T>*create_dnode(int n);
void insert_end();
void insert_front();
void delete_end();
void delete_front();
void dnode_count();

Page 15
15
UNIT -1

void Insert_at_pos(int pos);


void Delete_at_pos(int pos);
};

Insertions: To place an elements in the list there are 3 cases


 1. At the beginning
 2. End of the list
 3. At a given position

case 1:Insert at the beginning


head

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

Code for insert front:-


template <class T>
void DLL<T>::insert_front()
{
struct dnode <T>*t,*temp;
cout<<"Enter data into node:";
cin>>data;
temp=create_dnode(data);
if(head==NULL)
head=temp;
else
{ temp->next=head; head-
>prev=temp;
head=temp;
}
}

Page 16
16

You might also like