SlideShare une entreprise Scribd logo
1  sur  38
A PRESENTATION ON DATA
STRUCTURE
COLLEGE OF VOCATIONAL STUDIES
(UNIVERSITY OF DELHI)
PREPARED BY : PINTU RAM
COURSE: BSC(H)CS
ROLL NO: 2K17/ CS/72
SUBMITTED TO: DHANANJAY SINGH
TOPICS
 Operation performed on Circular linked list
 Selection and Insertion Sort
 AVL tree
 Linked list implementation of Queue
TOPIC 1: Operation performed on
Circular linked list
 What is list? What is linked list?
 Circular linked list:
“Circular linked list is a sequence of elements in which every element has link
to its next element in the sequence and the last element has a link to the first
element in the sequence.”
Or,
“This means that circular linked list is similar to singly linked list except that
the last node points to the first node in the sequence.”
 Example:
10 1004 20 100115 1008
1001
1001 10081004
*head
Operations in circular linked list
 Insertion
1: Insertion at beginning of the list
2: Insertion at last of the list
3:Insertion at specific location in the list
 Deletion
1: Deletion at beginning of the list
2: Deletion at last of the list
3:Deletion at specific location in the list
 Display
Note: Before implementing actual operations first we need to setup empty list
and perform some following steps.
 Include all header files.
 All user defined function .
 Node structure with 2 members data and next.
 Main function
Inserting at beginning at the list
Steps:
1. Create a newnode with given value.
2. Check whether list is empty or not.
3. If list is empty then set: head=newnode & newnode ->next =head
4. If list is not empty then define a node pointer temp and initialize with head.
5. Keep moving the temp to its next node until it reaches to the last node.
6. Set, newnode->next=head & head=newnode & temp->next=head.
Inserting at beginning at list
Example:
10 1004 20 100115 1008
1001
1001 10081004
10 1004 20 100115 1008
add
1001 10081004
data 1001
add
Inserting at end of the list
 Steps 1,2,3,4,5 will remain same.
 Step6: Set, temp->next=newnode & newnode->next=head
10 1004 20 100115 1008
1001
1001 10081004
15 1008 Data 100115 add
1001
1004 add1008
10 1004
1001
Insertion at Specific of the list
 Step 1,2,3,4 will remain same.
 Step5: keep moving the temp to its next node until it reaches to the node
after which we want to insert the newnode.
 Step6: Every time check whether temp is reached to the last node or not. If it
is reached to the last node then display “THE GIVEN NODE IS NOT
FOUND IN LIST!!! INSERTION NOT POSSIBLE!!!” and terminate the
function otherwise move the temp to next node.
 Step7: If temp is not reached to the exact node after which we want to insert
a the newnode then whether it is last node (temp->next==head).
 Step8: If temp is not last node then set, newnode->next=temp->next &
temp->next=newnode.
Insertion at Specific of the list
Example:
10 1004 20 100115 1008
1001
1001 10081004
Data 1004 20 100115 1008
1001
add 10081004
10 add
1001
Deletion
“Deleting from Beginning of list”
Step1: check whether list is empty (head==NULL)
Step2: If it is empty then display “LIST IS EMPTY!!! DELETION NOT
POSSIBLE!!!” & terminate the function.
Step3: If it is not empty then define two node pointers temp1 & temp2 & initialize
both with head.
Step4: check whether list is having only one node (temp->next==head)
Step5: If it is true then set head=NULL & delete temp.
Step6: If it is false then move temp until it reaches to the last node (temp-
>next==head)
Step7: Then set, head=temp2->next, temp1->next=head & delete temp.
10 1004 20 100115 1008
1001 1001 10081004
15 1008 15 1008
1004
1004 1008
Deleting from end of the list
Step 1,2,3,4,5 will remain same.
Step6: If it is false then set, temp2=temp1 & move temp1 to its next node. Repeat
the same until temp1 reaches to the last node in the list.(until temp-
>next==head).
Step7: Set, temp2->next=head & delete temp1.
10 1004 20 100115 1008
1001
1001 10081004
10 1004 15 1008
1001
1001 1004
Deletion from Specific Location From
List
 Step1,2,3 will remain same.
 Step4: keep moving the temp1 until it reaches to the exact node to be
