3
3
A Simplified Approach
to
Data Structures
Prof.(Dr.)Vishal Goyal, Professor, Punjabi University Patiala
Dr. Lalit Goyal, Associate Professor, DAV College, Jalandhar
Mr. Pawan Kumar, Assistant Professor, DAV College, Bhatinda
2
Contents
3
Introduction to Linked List
Linked List
4
Types of Linked List
5
One Way Linked List
• The first part is known as info part which holds the element.
6
One Way Linked List
7
Operations of 1-Way Linked List
• Traversing a linked list.
9
Algorithm : Traversal of linked list
Step1.If Begin=Null then
Print ”linked list is empty”
Exit
[End If]
Step2. Set Pointer =Begin
Step3.Repeat While Pointer!=Null
a. print : Pointer Info
b. Assign Pointer =Pointer Next
[End loop]
Step4.Exit
10
Searching In A Linked List
11
Searching In A Link List
Pointer
102
12
Algorithm :Searching a Linked List
14
Insertion In Linked List
15
Insertion In Linked List (Continued)
Where we can insert element in linked list?
• At end
16
Insertion At Beginning
Begin 12 Null
400
17
Algorithm : Insertion At Beginning
Step 1: If Free = Null Then
Print : “Overflow: No free available for insertion”
Exit
[End if]
Step 2: Allocate Space to node New
(set New = Free And Free = Free Next)
Step 3: Set New Info = Data
Step 4: Set New Next = Begin And Begin = New
Step 5: Exit
18
Insertion At End
• If list is empty, store null value in next part of new node and insert
item in the info part
• Store address of new node into next part of the last node of the
linked list and the next part set to null.
Pointer
21
Insertion At Any Location
Item
Pointer
22
Algorithm : Insertion At Any Location
24
Insertion In Sorted Linked List
• Find the position of the node after which new node has to be
inserted
25
Insertion In A Sorted Linked List
Pointer
403
New
26
Algorithm : Insertion At Any Location In sorted Link List
Step 1: If Begin = Null Then
Allocate Space to node New
Set New = Free and Free = Free Next
Set New Info = Item
Set New Next = Begin and Begin = New
[End If]
Step 2: If Item < Begin Info Then
Allocate Space to node New
Set New = Free And Free = Free Next
Set New Info = Item
Set New Next = Begin and Begin = New
Exit
[End If]
Step 3: Set Pointer = Begin and Pointer2 = Begin Next
27
Step 4: Repeat While Pointer2 != Null and Item > pointer2 Info
Set Pointer1 = Pointer2 and Pointer2 = Pointer2 Next
[End loop]
Step 5: If Free = Null Then
Print : “No space for insertion , Allocation of space to node
New is not possible”
Exit
[End If]
Step 6: Allocate space to node New
Set New = Free and Free = Free Next
Step 7: Set New Info = Item
Step 8: If Pointer2 = Null Then
Set Pointer1 Next = New and New Next = Null
Else
Set New Next = Pointer Next
Set Pointer1 Next = New
[End If]
Step 9: Exit 28
Deletion From Linked List
29
Deleting A Node At Begin.
• Deletion of a node at the begin of the list is a very simple operation
which can be done by changing the list pointer variable begin.
• The space occupied by the deleted node is returned to the free storage
list.
Free Null
31
Deleting A Node At End
• For deleting the lost node from the given plinked list, it is
necessary to traverse the entire linked list for finding the
address of the preceding node of the last node i.e, address
of second last node.
32
Deleting A Node At End
Pointer1 Pointer2
Free Null
Pointer1->Next=Pointer2->Next
Pointer1 = Begin Pointer1 = Pointer2 Pointer2->Next=Free
Pointer2 = Begin->Next Pointer2 = Pointer2 -> Next Free=Pointer2
33
Algorithm : Deletion At End
35
Delete A Particular Node From Link List
• For deleting a particular node from the linked list, the first
task is to find the address of the preceding node of nth
node to be deleted.
• To complete the task traverse the linked list from begin and
compare the info. Stored in node with item.
36
Deleting A Particular Node In Link List
Begin
4
25 17 8 Null
Pointer1 Pointer2
Free
Null
Pointer1 ->Next=Pointer2->Next
Pointer2->Next=Free
Free=Pointer2
37
Algorithm: Deletion At Any Location
Step 1 :If Begin = Null Then
Print : “Linked List is Empty”
Exit
[End If]
Step 2: If Begin Info = Item Then
Set Pos = Begin
Set Begin = Begin Next
Pos Next = Free and Free = Pos
Exit
[End If]
Step 3: Set Pointer1 = Begin and Pointer2 = Begin Next
Step 4: Repeat While Pointer2! = Null and Pointer2 Info!= Item
SET Pointer1 = Pointer2 and Pointer2 Next
[End loop]
38
Step 5: If Pointer2 = Null Then
Print :”Node containing element item not found”
Exit
Else
Set Pointer1 Next = Pointer2 Next
[End If]
Step 6: Deallocate memory held by Pointer2
(Set Pointer2 Next = Free and Free = Pointer2)
Step 7: Exit
39
Copy A Link List Into Other Link List
• Consider the linked list with its start pointer as begin1.For
copying this given linked list into another list, use a new
pointer variable begin2 for the list in which source list will be
copied.
• Now we will traverse the entire source list from begin to the
end by copying the contents to the new target.
40
Copy A Link List Into Other Link List
Begin1 25 17 8 38 Null
Pointer
Begin2 25 17 8 38 Null
43
Merging Two Linked List
• There are number of applications where there is need to merge
two or more linked lists into a single linked list.
Begin1 25 27 42 Null
Begin2 10 17 30 Null
44
After Merging
Begin1 25 27 42 Null
Pointer1
Begin2 10 17 30 Null
Pointer2
Begin
Merged Complete List
10 17 25 27 30 42 Null
45
Algorithm : Merging Two Sorted Linked List
Step 1: If Begin1=Null or Begin2=Null then
Print “one of the given linked list is empty”
Exit
[end if]
Step 2: If Free =Null then
Print: “no free space available”
Exit
Else
//Allocate memory to node New
Set New =Free and Free=Free Next
[End If]
Step 3: Set Begin=Null
Step 4: If Begin1 Info > =Begin2 Info then
Set New Info=Begin2 Info and New Next=Null
Set Pointer1=Begin1 and Pointer2=Begin2 Next
46
Else
Set New Info=Begin1 Info and New Next=Null
Set Pointer1=Begin1 Next and Pointer2=Begin2
[End If]
Step 5: Set Begin=New and Pointer =New
Step 6: Repeat steps 7and 8 while Pointer1!=Null and Pointer2!=Null
Step 7: If Free=Null then
Print “No free space available”
Exit
Else
Set New =Free and Free=Free Next
[End If]
Step 8: If Pointer1 Info >=Pointer2 Info then
Set New Info=Pointer Info
Set New Next=Null
Set Pointer Next=New
Set Pointer=New and Pointer2=Pointer2 Next 47
[End If]
[End Loop]
Step 9: If Pointer1=Null and Free!=Null then
Repeat while Pointer2!=Null
a. Set New=Free and Free=Free Next
b. Set New Info=Pointer2 Info and
New Next =Null
c. Set Pointer Next=New
d. Set Pointer =New and Pointer2=Pointer2 Next
[End Loop]
Else
Repeat while Pointer1!=Null
a. Set New=Free and Free=Free Next
b. Set New Info=Pointer1 Info and
New Next=Null
c.Set Pointer Next=New
d. Set Pointer=New and Pointer1=Pointer1 Next48
[End Loop]
[End If]
Step 10: If Pointer1 = Null And Pointer2=Null Then
Print: “The given link lists merged successfully”
Else
Print “Not Enough space”
[End If]
Step 11: Exit
49
Splitting Two Lists
• Now our list divide into 2 parts n/2 and n-n/2 with list
begin1 and begin2.
50
Splitted List1 And List2 .
Begin
10 17 25 27 28 32 Null
Pointer
Begin1
10 17 25 Null
Begin2
27 28 30 Null
51
Algorithm: Split A Link List Into Two Link Lists.
Step 1: If Begin=Null
Print: “Splitting cannot be performed on empty list”
Exit
[End If]
Step 2: Set pointer= Begin And Count=0
Step 3: Repeat Steps 4 and 5 While Pointer!=Null
Step 4: Set Count=Count 1
Step 5: Set Pointer=pointer Next
[End Loop]
Step 6: Set Mid=Integer(count/2)
Step 7: Set Begin2=Null And Pointer=Begin And i =1
Step 8: Repeat Step 9 While i<Mid
52
Step 9: Set Pointer=Pointer Next
Set i=i 1
[End Loop]
Step 10: Set Begin2 = Pointer Next And
Pointer Next = Null
Step 11: Exit
53
Reversing A One Way Linked List
• To reverse a linked list, we need to use three pointers
variables.
54
Reversing A One Way Linked List
Begin1
5 10 Null
Begin2
5 Null 10
55
Reverse Link List With More Than Two Nodes
Begin
10 17 25 27 28 42 Null
Begin
10 Null 17 25 27 28 42
56
Algorithm: Reverse The One Way Link List