Datastructures Uniti
Datastructures Uniti
UNIT I:
Abstract Data Types (ADTs)- List ADT-array-based implementation-linked list implementation singly linked
lists-circular linked lists-doubly-linked lists-applications of lists-Polynomial Manipulation- All operations-
Insertion-Deletion-Merge-Traversal
UNIT II
Stack ADT-Operations- Applications- Evaluating arithmetic expressions – Conversion of infix to postfix
expression-Queue ADT-Operations-Circular Queue- Priority Queue- de Queue applications of queues.
UNIT III
Tree ADT-tree traversals-Binary Tree ADT-expression trees-applications of trees-binary search tree ADT-
Threaded Binary Trees-AVL Trees- B-Tree- B+ Tree – Heap-Applications of heap.
UNIT IV
Definition- Representation of Graph- Types of graph-Breadth first traversal – Depth first traversal-
Topological sort- Bi-connectivity – Cut vertex- Euler circuits-Applications of graphs.
UNIT V
Searching- Linear search-Binary search-Sorting-Bubble sort-Selection sort-Insertion sort-Shell sort-Radix
sort-Hashing-Hash functions-Separate chaining- Open Addressing-Rehashing Extendible Hashing
Text Books:
1. Mark Allen Weiss, “Data Structures and Algorithm Analysis in C++”, Pearson Education 2014, 4th Edition.
2.ReemaThareja, “Data Structures Using C”, Oxford Universities Press 2014, 2nd Edition
Reference Books:
Web Resources:
1. https://www.programiz.com/dsa
2. https://www.geeksforgeeks.org/learn-data-structures-and-algorithms-dsa-tutorial/
Unit i
Abstract data types (ADT) – list ADT – array-based implementation – linked list implementation
––singly linked lists- circularly linked lists- doubly-linked lists– applications of lists –polynomial
manipulation – all operation (insertion, deletion, merge, traversal)
Data:
A collection of facts, concepts, figures, observations, occurrences or instructions in a
Formalized manner.
Information:
The meaning that is currently assigned to data by means of the conventions applied to
those data (i.e. processed data)
Record:
Collection of related fields.
Data type:
Set of elements that share common set of properties used to solve a program.
Datastructures:
Data structure is the way of organizing, storing, and retrieving data and their relationship
with each other.
Characteristics of data structures:
1. It depicts the logical representation of data in computer memory.
2. It represents the logical relationship between the various data elements.
1. Traversal
2.search
3. Insertion
4.deletion
Static DD:
If a data structures is created using static memory allocation, ie.ds formed when the number of
data items are known in advance, it is known as static data static data structures or fixed size data
structures.
Dynamic ds:
Data Structures and Algorithms
If the data structures is created using dynamic memory allocation i.eds formed when the
number of data items are not known in advance is known as dynamic data structures or variable
size data structures.
An abstract data type (ADT) is defined as a mathematical model with a collection of operations
defined on that model. Set of integers, together with the operations of union, intersection and set
difference form an example of an ADT. An ADT consists of data together with functions that
operate on that data.
Advantages/benefits of adt:
1. Modularity
2.reuse
3. Code is easier to understand
4. Implementation of ADTS can be changed without requiring changes to the program that uses
the ADTS.
If the element at position i is ai, then its successor is ai+1and its predecessor is ai-1
5. Previous (i)-returnsthepositionofitspredecessori-1.
Insertion and deletion operation are expensive, as it requires more data movements
Declaration of array:
#define maxsize 10
int list[maxsize], n ;
create operation:
Create operation is used to create the list with „ n „ number of elements .if “n” exceeds the
array’s max size, then elements cannot be inserted into the list. Otherwise the array elements are
stored in the consecutive array locations (i.e.) List [0], list [1] and so on.
Void create()
{
Int i;
Printf(" enter the number of elements to be added in the list:");
Scanf ("%d",&n);
Printf("enter the array elements:");
for(i=0;i<n;i++)
Scanf("%d", &list[i]);
}
Insert operation:
Insert operation is used to insert an element at particular position in the existing list. Inserting the
element in the last position of an array is easy. But inserting the element at a particular position in
an array is quite difficult since it involves all the subsequent elements to be shifted one position to
the right.
Data Structures and Algorithms
Insert an element in the array:
Void insert()
{
Inti, data, pos;
Printf("enter the data to be inserted:\t");
scanf("%d", &data);
Printf("enter the position at which element to be inserted:\t");
scanf("%d",&pos);
If(pos==n)
Printf (“array overflow”);
for(i=n-1;i>=pos-1;i--)
List[i+1]=list[i];
list[pos-1] = data;
n=n+1;
Display();}
Consider an array with5elements[maxelements=10]
10 20 30 40 50
if data 15 is to be inserted in the 2nd position than 50 has to be moved to next index position, 40
has to be moved to50 position,30 has to be moved to40 position and 20 has to be moved to 30
position.
10 20 30 40 50
10 20 30 40 50
10 15 20 30 40 50
Deletion operation:
Deletion is the process of removing an element from the array at any position.
Deleting an element from the end is easy. If an element is to be deleted from any particular
position , it requires all subsequent element from that position is shifted one position towards left.
Routine to delete an element in the array:
Void delete()
{
int i, pos ;
Printf("Enter the position of the data to be deleted:\t");
scanf("%d",&pos);
Printf("Enter the data deleted is:\t%d", list[pos-1]);
for(i=pos-1;i<n-1;i++)
List[i]=list[i+1] ;
n=n-1;
Display();
}
Consider an array with 5 elements [max elements=10 ]
10 20 30 40 50
10 20 30 40 50
After this3data movements, data20is deleted from the2nd position of the array.
10 30 40 50
Search operation:
Search( ) operation is used to determine whether a particular element is present in the list or not.
Input the search element to be checked in the list.
Routine to search an element in the array:
Void search()
{
Intsearch,i,count= 0;
Data Structures and Algorithms
Printf("\nentertheelementtobesearched:\t");
scanf("%d",&search);
For(i=0;i<n;i++)
{
If(search==list[i])
count++;
}
If(count==0)
Printf("\nelementnotpresentinthelist"); else
Printf("\nelementpresentinthelist");
}
Void display()
{
int i;
Printf("\n**********elements in the array**********\n");
for(i=0;i<n;i++)
Printf("%d\t",list[i]);
}
Void search()
{
Int search,i, count= 0;
Printf("Enter the element to be searched:\t");
scanf("%d",&search);
For(i=0;i<n;i++)
{
If(search==list[i])
{
Count++;
}
}
If(count==0)
Printf("Element not present in the list");
else
Printf("Element present in the list");
}
Output
1.create
2. Insert
3.delete
4.display
5.search
6.exit
Enter your choice: 1
Enterthenumberofelementstobeaddedinthelist:5 enter
1 2 3 4 5
1.create
2. insert
3.delete
4.display
5.search
6.exit
3 1 2 3 4 5
1.create
2. insert
3.delete
4.display
5.search 6.exit
3 1 2 4 5
Insertion &deletion Insertion and deletion takes more Insertion and deletion are fast and
time in array easy
Memory allocation Memory is allocated at compile time Memory is allocated at run time
i.e static memory allocation i.e dynamic memory allocation
Linked lists
Thelinkedlistisverydifferenttypeofcollectionfromanarray.usingsuchlists, 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.
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 beings to red 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;
};
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;
Afterinsertion:
Template<classt>
Void list<t>::insert_front()
{
struct node <t>*t,*temp;
cout<<"enter data in to node:";
cin>>item;
temp=create_node(item);
if(head==null)
Head=temp;
Else
{
temp->link=head;
Head=temp;
}
}
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
Template<classt>
Void list<t>::insert_end()
{
structnode<t>*t,*temp;
int n;
Cout<<"enter data in to 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;
}
}
c=1;
while(c<pos)
{
prev=cur;
cur=cur->link;
c++;
}
prev->link=temp;
temp->link=cur;
Code for inserting a nod eat a given position:-
Template<classt>
Voidlist<t>::insert_at_pos(intpos)
{
structnode<t>*cur,*prev,*temp;
int c=1;
Cout<<"enter data in to 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;
}
}
}
Deletions:
Removing an element from the list, without destroying the integrity of the list itself.
To place an element from the list there are 3cases:
1. Delete a nod eat beginning of the list.
2. Delete a node at end of the list.
3. Delete a node at a given position.
c
Head is the pointer variable which contains address of the first node
Sample code is
T=head;
head=head->link;
cout<<"node"<<t->data<<"deletionissucess"; delete(t);
head
head
structnode<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 an 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<<"listis 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<<"deletionissucess";
free(cur);
}
}
}
head
c=1;
while(c<pos)
{
c++;
prev=cu r;
cur=cur->link;
}
prev cur
10 20 30 40 NULL
Cur is the node to be deleted.before deleting up date links code to update links
Prev->link=cur->link;
prev
10 20 30 40 NULL
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<<"listis empty\n";
}
Else
{
t=head;
While(t!=nul l)
{
cout<<t->data<<"->";
t=t->link;
}
}
}
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.
method -1:
struct node
{
int data;
struct node *prev;
structnode * next;
};
method -2:
Class node
{
Public:
int data;
node *prev;
node* next;
};
NUL
NULL 10 20 30 L
Template<classt>
class dlist
{
int data;
Struct dnode<t>*head;
Public:
dlist()
{
head=null;
}
Void display();
Struct dnode<t>*create_dnode(intn);
void insert_end();
Void insert_front();
void delete_end();
void delete_front();
voiddnode_count();
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
NUL
40 10 20 30 L
Codeforinsert front:-
Template<classt>
Void dll<t>::insert_front()
{
Struct dnode <t>*t,*temp;
cout<<"enter data in to node:";
cin>>data;
temp=create_dnode(data);
if(head==null)
Head=temp
else
{
tempt>next=head;
head->prev=temp;
Head=temp;
}
}
code to insert a node at end:-
Template<classt>
Void dll<t>::insert_end()
{
Struct dnode<t>*t,*temp; int n;
cout<<"enter data in to dnode:";
cin>>n;
Temp=create_dnode(n);
if(head==null)
Head=temp;
Else
{
t=head;
while(t->next!=null)
T=t->next;
t->next=temp;
Temp->prev=t;
}
}
Code to insert a node at a position
Template<classt>
Void dlist<t>::insert_at_pos(int pos)
{
struct dnode<t>*cr,*pr,*temp;
int count=1;
Cout<<"enter data in to dnode:";
cin>>data;
temp=create_dnode(data);
display();
If(head==null)
{ //whenlistis empty
Head=temp;
}
Else
{
pr=cr=head;
if(pos==1)
{ //insertingatpos=1
temp->next=head;
head=temp;
}
else
{
While(count<pos)
{
count++;
Pr=cr;
cr=cr->next;
}
Pr->next=temp;
temp->prev=pr;
temp->next=cr;
cr->prev=temp;
}
}
}
Deletions: Removing an element from the list, without destroying the integrity of the list
itself.
head
NUL NUL
L 10 20 30 L
head is the pointer variable which contains address of the first node
sample code is
t=head;
head=head->next;
head->prev=NULL;
cout<<"dnode"<<t->data<<"Deletion is sucess";
delete(t);
head
NUL
NULL 10 NULL 20 30 L
Template<classt>
Void dlist<t>::delete_front()
{
struct dnode<t>*t;
if(head==null)
Cout<<"listis empty\n";
else
{
t=head;
head=head->next;
head->prev=null;
cout<<"dnode"<<t->data<<"deletion is sucess";
delete(t);
}
}
Case2. Delete a node at end of thel ist
To deleted the last node find the last node. Find the node using following code
Struct dnode<T>*pr,*cr;
pr=cr=head;
while(cr->next!=NULL)
{
pr=cr;
cr=cr->next;
}
pr->next=NULL;
cout<<"dnode"<<cr->data<<"Deletion is sucess";
delete(cr);
head
Pr cr
Template<classt>
Void dlist<t>::delete_end()
{
struct dnode<t>*pr,*cr;
pr=cr=head;
if(head==null)
cout<<"listis empty\n";
Else
{
cr=pr=head;
if(head->next==null)
{
cout<<"dnode"<<cr->data<<"deletion is success";
delete(cr);
head=null;
}
else
{
while(cr->next!=null)
{
pr=cr;
cr=cr->next;
}
pr->next=null;
Cout<<"dnode"<<cr->data<<"deletionissucess"; delete(cr);
}
}
case3.delete a node at a given position
head
NULL 10 30 20 NULL
While(count<pos)
{
pr=cr;
cr=cr->next;
count++;
}
Pr->next=cr->next;
cr->next->prev=pr;
Head
NUL
L
NULL 10 30 20
cr
pr
Advantages:
Any node can be traversed starting from any other node in the list.
There is no need of null pointer to signal the end of the list and hence, all
pointers contain valid addresses.
In contrast to singly linked list, deletion operation in circular list is simplified as the
search for the previous node of an element to be deleted can be started from that
item itself.
head
Advantagesofcircularlinkedlist
Applicationsoflist:
1. Polynomialadt
2. Radixsort
3.multilist
Polynomialmanipulation
Polynomialmanipulationssuchasaddition,subtraction&differentiationetc..canbe performed
using linked list
Declarationforlinkedlistimplementationofpolynomialadt
structpoly
{
int coeff;
intpower;
structpoly*next;
}*list1,*list2,*list3;
Creationofthepolynomial
Polycreate(poly*head1,poly*newnode1)
{
Poly *ptr;
if(head1==null)
{
head1=newnode1;
return (head1);
}
Els
e
{ Ptr=head1;
While(ptr->next!=null)
ptr=ptr->next;
Ptr->next=newnode1;
}
Return(head1);
}
Additionoftwopolynomials
Void add()
{
Poly*ptr1,*ptr2,*newnode;
ptr1= list1;
Ptr2 =list2;
While(ptr1 !=null&&ptr2!=null)
{
Newnode=(structpoly*)malloc(sizeof(structpoly)); if(
ptr1 -> power = = ptr2 -> power )
{
Newnode->coeff =ptr1->coeff+ptr2->coeff;
Newnode->power=ptr1->power;
newnode -> next = null;
List3=create(list3,newnode); ptr1
= ptr1 -> next;
Ptr2=ptr2 ->next;
}
Elseif(ptr1->power>ptr2->power )
{
newnode -> coeff = ptr1 -> coeff;
newnode->power=ptr1->power; newnode
-> next = null;
List3=create(list3,newnode); ptr1
= ptr1 -> next;
}
Else
{
Newnode -> coeff = ptr2 -> coeff;
newnode->power=ptr2->power; newnode
-> next = null;
List3=create(list3,newnode);
Ptr2=ptr2 ->next;
}}}
Subtractionoftwopolynomial
{
Void sub() poly*ptr1,*ptr2,*newnode;
ptr1 = list1;
Ptr2 =list2;
While(ptr1 !=null&&ptr2!= null)
{
Newnode=(structpoly*)malloc(sizeof(structpoly));
if(ptr1->power==ptr2->power)
{
Newnode->coeff=(ptr1-coeff)-(ptr2->coeff);
newnode->power=ptr1->power;
Newnode->next=null;
list3=create(list3,newnode);
ptr1=ptr1->next;
Ptr2=ptr2->next;
}
Else
{
If(ptr1-power>ptr2-power)
{
newnode->coeff=ptr1->coeff;
newnode->power=ptr1->power;
newnode->next=null;
list3=create(list3,newnode);
ptr1=ptr1->next;
}
Else
{
Newnode->coeff=-(ptr2->coeff);
newnode->power=ptr2->power;
newnode->next=null;
list3=create(list3,newnode);
ptr2=ptr2->next;
}
}
}
Polynomialdifferentiation:
Void diff()
Poly*ptr1,*newnode;
ptr1 = list1;
While(ptr1!=null)
{
Newnode=(structpoly*)malloc(sizeof(struct poly));
Newnode->coeff=(ptr1-coeff)*(ptr1->power);
newnode->power=ptr1->power-1;
Newnode->next=null;
list3=create(list3,newnode);
ptr1=ptr1->next;
}
}
Polynomialmultiplication
Void mul()
{
Poly*ptr1,*ptr2,*newnode; ptr1=
list1;
Ptr2 =list2;
While(ptr1 !=null&&ptr2!=null)
{
Newnode=(structpoly*)malloc(sizeof(structpoly)); if(
ptr1 -> power = = ptr2 -> power )
{
Newnode -> coeff = ptr1 -> coeff * ptr2 -> coeff;
newnode->power=ptr1->power+ptr2->power;; newnode
-> next = null;
List3=create(list3,newnode); ptr1
= ptr1 -> next;
Ptr2=ptr2 ->next;
}}
}
Advantages of Circular linked List
Applications of List:
1. Polynomial ADT
2.Radix sort
3.Multilist
Polynomial Manipulation
Polynomial manipulations such as addition, subtraction & differentiation etc.. can be
performed using linked list
Declaration for Linked list implementation of Polynomial ADT
struct poly
{
int coeff;
int power;
struct poly *next;
}*list1,*list2,*list3;
ptr2=ptr2->next;
}
else
{
if(ptr1-power>ptr2-power)
{
newnode->coeff=ptr1->coeff;
newnode->power=ptr1->power;
newnode->next=NULL;
list3=create(list3,newnode);
ptr1=ptr1->next;
}
else
{
newnode->coeff=-(ptr2->coeff);
newnode->power=ptr2->power;
newnode->next=NULL;
list3=create(list3,newnode);
ptr2=ptr2->next;
}
}
}
Polynomial Differentiation:
void diff()
newnode->coeff=(ptr1-coeff)*(ptr1->power);
newnode->power=ptr1->power-1;
newnode->next=NULL;
list3=create(list3,newnode);
ptr1=ptr1->next;
}
}
Polynomial Multiplication
void mul()
{
poly *ptr1, *ptr2, *newnode ;
ptr1= list1;
ptr2 = list2;
while( ptr1 != NULL && ptr2 != NULL )
{
newnode = (struct poly*)malloc( sizeof ( struct poly ));
if( ptr1 -> power = = ptr2 -> power )
{
newnode -> coeff = ptr1 -> coeff * ptr2 -> coeff;
newnode -> power = ptr1 -> power+ptr2->power; ;
newnode -> next = NULL;
list3 = create( list3, newnode );
ptr1 = ptr1 -> next;
ptr2 = ptr2 -> next;
}}
}