deleted or to the last node. And every time set temp2=temp1 before
moving the temp1 to its next node.
 Step5: If it is reached to the last node then display “GIVEN NODE NOT
FOUND IN THE LIST!!! DELETION NOT POSSILBE!!!” And terminate
the function.
 Step6: If it is reached to the exact node which we want to delete, then
check whether list is having only one node(temp1->next=head).
 Step7: If list has only one node and that is the node to be deleted then
set head=NULL & delete temp1.
 Step8: If list contains multiple nodes then check whether temp1 is the
first node in the list(temp1=head).
 Step9: If temp1 is the first mode then set temp2=head and keep moving
temp2 to its next node until temp2 reaches to the last node. Then set,
head=head->next, temp2->next=head, delete temp1.
 Step10: If temp1 is not first node then check whether it is last node in the
list(temp1->next==head).
 Step11: If temp1 is last node then set temp2->next=head & delete temp1.
 Step12: If temp1 is not first node and not last node then set temp2-
>next=temp1->next & delete temp1.
10 1004 20 100115 1008
1001
1001 10081004
10 1008 20 1001
1001
1001 1008
Displaying a circular linked list
o Step1,2,3 will remain same.
o Step4: Keep display temp->data with and arrow() until temp reaches to the
last node.
o Step5: Finally display temp->data with arrow pointing to head->data.
o Example: after deleting 15 from list of 10,15 and 20.
10 1004 20 100115 1008
1001
1001 10081004
10 1008 20 1001
1001
1001 1008
TOPIC 2: SELECTION SORT
 Algorithm to arrange a list of elements in a particular order (Ascending or
Descending).
 Step by Step Process:
 Step 1: Select the first element of the list (i.e., Element at first position in the
list).
 Step 2: Compare the selected element with all other elements in the list.
 Step 3: For every comparison, if any element is smaller than selected element
(for Ascending order), then these two are swapped.
 Step 4: Repeat the same procedure with next position in the list till the entire
list is sorted.
 Example: consider following unsorted list:
15 20 10 30 50 18 5 45
15 20 10 30 50 18 5 45
15>20 FALSE
15 20 10 30 50 18 5 45
15>10 TRUE,SWAP
10 20 15 30 50 18 5 45
10>30 FALSE
15 20 10 30 50 18 5 45
10>50 FALSE
Iteration #1:
10 20 10 30 50 18 5 45
10>18 FALSE
10 20 10 30 50 18 5 45
10>5 TRUE,SWAP
5 20 15 30 50 18 10 45
5>45 FALSE
5 20 15 30 50 18 5 45
List after first iteration
Iteration #2:
Iteration #3:
Iteration #4:
Iteration #5:
Iteration #6:
Iteration #7:
5 10 20 30 50 18 15 45
5 10 15 30 50 20 18 45
5 10 15 18 50 30 20 45
5 10 15 18 20 50 30 45
5 10 15 18 20 30 50 45
5 10 15 18 20 30 45 50
Final sorted list
Insertion Sort
 In this technique, every elements moves an element from unsorted portion to
sorted portion.
 Consider first element from the unsorted list and insert that element into
sorted list.
 Example: consider the following unsorted list:
15 20 10 30 50 18 5 45
15 20 10 30 50 18 5 45
unsortedsorted
15 20 10 30 50 18 5 45
unsortedsorted
15 20 10 30 50 18 5 45
unsortedsorted
10 15 20 30 50 18 5 45
unsortedsorted
10 15 20 30 50 18 5 45
unsortedsorted
10 15 20 30 50 18 5 45
unsortedsorted
10 15 18 20 30 50 5 45
unsortedsorted
5 10 15 18 20 30 50 45
unsortedsorted
5 10 15 18 20 30 45 50
unsortedsorted
Difference between selection sort and insertion sort
Insertion-sort: –
 Fast in beginning , because few elements need to be moved.
 Slows down at the end , because many moves must be performed.
 Comparisons:1st = 1, 2nd =2 … nth= n – 1.
 Best case: all elements are sorted! → O(n), worst case: all elements are sorted
