You are on page 1of 20

Data Structures - I

“No Bonus” marks for this course anymore

CP
CS
20
4

O my Lord! Expand for me my chest [with


assurance] and ease for me my task and untie
the knot from my tongue that they may
understand my speech. (Quran 20 : 25-28)

1 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP
CS
20
4
Linked List
Operations

2 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Operations
CS
• Is Empty
20
4 • Traversing
• Modifying
• Searching
• Insertion
• Deletion
• Merging
3 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP
CS
20
4
Linked List
Operations
(Deletion)

4 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Linked Lists - Deletion


CS
• You must search for the node that you want to
20 delete (remember, we are using sorted lists)
4
• If found, you must delete the node from the
list
• This means that you change the various link
pointers
o The predecessor of the deleted node must point
to the deleted nodes successor

5 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List – Deletion


CS
• 4 – Cases
20
o Delete the only node in the list
4 o Delete the first node
o Delete the last node
o Anywhere between two nodes
• Pre – Condition
o List must be non - empty

6 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List – Deletion (only node of list)


CS
• Check if it is the only node 10 null
20
o head.data == value &
4 o head.next == null
• Make the node inaccessible
o head = null OR
10 null
o head = head . next
• Java garbage collector will
remove the node
7 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List – Deletion (only node of list)


CS public void delete(int value) {
20 if (!isEmpty()) {

if (head.data == value && head.next == null)


4
head = head.next;
}
}

10 null 10 null

• Value = 10
8 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List – Deletion (first node)


CS
• Check if it is the first node head
20
o head.data == value 10 20 50
4 null
• Move head to point next
head
node
o head = head . next 10 20 50 null

• Java garbage collector will


remove the first node

9 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List – Deletion (first node)


CS public void delete(int value) {
20 if (!isEmpty()) {

if (head.data == value)
4
head = head.next;
}
}
head

10 20 50 null

• Value = 10
10 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List – Deletion (Only node or first node)


CS public void delete(int value) {
20 if (!isEmpty()) {

if (head.data == value)
4
head = head.next;
}
}
head

10 20 50 null

• Value = 10
11 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
helpPt helpPt
head r r
CP 10 20 null 50 null

CS
• Traverse till 2nd last node
20 o helpPtr=head
4 o helpPtr.next.next != null
• Make helpPtr’s next point to null
o helpPtr.next = null
• Java garbage collector will
remove the last node

Linked List – Deletion (last node)


12 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
helpPt helpPt
head r r
CP 10 20 null 50 null

CS public void delete(int value) {


20 if (!isEmpty()) {
if (head.data == value)
4 head = head.next;
else{
LLnode help_ptr = head;
while (help_ptr.next.next != null) {
help_ptr = help_ptr.next;
}
helpPtr.next = null;
}
}
}
Linked List – Deletion (last node) 50
13 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
helpPt helpPt
head r r
CP 10 20 50 60 null

CS • Find the predecessor node


20 o helpPtr=head (initialize)
4 o helpPtr.next != null (loop condition)
o helpPtr.next.data == value (finding value)
• Connect predecessor node with the successor node
o helpPtr.next = helpPtr.next.next
• Java garbage collector will remove the last node

Linked List – Deletion (in middle)


14 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
helpPt helpPt
head r r
CP 10 20 50 60 null

CS public void delete(int value) {


if (!isEmpty()) {
20 if (head.data == value)
4 head = head.next;
else {
LLnode help_ptr = head;
while (help_ptr.next != null) {
if (help_ptr.next.data == value) {
help_ptr.next = help_ptr.next.next;
break;
}
help_ptr = help_ptr.next;
}}}}

Linked List – Deletion (in middle) 50


15 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List – Deletion (Complete Code)


CS public void delete(int value) {
20 if (!isEmpty()) {
if (head.data == value) // if “value” is in the first node
head = head.next;
4 // ELSE, value is (possibly) elsewhere in the list
else {
LLnode help_ptr = head;
while (help_ptr.next != null) {
if (help_ptr.next.data == value) {
help_ptr.next = help_ptr.next.next;
break;
}
help_ptr = help_ptr.next;
}
}
}
}

16 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP
CS
20
4
Linked List
Operations
(Merging)

17 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Linked Lists - Merge


CS
• Combining the data of two or more lists
20
into any other data structure e.g. another
4 linked list
• Find the last node of first linked list
• Connect the last node of 1st list to the first
node of 2nd list
• Make a new pointer to merged list (OPTIONAL)

18 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List – Merge (Non Empty lists)


CS public LLnode merge(LLnode head1, LLnode head2) {
LLnode help_ptr = head1;
20 while (help_ptr.next != null) {
help_ptr = help_ptr.next;

4 helpPtr.next = head2
return head1;
}
helpPt helpPt helpPt helpPt
head1 r r r r
10 20 50 60 null

head2
70 80 90 100 null

19 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP
CS
20
4

Allah (Alone) is Sufficient for us,


and He is the Best Disposer of
affairs (for us).(Quran 3 : 173)

20 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan

You might also like