SlideShare a Scribd company logo
Data Structures
CCE 411(2013)
CCE 332(2019)
El-Shorouk Academy
The Higher Institute of Engineering, El-Shorouk City
Dr. Ahmed El-Shafei
Communications and Computer Engineering Department
Data Structures - Computer and Control Engineering- First Term
25 October 2024
1
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Notes
Group (2)
Group (1)
Lecture
#
L_01_DS_24-25
2/10/2024
30/9/2024
1
L_01_DS_24-25
9/10/2024
7/10/2024
2
L_02_DS_24-25
16/10/2024
14/10/2024
3
L_03_DS_24-25
23/10/2024
21/10/2024
4
5
6
7
8
9
10
11
12
13
3
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Lists
Lecture 4
4
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Lists
Merging Two sorted link lists
▪ To merge two sorted link list and create a third list in the same sequence of inputted link list.
Algorithm:
Step 1 PTR1 = FIRST1
PTR2 = FIRST2
Step 2 PTR = AVAIL
AVAIL = LINK(AVAIL)
FIRST = PTR
Step 3 If INFO (PTR1) < INFO (PTR2) THEN
INFO (PTR) = INFO(PTR1)
PTR1 = LINK (PTR1)
Otherwise
INFO(PTR) = INFO (PTR2)
PTR2 = LINK (PTR2)
Step 4 Repeat Step 5 to Step 7 while PTR1 ≠ NULLAND PTR2 ≠ NULL
Step 5 CPT = AVAIL
AVAIL = LINK(AVAIL)
Step 6 If INFO (PTR1) < INFO (PTR2) THEN
INFO (CPT) = INFO(PTR1)
PTR1 = LINK (PTR1)
Otherwise
INFO(CPT) = INFO (PTR2)
PTR2 = LINK (PTR2)
5
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Lists
Merging Two sorted link lists
Step 7 LINK (PTR) = CPT
PTR = CPT
Step 8 Repeat step 9 while PTR1 ≠ NULL
Step 9 (i) CPT = AVAIL
AVAIL = LINK (AVAIL)
INFO (CPT) = INFO (PTR1)
PTR1 = LINK (PTR1)
(ii) LINK (PTR) = CPT
PTR = CPT
(iii) PTR2 = LINK (PTR1)
Step 10 Repeat step 11 while PTR2 ≠ NULL
Step 11 (i) CPT = AVAIL
AVAIL = LINK (AVAIL)
INFO (CPT) = INFO (PTR2)
PTR1 = LINK (PTR1)
(ii) LINK (PTR) = CPT
PTR = CPT
(iii) PTR2 = LINK (PTR2)
Step 12 LINK (PTR) = NULL
Step 13 STOP
6
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Lists
Example: Merge the following two sorted link lists. Merge these link lists into third link lists
5 8 12 20 NULL
FIRST 1
2 10 15 NULL
FIRST 2
Initially, PTR1 = FIRST1 and PTR2 = FIRST2
INFO (PTR1) = 5
INFO (PTR2) = 2
Here, INFO (PTR2) < INFO (PTR1)
So, create a new node PTR and INFO (PTR) = INFO (PTR2) (for the new list that will include the merge result)
PTR
i.e., and PTR2 = LINK (PTR2)
i.e., now
2
5 8
12 20 NULL
PTR 1
2 10 15 NULL
PTR 2
PTR
2
7
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Lists
INFO (PTR1) = 5
INFO (PTR2) = 10
Here, INFO (PTR2) < INFO (PTR1)
So, create a new node CPT and INFO (CPT) = INFO (PTR1)
LINK (PTR) = CPT
PTR = CPT
i.e., 2
PTR
5 and PTR1 = LINK (PTR1)
Now, 5 8 12 20 NULL
PTR 1
2 10 15 NULL
PTR 2
2 5
PTR
8
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Lists
INFO (PTR1) = 8
INFO (PTR2) = 10
Here, INFO (PTR1) < INFO (PTR2)
So, create a new node CPT and INFO (CPT) = INFO (PTR1)
PTR1 = LINK (PTR1)
LINK (PTR) = CPT
PTR = CPT
Now link lists are:
5 8 12 20 NULL
PTR 1
2 10 15 NULL
PTR 2
2 5 8
PTR
9
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Lists
INFO (PTR1) = 12
INFO (PTR2) = 10
Here, INFO (PTR2) < INFO (PTR1)
So, create a new node CPT and INFO (CPT) = INFO (PTR2)
PTR2 = LINK (PTR2)
LINK (PTR) = CPT
PTR = CPT
Now link lists are:
5 8 12 20 NULL
PTR 1
2 10 15 NULL
PTR 2
2 5 8
PTR
10
10
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Lists
INFO (PTR1) = 12
INFO (PTR2) = 15
Here, INFO (PTR1) < INFO (PTR2)
So, create a new node CPT and INFO (CPT) = INFO (PTR1)
PTR1 = LINK (PTR1)
LINK (PTR) = CPT
PTR = CPT
Now link lists are:
5 8 12 20 NULL
PTR 1
2 10 15 NULL
PTR 2
2 5 8 10
PTR
12
11
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Lists
INFO (PTR1) = 20
INFO (PTR2) = 15
Here, INFO (PTR2) < INFO (PTR1)
So, create a new node CPT and INFO (CPT) = INFO (PTR2)
PTR2 = LINK (PTR2) = NULL
LINK (PTR) = CPT
PTR = CPT
Now link lists are:
5 8 12 20 NULL
PTR 1
2 10 15 NULL
2 5 8 10 12
PTR
15
12
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Lists
Here PTR1 ≠ NULL and PTR2 = NULL
INFO (PTR1) = 20
So, create a new node CPT and INFO (CPT) = INFO (PTR1)
PTR1 = LINK (PTR1) = NULL
LINK (PTR) = CPT
PTR = CPT
Now , PTR1 = NULL and PTR2 = NULL
So, LINK (PTR) = NULL i.e., final link-list after merging is:
2 5 8 10 12
15 20 NULL
13
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Lists
Reversing a List
▪ To reverse a linear linked list, three pointer fields are used. These are tpt, ptr, cpt which
hold the address of previous node, current node, and next node in the linear linked list.
▪ To begin with the address of the first node, which is held in pointer variable FIRST, is
assigned to PTR and the tpt is assigned value NULL.
▪
Algorithm:
Step 1 PTR = FIRST
Step 2 TPT = NULL
Step 3 Repeat step 4 while PTR ≠ NULL
Step 4 (a) CPT = LINK (PTR)
(b) LINK (PTR) = TPT
(c) TPT = PTR
(d) PTR = CPT
Step 5 STOP
14
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Lists
Example: Reverse the following link list
5 10 15 20 NULL
FIRST
Solution:
Initially, PTR = FIRST
TPT = NULL
PTR ≠ NULL So,
CPT = LINK (PTR)
LINK (PTR) = TPT i.e., NULL
and TPT = PTR
PTR = CPT
i.e.,
5 10 15 20 NULL
PTR CPT
TPT PTR
Now, PTR is node 10, TPT is node 5
Again, PTR ≠ NULL,
So, CPT = LINK(PTR), i.e., node 15
5 10 15 20 NULL
TPT
NULL
PTR CPT
15
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Lists
Now, LINK(PTR) = TPT
i.e.,
10 5
PTR TPT
NULL
and TPT = PTR
i.e., node 10 is now TPT
PTR = CPT
i.e., node 15 is now PTR
Now,
10 5
TPT
NULL
15 20
PTR CPT
NULL
16
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Lists
Again, PTR ≠ NULL
Now, CPT = LINK(PTR), i.e., node 20
LINK (PTR) = TPT
i.e.,
15 10 5 NULL
PTR TPT
and TPT = PTR i.e., node 15
PTR = CPT i.e., node 20
Now,
15 10 5 NULL
TPT
20 NULL
PTR
17
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Lists
Again, PTR ≠ NULL
Now, CPT = LINK(PTR), i.e., NULL
So, CPT = NULL
LINK(PTR) = TPT, i.e.,
15 10 5 NULL
TPT
20
PTR
and TPT = PTR i.e., node 20
PTR = CPT i.e., PTR = NULL
Now, PTR = NULL so STOP, and the final link list is
LINK(PTR) = TPT, i.e.,
15 10 5 NULL
20
18
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
SEARCHINING
19
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
20
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
▪ Searching is an operation or a technique that helps finds the place of a given
element or value in the list. Any search is said to be successful or
unsuccessful depending upon whether the element that is being searched is
found or not.
▪ Search algorithms are algorithms designed to search for or retrieve elements
from a data Structures, where they are stored. They are essential to access
desired elements in a data Structures and retrieve them when a need arises.
▪ A vital aspect of search algorithms is Path Finding, which is used to find
paths that can be taken to traverse from one point to another, by finding the
most optimum route.
▪ Some of the standard searching technique that is being followed in data
Structures is listed below:
➢ Linear / Sequential Search
➢ Binary Search
21
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
➢ Linear / Sequential Search
Linear search is a very basic and simple search algorithm. In Linear search, we
search an element or value in a given array by traversing the array from the
starting, till the desired element or value is found. It compares the element to be
searched with all the elements present in the array and when the element is
matched successfully, it returns the index of the element in the array, else it
return -1. Linear Search is applied on unsorted or unordered lists, when there
are fewer elements in a list. For Example,
10 14 19 26 27 31 33 35 42 44 50 56 62 69 70 75
Features of Linear Search Algorithm
1. It is used for unsorted and unordered small list of elements.
2. It has a time complexity of O(n), which means the time is linearly dependent
on the number of elements, which is not bad, but not that good too.
3. It has a very simple implementation.
22
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Linear / Sequential Search
Algorithm
Linear Search ( Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: STOP
procedure linear_search (list, value)
for each item in the list
if match item == value
return the item’s location
end if
end for
end procedure
23
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Linear Search
Flowchart: Sequential Search with Array
Start
i = 0
K = A[i]?
Print "Successful"
Print "Unsuccessful"
i = i+1
i ≥ n
Stop
Yes
Yes
No
No
24
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Complexity Analysis
• Case 1: The key matches with the first element
T(n) = 1
• Case 2: Key does not exist
T(n) = n
• Case 3: The key is present at any location in the array

=

=
n
i
i i
p
n
T
1
)
(
n
p
p
p
p n
i
1
2
1 =



=



=
=

=
=
n
i
i
n
n
T
1
1
)
(
2
1
)
(
+
=
n
n
T
Linear Search
25
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Complexity Analysis : Summary
Case
Number of key
comparisons
Asymptotic
complexity
Remark
Case 1 T(n) = 1 T(n) = O(1) Best case
Case 2 T(n) = n T(n) = O(n) Worst case
Case 3 T(n) = O(n) Average case
2
1
)
(
+
=
n
n
T
Linear Search
26
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Binary Search
l u
mid = (l+u)/2
(a) An ordered array of elemnets with index values l, u and mid
l u
mid
(b) Search the entire list turns into the searching of left-half only
u = mid-1
Serach this half the same way
if K < A[mid]
l u
mid
l = mid+1 Serach this half the same way
if K > A[mid]
(c) Search the entire list turns into the searching of right-half only
Binary Search Technique
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Data Structures ‫بيانات‬ ‫هياكل‬
28
Data Structures ‫بيانات‬ ‫هياكل‬
Data Structures - Computer and Control Engineering- First Term
25 October 2024
Flowchart: Binary Search with Array
mid = (l+u)/2
K = A[mid]?
Start
Search is successful
YES NO
K < A[mid]?
YES
u = mid-1 l = mid+1
NO
Stop
(l>u)?
Start
Search is unsuccessful
YES
NO
ThankYou!

More Related Content

PPT
Linked list introduction and different operation
PPTX
Link list(by harshit)
PPT
Lecture#1(Algorithmic Notations).ppt
DOCX
Updated Lab3.docx
PPTX
Singly & Circular Linked list
PPT
Notes fp201-pointer notes
PDF
Week 2 - Data Structures and Algorithms
PPT
The study underscores the importance of collaborative efforts and technologic...
Linked list introduction and different operation
Link list(by harshit)
Lecture#1(Algorithmic Notations).ppt
Updated Lab3.docx
Singly & Circular Linked list
Notes fp201-pointer notes
Week 2 - Data Structures and Algorithms
The study underscores the importance of collaborative efforts and technologic...

Similar to Data Structure lecture 4 linked list.pdf (20)

PPT
Pointers.ppt
PPT
Pointers.ppt
PDF
Data Structures 01
PDF
2a-Linked Listsxcxxcxxcxcxcxcxcxcxcxcxx.pdf
PPTX
Relational algebra
PDF
It110 assignment-1 answer key
PDF
SQL Pattern Matching – should I start using it?
PDF
lect 2-DS ALGO(online).pdf
PDF
Algorithm Specification and Data Abstraction
PPT
Wk11-linkedlist.ppt
PPT
Wk11-linkedlist.ppt
PPT
Linked List.ppt
PPT
Wk11-linkedlist.ppt
PPT
Linked list
PPT
Wk11-linkedlist.ppt linked list complete iit
PPT
Singly Circular Linked List – Last node points to the first node.
PPT
Chapter 5 ds
PPTX
Relational Algebra and it's Operations pptx
PPT
Data Structures with C Linked List
Pointers.ppt
Pointers.ppt
Data Structures 01
2a-Linked Listsxcxxcxxcxcxcxcxcxcxcxcxx.pdf
Relational algebra
It110 assignment-1 answer key
SQL Pattern Matching – should I start using it?
lect 2-DS ALGO(online).pdf
Algorithm Specification and Data Abstraction
Wk11-linkedlist.ppt
Wk11-linkedlist.ppt
Linked List.ppt
Wk11-linkedlist.ppt
Linked list
Wk11-linkedlist.ppt linked list complete iit
Singly Circular Linked List – Last node points to the first node.
Chapter 5 ds
Relational Algebra and it's Operations pptx
Data Structures with C Linked List
Ad

Recently uploaded (20)

PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
web development for engineering and engineering
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Digital Logic Computer Design lecture notes
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Well-logging-methods_new................
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Internet of Things (IOT) - A guide to understanding
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
web development for engineering and engineering
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Digital Logic Computer Design lecture notes
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Well-logging-methods_new................
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
R24 SURVEYING LAB MANUAL for civil enggi
OOP with Java - Java Introduction (Basics)
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Ad

Data Structure lecture 4 linked list.pdf

  • 1. Data Structures CCE 411(2013) CCE 332(2019) El-Shorouk Academy The Higher Institute of Engineering, El-Shorouk City Dr. Ahmed El-Shafei Communications and Computer Engineering Department Data Structures - Computer and Control Engineering- First Term 25 October 2024
  • 2. 1 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Notes Group (2) Group (1) Lecture # L_01_DS_24-25 2/10/2024 30/9/2024 1 L_01_DS_24-25 9/10/2024 7/10/2024 2 L_02_DS_24-25 16/10/2024 14/10/2024 3 L_03_DS_24-25 23/10/2024 21/10/2024 4 5 6 7 8 9 10 11 12 13
  • 3. 3 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Lists Lecture 4
  • 4. 4 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Lists Merging Two sorted link lists ▪ To merge two sorted link list and create a third list in the same sequence of inputted link list. Algorithm: Step 1 PTR1 = FIRST1 PTR2 = FIRST2 Step 2 PTR = AVAIL AVAIL = LINK(AVAIL) FIRST = PTR Step 3 If INFO (PTR1) < INFO (PTR2) THEN INFO (PTR) = INFO(PTR1) PTR1 = LINK (PTR1) Otherwise INFO(PTR) = INFO (PTR2) PTR2 = LINK (PTR2) Step 4 Repeat Step 5 to Step 7 while PTR1 ≠ NULLAND PTR2 ≠ NULL Step 5 CPT = AVAIL AVAIL = LINK(AVAIL) Step 6 If INFO (PTR1) < INFO (PTR2) THEN INFO (CPT) = INFO(PTR1) PTR1 = LINK (PTR1) Otherwise INFO(CPT) = INFO (PTR2) PTR2 = LINK (PTR2)
  • 5. 5 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Lists Merging Two sorted link lists Step 7 LINK (PTR) = CPT PTR = CPT Step 8 Repeat step 9 while PTR1 ≠ NULL Step 9 (i) CPT = AVAIL AVAIL = LINK (AVAIL) INFO (CPT) = INFO (PTR1) PTR1 = LINK (PTR1) (ii) LINK (PTR) = CPT PTR = CPT (iii) PTR2 = LINK (PTR1) Step 10 Repeat step 11 while PTR2 ≠ NULL Step 11 (i) CPT = AVAIL AVAIL = LINK (AVAIL) INFO (CPT) = INFO (PTR2) PTR1 = LINK (PTR1) (ii) LINK (PTR) = CPT PTR = CPT (iii) PTR2 = LINK (PTR2) Step 12 LINK (PTR) = NULL Step 13 STOP
  • 6. 6 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Lists Example: Merge the following two sorted link lists. Merge these link lists into third link lists 5 8 12 20 NULL FIRST 1 2 10 15 NULL FIRST 2 Initially, PTR1 = FIRST1 and PTR2 = FIRST2 INFO (PTR1) = 5 INFO (PTR2) = 2 Here, INFO (PTR2) < INFO (PTR1) So, create a new node PTR and INFO (PTR) = INFO (PTR2) (for the new list that will include the merge result) PTR i.e., and PTR2 = LINK (PTR2) i.e., now 2 5 8 12 20 NULL PTR 1 2 10 15 NULL PTR 2 PTR 2
  • 7. 7 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Lists INFO (PTR1) = 5 INFO (PTR2) = 10 Here, INFO (PTR2) < INFO (PTR1) So, create a new node CPT and INFO (CPT) = INFO (PTR1) LINK (PTR) = CPT PTR = CPT i.e., 2 PTR 5 and PTR1 = LINK (PTR1) Now, 5 8 12 20 NULL PTR 1 2 10 15 NULL PTR 2 2 5 PTR
  • 8. 8 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Lists INFO (PTR1) = 8 INFO (PTR2) = 10 Here, INFO (PTR1) < INFO (PTR2) So, create a new node CPT and INFO (CPT) = INFO (PTR1) PTR1 = LINK (PTR1) LINK (PTR) = CPT PTR = CPT Now link lists are: 5 8 12 20 NULL PTR 1 2 10 15 NULL PTR 2 2 5 8 PTR
  • 9. 9 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Lists INFO (PTR1) = 12 INFO (PTR2) = 10 Here, INFO (PTR2) < INFO (PTR1) So, create a new node CPT and INFO (CPT) = INFO (PTR2) PTR2 = LINK (PTR2) LINK (PTR) = CPT PTR = CPT Now link lists are: 5 8 12 20 NULL PTR 1 2 10 15 NULL PTR 2 2 5 8 PTR 10
  • 10. 10 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Lists INFO (PTR1) = 12 INFO (PTR2) = 15 Here, INFO (PTR1) < INFO (PTR2) So, create a new node CPT and INFO (CPT) = INFO (PTR1) PTR1 = LINK (PTR1) LINK (PTR) = CPT PTR = CPT Now link lists are: 5 8 12 20 NULL PTR 1 2 10 15 NULL PTR 2 2 5 8 10 PTR 12
  • 11. 11 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Lists INFO (PTR1) = 20 INFO (PTR2) = 15 Here, INFO (PTR2) < INFO (PTR1) So, create a new node CPT and INFO (CPT) = INFO (PTR2) PTR2 = LINK (PTR2) = NULL LINK (PTR) = CPT PTR = CPT Now link lists are: 5 8 12 20 NULL PTR 1 2 10 15 NULL 2 5 8 10 12 PTR 15
  • 12. 12 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Lists Here PTR1 ≠ NULL and PTR2 = NULL INFO (PTR1) = 20 So, create a new node CPT and INFO (CPT) = INFO (PTR1) PTR1 = LINK (PTR1) = NULL LINK (PTR) = CPT PTR = CPT Now , PTR1 = NULL and PTR2 = NULL So, LINK (PTR) = NULL i.e., final link-list after merging is: 2 5 8 10 12 15 20 NULL
  • 13. 13 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Lists Reversing a List ▪ To reverse a linear linked list, three pointer fields are used. These are tpt, ptr, cpt which hold the address of previous node, current node, and next node in the linear linked list. ▪ To begin with the address of the first node, which is held in pointer variable FIRST, is assigned to PTR and the tpt is assigned value NULL. ▪ Algorithm: Step 1 PTR = FIRST Step 2 TPT = NULL Step 3 Repeat step 4 while PTR ≠ NULL Step 4 (a) CPT = LINK (PTR) (b) LINK (PTR) = TPT (c) TPT = PTR (d) PTR = CPT Step 5 STOP
  • 14. 14 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Lists Example: Reverse the following link list 5 10 15 20 NULL FIRST Solution: Initially, PTR = FIRST TPT = NULL PTR ≠ NULL So, CPT = LINK (PTR) LINK (PTR) = TPT i.e., NULL and TPT = PTR PTR = CPT i.e., 5 10 15 20 NULL PTR CPT TPT PTR Now, PTR is node 10, TPT is node 5 Again, PTR ≠ NULL, So, CPT = LINK(PTR), i.e., node 15 5 10 15 20 NULL TPT NULL PTR CPT
  • 15. 15 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Lists Now, LINK(PTR) = TPT i.e., 10 5 PTR TPT NULL and TPT = PTR i.e., node 10 is now TPT PTR = CPT i.e., node 15 is now PTR Now, 10 5 TPT NULL 15 20 PTR CPT NULL
  • 16. 16 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Lists Again, PTR ≠ NULL Now, CPT = LINK(PTR), i.e., node 20 LINK (PTR) = TPT i.e., 15 10 5 NULL PTR TPT and TPT = PTR i.e., node 15 PTR = CPT i.e., node 20 Now, 15 10 5 NULL TPT 20 NULL PTR
  • 17. 17 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Lists Again, PTR ≠ NULL Now, CPT = LINK(PTR), i.e., NULL So, CPT = NULL LINK(PTR) = TPT, i.e., 15 10 5 NULL TPT 20 PTR and TPT = PTR i.e., node 20 PTR = CPT i.e., PTR = NULL Now, PTR = NULL so STOP, and the final link list is LINK(PTR) = TPT, i.e., 15 10 5 NULL 20
  • 18. 18 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 SEARCHINING
  • 19. 19 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024
  • 20. 20 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 ▪ Searching is an operation or a technique that helps finds the place of a given element or value in the list. Any search is said to be successful or unsuccessful depending upon whether the element that is being searched is found or not. ▪ Search algorithms are algorithms designed to search for or retrieve elements from a data Structures, where they are stored. They are essential to access desired elements in a data Structures and retrieve them when a need arises. ▪ A vital aspect of search algorithms is Path Finding, which is used to find paths that can be taken to traverse from one point to another, by finding the most optimum route. ▪ Some of the standard searching technique that is being followed in data Structures is listed below: ➢ Linear / Sequential Search ➢ Binary Search
  • 21. 21 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 ➢ Linear / Sequential Search Linear search is a very basic and simple search algorithm. In Linear search, we search an element or value in a given array by traversing the array from the starting, till the desired element or value is found. It compares the element to be searched with all the elements present in the array and when the element is matched successfully, it returns the index of the element in the array, else it return -1. Linear Search is applied on unsorted or unordered lists, when there are fewer elements in a list. For Example, 10 14 19 26 27 31 33 35 42 44 50 56 62 69 70 75 Features of Linear Search Algorithm 1. It is used for unsorted and unordered small list of elements. 2. It has a time complexity of O(n), which means the time is linearly dependent on the number of elements, which is not bad, but not that good too. 3. It has a very simple implementation.
  • 22. 22 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Linear / Sequential Search Algorithm Linear Search ( Array A, Value x) Step 1: Set i to 1 Step 2: if i > n then go to step 7 Step 3: if A[i] = x then go to step 6 Step 4: Set i to i + 1 Step 5: Go to Step 2 Step 6: Print Element x Found at index i and go to step 8 Step 7: Print element not found Step 8: STOP procedure linear_search (list, value) for each item in the list if match item == value return the item’s location end if end for end procedure
  • 23. 23 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Linear Search Flowchart: Sequential Search with Array Start i = 0 K = A[i]? Print "Successful" Print "Unsuccessful" i = i+1 i ≥ n Stop Yes Yes No No
  • 24. 24 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Complexity Analysis • Case 1: The key matches with the first element T(n) = 1 • Case 2: Key does not exist T(n) = n • Case 3: The key is present at any location in the array  =  = n i i i p n T 1 ) ( n p p p p n i 1 2 1 =    =    = =  = = n i i n n T 1 1 ) ( 2 1 ) ( + = n n T Linear Search
  • 25. 25 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Complexity Analysis : Summary Case Number of key comparisons Asymptotic complexity Remark Case 1 T(n) = 1 T(n) = O(1) Best case Case 2 T(n) = n T(n) = O(n) Worst case Case 3 T(n) = O(n) Average case 2 1 ) ( + = n n T Linear Search
  • 26. 26 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Binary Search
  • 27. l u mid = (l+u)/2 (a) An ordered array of elemnets with index values l, u and mid l u mid (b) Search the entire list turns into the searching of left-half only u = mid-1 Serach this half the same way if K < A[mid] l u mid l = mid+1 Serach this half the same way if K > A[mid] (c) Search the entire list turns into the searching of right-half only Binary Search Technique Data Structures - Computer and Control Engineering- First Term 25 October 2024 Data Structures ‫بيانات‬ ‫هياكل‬
  • 28. 28 Data Structures ‫بيانات‬ ‫هياكل‬ Data Structures - Computer and Control Engineering- First Term 25 October 2024 Flowchart: Binary Search with Array mid = (l+u)/2 K = A[mid]? Start Search is successful YES NO K < A[mid]? YES u = mid-1 l = mid+1 NO Stop (l>u)? Start Search is unsuccessful YES NO