in inverted order! → O(n2), average case: O(n2)
Selection-sort
 Slow in beginning, because many elements need to be compared to select the
smallest element.
 Fast in end , because few comparisons are needed.
 Best case: O(n2), worst case: O(n2), average case: O(n2) (Ouch!) → The
performance is independent from the input!
TOPIC 3: AVL TREE
Highlights :
 Self balanced binary search : means a binary tree that is balanced.
 Balance means: the difference left subtree and right subtree of every node
should be either 0,-1,+1.
 In other word: For every node, height of its children differ by at most one.
 Definition: An AVL tree is a balanced binary search tree. In which, balance
factor of every node is either -1, 0 or +1.
 Discovered by: In 1962, G.M Adelson-Velsky & E.M Landis
 Balance factor:
 Note: Every AVL Tree is a binary search tree but all the Binary Search Trees
need not to be AVL trees.
Balance factor = heightOfLeftSubtree - heightOfRightSubtree
Example: This tree is a binary search tree and every node is satisfying balance
factor condition. So this tree is said to be an AVL tree.
25
22
36
403010
20
382812 48
0
-1 010
01
0 000
AVL Tree Rotations
 Rotation is the process of moving the nodes to either left or right to make
balanced.
 There are 4 of rotations classified into 3 types:
Rotation
Double
Rotation
Right Left Rotation(RL Rotation)
Left Right Rotation(LR Rotation)
Right Rotation(R Rotation)
Single
Rotation
Left Rotation(L Rotation)
L Rotation
 Every node moves one position to left from the current position.
1
2
3
1 3
2
-1
0
-2
00
0
1
2
3
-1
0
-2
R Rotation
 Every node moves one position to right from the current position.
1
2
3
-1
0
1
2
3
-1
0
1 3
2
00
LR Rotation
 Combination of LL followed by RR.
 In LRR , first every node moves one position to left then one position to right
from the current position.
3
1
2 1 3
2
1
3
2
0
-2
2
0
1
2
00
0
RL Rotation
 Combination of RR followed by LR.
 In RLR Rotation, first every node moves one position to right then one position
to left from the current position.
1
3
2
1 3
2
3
1
2
-2
1
0
0
-1
-2
0 0
0
Operations on an AVL Tree
 Search
 Insertion
 Deletion
 Insertion in AVL Tree:
 In an AVL tree, the insertion operation is performed with O(log n) time
complexity. In AVL Tree, new node is always inserted as a leaf node. The
insertion operation is performed as follows...
 Step 1: Insert the new element into the tree using BST insertion logic.
 Step 2: After insertion, check the Balance Factor of every node.
 Step 3: If the Balance Factor of every node is 0 or 1 or -1 then go for next
operation.
 Example: Construct an AVL Tree by inserting numbers from 1 to 8.
Insertion in AVL Tree
 Insert 0: tree is balanced.
 Insert 2:tree is balanced.
 Insert 3:tree is unbalanced. Performing Left Rotation.
1
0
2
1
0
-1
3
-1
2
1
0
-2
1 4
2
00
0
 Insert 4: tree is balanced.
 Insert 5: tree is unbalanced, performing left rotation.
1 3
2
-10
-1
4
0
1
3
2
-2
0
4
-1
-2
5
0
1 4
2
00
-1
3
0
5
0
 Insert 6:tree is unbalanced. Performing left Rotation.
 Insert 7:tree is unbalanced, performing left rotation.
1 4
2
-10
-2
3
0
5
-1
6
0
2 5
4
-10
0
3
0
6
0
1
0
2 5
4
-20
3
0
6
-1
1
0
7
0
-1
2 6
4
00
3
0
7
0
1
0
5
0
 Insert 8: tree is balanced
