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
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
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
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->dataNULL.
Pseudocode
enqueue()
1. if(front==null)
2. front=rear=newnode;
3. else
4. {rear->next=newnode;
5. rear=newnode;}