2 6
4
-10
3
0
7
-1
1
0
5
8
0
-1
TOPIC 4: Queue Using Linked List
 Queue: A linear data structure in which the operations are performed based
on the FIFO technique.
 Example: A queue after inserting 25,30,51,60,85
 Operation on Queue: Enqueue, Dequeue, Display
 It can be implemented by 2 ways: using array & using linked list.
 When a queue is implemented using array, that queue can organize only
limited number of elements.
 When a queue is implemented using linked list , that queue can organize
unlimited number of elements.
 Queue using linked list: In linked list implementation of a queue, the last
inserted node is always pointed by rear and the first node is always pointed by
front.
25 30 51 60 85
Front Rear
 Example:
 Operations:
Before implementing actual operation, perform following steps-
1. Include all header files
2. Declare all user defined functions
3. Define node with 2 members: data & next
4. Define 2 node pointers: front & rear, set both as NULL
5. Implemented main method
 Enqueue:
1. Create a newnode with given value and set newnode->next to NULL.
2. Check whether queue is empty.
3. If it is empty set ,front=newnode & rear=newnode
4. If it is not empty set, rear ->next=newnode & rear=newnode
10 1008 20 NULL15 1012
Front
1004
1012
1008
data 1004
1001
Rear
 Dequeue:
1. Check whether queue is empty (front=Null)
2. If it is empty display, QUEUE IS EMPTY!!! DELETION NOT POSSIBLE
3. If it is not empty then define a node pointer temp & set it to front.
4. Then set front=front->next & delete temp.
 Display:
1. Check whether queue is empty (front=NULL).
2. If it is empty, QUEUE IS EMPTY.
3. If it is not empty, define a node pointer temp & initialize with front.
4. Display temp->data & move it to the next node. Repeat the same until temp
reaches to rear(temp->next!=NULL).
5. Finally display temp->dataNULL.
 Pseudocode
 enqueue()
1. if(front==null)
2. front=rear=newnode;
3. else
4. {rear->next=newnode;
5. rear=newnode;}
dequeue()
if(front==null)
“queue is empty”;
else
{struct node *temp=front;
front=front->next;
cout<<“deleted element:”<<temp->data;
delete temp;}
display()
if(front==null)
“queue is empty”
else{
struct node *temp=front;
while(temp->next!=null){
cout<<“”<<temp->data;
temp=temp->next;}
cout<<“null”<<temp->data;
Data Structure Presentation

Contenu connexe

Tendances

Sorting (introduction)
 Sorting (introduction) Sorting (introduction)
Sorting (introduction)
Arvind Devaraj
 
OEIS mining - poster
OEIS mining - posterOEIS mining - poster
OEIS mining - poster
Yangchen Pan
 

Tendances (20)

Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)
 
Sorting (introduction)
 Sorting (introduction) Sorting (introduction)
Sorting (introduction)
 
Simplex part 3 of 4
Simplex part 3 of 4Simplex part 3 of 4
Simplex part 3 of 4
 
stability ( Introduction (1) routh )
stability ( Introduction (1) routh )stability ( Introduction (1) routh )
stability ( Introduction (1) routh )
 
Analisys of Selection Sort and Bubble Sort
Analisys of Selection Sort and Bubble SortAnalisys of Selection Sort and Bubble Sort
Analisys of Selection Sort and Bubble Sort
 
Sorting algorithm
Sorting algorithmSorting algorithm
Sorting algorithm
 
Simplex part 2 of 4
Simplex part 2 of 4Simplex part 2 of 4
Simplex part 2 of 4
 
Sorting
SortingSorting
Sorting
 
1553 linear &amp; quadratic functions
1553 linear &amp; quadratic functions1553 linear &amp; quadratic functions
1553 linear &amp; quadratic functions
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
 
OEIS mining - poster
OEIS mining - posterOEIS mining - poster
OEIS mining - poster
 
K11019 SAMANT SINGH
K11019 SAMANT SINGHK11019 SAMANT SINGH
K11019 SAMANT SINGH
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
Selection and insertion sort
Selection and insertion sortSelection and insertion sort
Selection and insertion sort
 
Selection and insertion sort
Selection and insertion sortSelection and insertion sort
Selection and insertion sort
 
Regression Analysis - Thiyagu
Regression Analysis - ThiyaguRegression Analysis - Thiyagu
Regression Analysis - Thiyagu
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
RH CRITERIA
RH CRITERIARH CRITERIA
RH CRITERIA
 

Similaire à Data Structure Presentation

Similaire à Data Structure Presentation (20)

Circular linked list
Circular linked list Circular linked list
Circular linked list
 
1. 4 Singly linked list deletion
1. 4 Singly linked list deletion1. 4 Singly linked list deletion
1. 4 Singly linked list deletion
 
Linked list
Linked listLinked list
Linked list
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
 
1. 2 Singly Linked List
1. 2 Singly Linked List1. 2 Singly Linked List
1. 2 Singly Linked List
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptx
 
Selection Sort and Insertion Sort
Selection Sort and Insertion SortSelection Sort and Insertion Sort
Selection Sort and Insertion Sort
 
Selection sort and insertion sort
Selection sort and insertion sortSelection sort and insertion sort
Selection sort and insertion sort
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Sorting and Its Types
Sorting and Its TypesSorting and Its Types
Sorting and Its Types
 
day 13.pptx
day 13.pptxday 13.pptx
day 13.pptx
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssssDSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
 

Dernier

Dernier (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 

Data Structure Presentation

  • 1. A PRESENTATION ON DATA STRUCTURE COLLEGE OF VOCATIONAL STUDIES (UNIVERSITY OF DELHI) PREPARED BY : PINTU RAM COURSE: BSC(H)CS ROLL NO: 2K17/ CS/72 SUBMITTED TO: DHANANJAY SINGH
  • 2. TOPICS  Operation performed on Circular linked list  Selection and Insertion Sort  AVL tree  Linked list implementation of Queue
  • 3. TOPIC 1: Operation performed on Circular linked list  What is list? What is linked list?  Circular linked list: “Circular linked list is a sequence of elements in which every element has link to its next element in the sequence and the last element has a link to the first element in the sequence.” Or, “This means that circular linked list is similar to singly linked list except that the last node points to the first node in the sequence.”  Example: 10 1004 20 100115 1008 1001 1001 10081004 *head
  • 4. Operations in circular linked list  Insertion 1: Insertion at beginning of the list 2: Insertion at last of the list 3:Insertion at specific location in the list  Deletion 1: Deletion at beginning of the list 2: Deletion at last of the list 3:Deletion at specific location in the list  Display Note: Before implementing actual operations first we need to setup empty list and perform some following steps.  Include all header files.  All user defined function .  Node structure with 2 members data and next.  Main function
  • 5. Inserting at beginning at the list Steps: 1. Create a newnode with given value. 2. Check whether list is empty or not. 3. If list is empty then set: head=newnode & newnode ->next =head 4. If list is not empty then define a node pointer temp and initialize with head. 5. Keep moving the temp to its next node until it reaches to the last node. 6. Set, newnode->next=head & head=newnode & temp->next=head.
  • 6. Inserting at beginning at list Example: 10 1004 20 100115 1008 1001 1001 10081004 10 1004 20 100115 1008 add 1001 10081004 data 1001 add
  • 7. Inserting at end of the list  Steps 1,2,3,4,5 will remain same.  Step6: Set, temp->next=newnode & newnode->next=head 10 1004 20 100115 1008 1001 1001 10081004 15 1008 Data 100115 add 1001 1004 add1008 10 1004 1001
  • 8. Insertion at Specific of the list  Step 1,2,3,4 will remain same.  Step5: keep moving the temp to its next node until it reaches to the node after which we want to insert the newnode.  Step6: Every time check whether temp is reached to the last node or not. If it is reached to the last node then display “THE GIVEN NODE IS NOT FOUND IN LIST!!! INSERTION NOT POSSIBLE!!!” and terminate the function otherwise move the temp to next node.  Step7: If temp is not reached to the exact node after which we want to insert a the newnode then whether it is last node (temp->next==head).  Step8: If temp is not last node then set, newnode->next=temp->next & temp->next=newnode.
  • 9. Insertion at Specific of the list Example: 10 1004 20 100115 1008 1001 1001 10081004 Data 1004 20 100115 1008 1001 add 10081004 10 add 1001
  • 10. Deletion “Deleting from Beginning of list” Step1: check whether list is empty (head==NULL) Step2: If it is empty then display “LIST IS EMPTY!!! DELETION NOT POSSIBLE!!!” & terminate the function. Step3: If it is not empty then define two node pointers temp1 & temp2 & initialize both with head. Step4: check whether list is having only one node (temp->next==head) Step5: If it is true then set head=NULL & delete temp. Step6: If it is false then move temp until it reaches to the last node (temp- >next==head) Step7: Then set, head=temp2->next, temp1->next=head & delete temp. 10 1004 20 100115 1008 1001 1001 10081004 15 1008 15 1008 1004 1004 1008
  • 11. Deleting from end of the list Step 1,2,3,4,5 will remain same. Step6: If it is false then set, temp2=temp1 & move temp1 to its next node. Repeat the same until temp1 reaches to the last node in the list.(until temp- >next==head). Step7: Set, temp2->next=head & delete temp1. 10 1004 20 100115 1008 1001 1001 10081004 10 1004 15 1008 1001 1001 1004
  • 12. Deletion from Specific Location From List  Step1,2,3 will remain same.  Step4: keep moving the temp1 until it reaches to the exact node to be deleted or to the last node. And every time set temp2=temp1 before moving the temp1 to its next node.  Step5: If it is reached to the last node then display “GIVEN NODE NOT FOUND IN THE LIST!!! DELETION NOT POSSILBE!!!” And terminate the function.  Step6: If it is reached to the exact node which we want to delete, then check whether list is having only one node(temp1->next=head).  Step7: If list has only one node and that is the node to be deleted then set head=NULL & delete temp1.  Step8: If list contains multiple nodes then check whether temp1 is the first node in the list(temp1=head).  Step9: If temp1 is the first mode then set temp2=head and keep moving temp2 to its next node until temp2 reaches to the last node. Then set, head=head->next, temp2->next=head, delete temp1.
  • 13.  Step10: If temp1 is not first node then check whether it is last node in the list(temp1->next==head).  Step11: If temp1 is last node then set temp2->next=head & delete temp1.  Step12: If temp1 is not first node and not last node then set temp2- >next=temp1->next & delete temp1. 10 1004 20 100115 1008 1001 1001 10081004 10 1008 20 1001 1001 1001 1008
  • 14. Displaying a circular linked list o Step1,2,3 will remain same. o Step4: Keep display temp->data with and arrow() until temp reaches to the last node. o Step5: Finally display temp->data with arrow pointing to head->data. o Example: after deleting 15 from list of 10,15 and 20. 10 1004 20 100115 1008 1001 1001 10081004 10 1008 20 1001 1001 1001 1008
  • 15. TOPIC 2: SELECTION SORT  Algorithm to arrange a list of elements in a particular order (Ascending or Descending).  Step by Step Process:  Step 1: Select the first element of the list (i.e., Element at first position in the list).  Step 2: Compare the selected element with all other elements in the list.  Step 3: For every comparison, if any element is smaller than selected element (for Ascending order), then these two are swapped.  Step 4: Repeat the same procedure with next position in the list till the entire list is sorted.  Example: consider following unsorted list: 15 20 10 30 50 18 5 45
  • 16. 15 20 10 30 50 18 5 45 15>20 FALSE 15 20 10 30 50 18 5 45 15>10 TRUE,SWAP 10 20 15 30 50 18 5 45 10>30 FALSE 15 20 10 30 50 18 5 45 10>50 FALSE Iteration #1:
  • 17. 10 20 10 30 50 18 5 45 10>18 FALSE 10 20 10 30 50 18 5 45 10>5 TRUE,SWAP 5 20 15 30 50 18 10 45 5>45 FALSE 5 20 15 30 50 18 5 45 List after first iteration
  • 18. Iteration #2: Iteration #3: Iteration #4: Iteration #5: Iteration #6: Iteration #7: 5 10 20 30 50 18 15 45 5 10 15 30 50 20 18 45 5 10 15 18 50 30 20 45 5 10 15 18 20 50 30 45 5 10 15 18 20 30 50 45 5 10 15 18 20 30 45 50 Final sorted list
  • 19. Insertion Sort  In this technique, every elements moves an element from unsorted portion to sorted portion.  Consider first element from the unsorted list and insert that element into sorted list.  Example: consider the following unsorted list: 15 20 10 30 50 18 5 45 15 20 10 30 50 18 5 45 unsortedsorted 15 20 10 30 50 18 5 45 unsortedsorted 15 20 10 30 50 18 5 45 unsortedsorted
  • 20. 10 15 20 30 50 18 5 45 unsortedsorted 10 15 20 30 50 18 5 45 unsortedsorted 10 15 20 30 50 18 5 45 unsortedsorted 10 15 18 20 30 50 5 45 unsortedsorted 5 10 15 18 20 30 50 45 unsortedsorted 5 10 15 18 20 30 45 50 unsortedsorted
  • 21. Difference between selection sort and insertion sort Insertion-sort: –  Fast in beginning , because few elements need to be moved.  Slows down at the end , because many moves must be performed.  Comparisons:1st = 1, 2nd =2 … nth= n – 1.  Best case: all elements are sorted! → O(n), worst case: all elements are sorted in inverted order! → O(n2), average case: O(n2) Selection-sort  Slow in beginning, because many elements need to be compared to select the smallest element.  Fast in end , because few comparisons are needed.  Best case: O(n2), worst case: O(n2), average case: O(n2) (Ouch!) → The performance is independent from the input!
  • 22. TOPIC 3: AVL TREE Highlights :  Self balanced binary search : means a binary tree that is balanced.  Balance means: the difference left subtree and right subtree of every node should be either 0,-1,+1.  In other word: For every node, height of its children differ by at most one.  Definition: An AVL tree is a balanced binary search tree. In which, balance factor of every node is either -1, 0 or +1.  Discovered by: In 1962, G.M Adelson-Velsky & E.M Landis  Balance factor:  Note: Every AVL Tree is a binary search tree but all the Binary Search Trees need not to be AVL trees. Balance factor = heightOfLeftSubtree - heightOfRightSubtree
  • 23. Example: This tree is a binary search tree and every node is satisfying balance factor condition. So this tree is said to be an AVL tree. 25 22 36 403010 20 382812 48 0 -1 010 01 0 000
  • 24. AVL Tree Rotations  Rotation is the process of moving the nodes to either left or right to make balanced.  There are 4 of rotations classified into 3 types: Rotation Double Rotation Right Left Rotation(RL Rotation) Left Right Rotation(LR Rotation) Right Rotation(R Rotation) Single Rotation Left Rotation(L Rotation)
  • 25. L Rotation  Every node moves one position to left from the current position. 1 2 3 1 3 2 -1 0 -2 00 0 1 2 3 -1 0 -2
  • 26. R Rotation  Every node moves one position to right from the current position. 1 2 3 -1 0 1 2 3 -1 0 1 3 2 00
  • 27. LR Rotation  Combination of LL followed by RR.  In LRR , first every node moves one position to left then one position to right from the current position. 3 1 2 1 3 2 1 3 2 0 -2 2 0 1 2 00 0
  • 28. RL Rotation  Combination of RR followed by LR.  In RLR Rotation, first every node moves one position to right then one position to left from the current position. 1 3 2 1 3 2 3 1 2 -2 1 0 0 -1 -2 0 0 0
  • 29. Operations on an AVL Tree  Search  Insertion  Deletion  Insertion in AVL Tree:  In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL Tree, new node is always inserted as a leaf node. The insertion operation is performed as follows...  Step 1: Insert the new element into the tree using BST insertion logic.  Step 2: After insertion, check the Balance Factor of every node.  Step 3: If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.  Example: Construct an AVL Tree by inserting numbers from 1 to 8.
  • 30. Insertion in AVL Tree  Insert 0: tree is balanced.  Insert 2:tree is balanced.  Insert 3:tree is unbalanced. Performing Left Rotation. 1 0 2 1 0 -1 3 -1 2 1 0 -2 1 4 2 00 0
  • 31.  Insert 4: tree is balanced.  Insert 5: tree is unbalanced, performing left rotation. 1 3 2 -10 -1 4 0 1 3 2 -2 0 4 -1 -2 5 0 1 4 2 00 -1 3 0 5 0
  • 32.  Insert 6:tree is unbalanced. Performing left Rotation.  Insert 7:tree is unbalanced, performing left rotation. 1 4 2 -10 -2 3 0 5 -1 6 0 2 5 4 -10 0 3 0 6 0 1 0 2 5 4 -20 3 0 6 -1 1 0 7 0 -1 2 6 4 00 3 0 7 0 1 0 5 0
  • 33.  Insert 8: tree is balanced 2 6 4 -10 3 0 7 -1 1 0 5 8 0 -1
  • 34. TOPIC 4: Queue Using Linked List  Queue: A linear data structure in which the operations are performed based on the FIFO technique.  Example: A queue after inserting 25,30,51,60,85  Operation on Queue: Enqueue, Dequeue, Display  It can be implemented by 2 ways: using array & using linked list.  When a queue is implemented using array, that queue can organize only limited number of elements.  When a queue is implemented using linked list , that queue can organize unlimited number of elements.  Queue using linked list: In linked list implementation of a queue, the last inserted node is always pointed by rear and the first node is always pointed by front. 25 30 51 60 85 Front Rear
  • 35.  Example:  Operations: Before implementing actual operation, perform following steps- 1. Include all header files 2. Declare all user defined functions 3. Define node with 2 members: data & next 4. Define 2 node pointers: front & rear, set both as NULL 5. Implemented main method  Enqueue: 1. Create a newnode with given value and set newnode->next to NULL. 2. Check whether queue is empty. 3. If it is empty set ,front=newnode & rear=newnode 4. If it is not empty set, rear ->next=newnode & rear=newnode 10 1008 20 NULL15 1012 Front 1004 1012 1008 data 1004 1001 Rear
  • 36.  Dequeue: 1. Check whether queue is empty (front=Null) 2. If it is empty display, QUEUE IS EMPTY!!! DELETION NOT POSSIBLE 3. If it is not empty then define a node pointer temp & set it to front. 4. Then set front=front->next & delete temp.  Display: 1. Check whether queue is empty (front=NULL). 2. If it is empty, QUEUE IS EMPTY. 3. If it is not empty, define a node pointer temp & initialize with front. 4. Display temp->data & move it to the next node. Repeat the same until temp reaches to rear(temp->next!=NULL). 5. Finally display temp->dataNULL.  Pseudocode  enqueue() 1. if(front==null) 2. front=rear=newnode; 3. else 4. {rear->next=newnode; 5. rear=newnode;}
  • 37. dequeue() if(front==null) “queue is empty”; else {struct node *temp=front; front=front->next; cout<<“deleted element:”<<temp->data; delete temp;} display() if(front==null) “queue is empty” else{ struct node *temp=front; while(temp->next!=null){ cout<<“”<<temp->data; temp=temp->next;} cout<<“null”<<temp->data;