SlideShare a Scribd company logo
Prog_2 course- 2014 
2 bytes team 
Kinan keshkeh 
IT Engineering-Damascus University 
3rd year
Single linked 
list 
Locomotive and trailer
Why linked list ? 
When we deal with arrays sometimes we are exposed to the problem is ((constant Reservation in memory )) 
That means the size of the array already constant and we can’t change it in program 
We can use pointer to set up data structure 
Similar array but it allows us to change their size in the program during execution 
This data structure is named ((linked list))
Linked list are divided into : 
Single linked list 
Double linked list 
Circular linked list 
In the second year
Single linked list : 
list is elements of record type one of it fields pointer on another record 
Consists of a list of elements that are arranged where each element is assigned with the following element- with pointer, as in the following example: 
Definition : 
p 
type 
type 
type 
type 
nil 
H 
d 
u 
a 
<type>; 
char 
integer 
11 
10 
22 
7 
Type ptr=^node; 
node = record ; 
Key : 
next : ptr; 
end ; 
Var p : ptr;
Create list : 
We can create a single linked list of two ways : 
From the last node to the first one(FILO) 
1 
4 multiples 
Ex : 
Write procedure to create single linked list include Multiples number (3) Limited within the domain of 1 to 14, where the list is building from the last node to the first one 
4 nodes 
numbers are { 3,6,9,12 } 
Type ptr=^node 
Node=record 
Key : integer ; 
Next : ptr ; 
End ;
Procedure create1(var p1:ptr); 
Var temp: ptr; x,i : integer; 
Begin 
for i:=1 to 4 do 
begin 
end ; 
End ; 
p1:=nil; 
x:=3; 
new ( temp ); 
temp^.key :=x; 
temp^.next := p1 ; 
p1 := temp; 
x := x+3; 
x 
i 
p1 
Nil 
next 
Nil 
temp 
12 
3 
1 
9 
p1 
3 
p1 
6 
12 
6 
3 
2 
9 
temp 
p1 
4 
temp 
temp 
p1
Create list : 
From the first node to the last one (FIFO) : 
2 
Ex : 
Write procedure to create single linked list include Multiples number (5) Limited within the domain of 1 to 20, where the list is building from the first node to the last one 
Procedure create2( var p2 :ptr ); 
Var temp:ptr; x,i : integer; 
Begin 
x :=5; 
new(p2); 
p2.key:=x; 
temp:=p2; 
For i:=2 to 4 do 
Begin 
new (temp^.next); 
temp:= temp^.next; 
x:=x+5; 
temp^.key:=x; 
end; 
temp^.next :=nil; 
End ;
Procedure create2( var p2 :ptr ); 
Var temp:ptr; x,i : integer; 
Begin 
For i:=2 to 4 do 
Begin 
temp:= temp^.next; 
x:=x+5; 
end; 
End ; 
temp^.key:=x; 
temp^.next :=nil; 
new(p2); 
x :=5; 
P2^.key:=x; 
temp:=p2; 
new (temp^.next); 
next 
20 
i 
15 
10 
x 
5 
5 
2 
p2 
nil 
3 
temp 
We create the first node befor we start the repetitive loop 
temp 
10 
20 
temp 
temp 
15 
4 
Each list must end with nil
Procedure Add element to the list : 
To add element to the list we must keep attention to three cases : 
1.The list is empty 
2.Add in the beginning of the list 
3.Add in the middle or at the end of the list 
Guide: to add a node to the list you must have two pointers , one refers to the previous node and the other refers to the following node ,from Place Addition 
9 
8 
3 
P 
5 
{general}
Ex : 
We have a list each node in it takes the following form : 
Name 
Age 
next 
Key =record 
Assuming that the list arranged by (Name) 
Write procedure to add node to the list where does not change the list arrangement 
Type 
Std=record age: integer; name: string; end; 
Ptr = ^node; 
node=record key: std; next: ptr; end; 
Let’s do it ….
Procedure addelem(var p : ptr ; vstd : std ) ; 
Var 
pre,s,temp : ptr ; 
located : Boolean ; 
Begin 
new(temp); 
temp^.key :vstd; 
temp^.next :nil; 
If p=nil then 
p:=temp 
else 
begin 
s :=p; 
pre:=nil; 
located :=false; 
While (s<>nil) and (not located) do 
Begin 
If (s^.key.name < vstd.name) then 
begin 
pre:=s; 
s:=s^.next; 
end 
else 
located:=true; 
end; 
temp^.next:=s; 
if s=p then 
p:=temp; 
else 
pre^.next:=temp; 
end ; 
End; {procedure} 
{Create node } 
{Add in the middle or at the end of the list } 
{List is empty} 
{Find the right place} 
{Add in the beginning of the list} 
{ initialize }
next 
Hussam 
p 
nil 
22 
Salma 
18 
Jad 
25 
name 
Age 
{new(temp); 
temp^.key :vstd; 
temp^.next :nil; 
Ahmed 
20 
Ahmed 
20 
here 
next 
Hussam 
p 
nil 
22 
Salma 
18 
Jad 
25 
Ahmed 
20 
{Add in the beginning of the list }
next 
Hussam 
22 
Salma 
18 
Jad 
25 
p 
Kinan 
20 
here 
next 
Hussam 
22 
Salma 
18 
Jad 
25 
p 
Kinan 
20 
{Add in the middle of the list }
Procedure delete node from the list : 
To delete node from the list we must take into account three cases : 
1.Element to be deleted does not exist 
2.Deletion in the beginning of the list 
3.Deletion in the middle or at the end of the list 
P 
Pre
Procedure deletenode(var p:Ptr; var flag:char; key:integer) 
Var temp,s :ptr; located:=Boolean; 
Begin 
If p=nil then 
flag:=‘3’ 
Else 
Begin 
if p^.key=key then 
begin 
flag:=‘1’; 
temp:=p; 
p:=p^.next; 
dispose(temp); 
end 
else 
begin 
s:=p; 
located:=false; 
While (s^.next<>nil) and (not located) do 
Begin 
if (s^.next^.key<>key) then 
s:=s^.next 
else 
located:=true; 
end; 
If located then 
begin 
flag:=‘1’; 
temp:=s^.next; 
s^.next:=s^.next^.next; 
dispose(temp); 
end 
Else flag:=‘2’; 
end; end; 
End; 
{List is empty} 
{Deletion in the beginning of the list} 
{Find the element you want to delete} 
{Do the deletion} 
{element not found} 
{initialize}
p 
S 
next 
20 
15 
10 
5 
temp 
nil 
next 
20 
10 
5 
p 
nil 
{Deletion in the middle of the list }
Homework: 
ان سًأنح 1 : نذ اٌُ سهسهح )لائحح يشتثطح ي طشف واحذ ( 
ع اُصشها يشتثح تٌض كم ع صُش سجم حٌتىي عهى ) id , 
name ) 
أكتة تش اَيجا قٌىو ت قُم انع اُصش راخ انق حًٍ id انفشد حٌ إنى 
يهف ث اُئ يع يشاعاج حزف انع صُش ان قًُىل يثاششج تعذ قَهه 
إنى ان هًف ثى طثاعح ان هًف وانسهسهح 
ان سًأنح 2 : أكتة الإجشائ اٍخ وانتىاتع انتان حٍ : 
 إجشائ حٍ نهثحث ع أول ع صُش أكثش ي n ي قائ حً ي الأعذاد ان ىًجثح يشتثح تصاعذ اٌ تح ثٍ عٌ ذٍ )- 1 ( ف حال نى 
تٌى انعثىس عهى عذد أكثش ي n 
 إجشائ حٍ نهثحث ع أخش ع صُش أصغش ي m ي قائ حً ي الأعذاد ان ىًجثح ان شًتثح تصاعذ اٌ 
 إجشائ حٍ لإضافح ع صُش ) k ( ف ان كًا ان اًُسة ف انسهسهح 
 إجشائ حٍ تقىو تتثذ مٌ ع صُش ون كٍ m,n ي انسهسهح 
ان ذًخهح 
أستخذو الإجشائ اٍخ انساتقح ف تش اَيج سئ سٍ قٌىو تإ شَاء 
سهسهح ي الأعذاد انصح حٍح ان شًتثح تصاعذ اٌ عهى انشكم 
انتان 2,4,6….192} { تتشت ةٍ ) FIFO ) 
ان سًانح 3 : أكتة تش اَيج لإ شَاء سهسهت الأونى )1P ( عذد 
ع اُصشها ) n ( ذٌخهها ان سًتخذو تتشت ةٍ ) FIFO ( وانثا حٍَ) P2 ) 
عذد ع اُصشها ) m ( ذٌخهها ان سًتخذو تتشت ةٍ ) FILO ( ثى قى 
تحزف انع اُصش انت تقثم انقس حً عهى 3 ف انسهسهح 
الأونى ثى قى تذيج انسهسهت تسهسهح واحذج تثذأ ب p2 
وت تُه ب p1 
Good luck….. 
Kinan 
Group : group link 
Mobile phone- Kinan : 0994385748 
Facebook account : kinan’s account 
2 bytes team

More Related Content

PDF
2Bytesprog2 course_2014_c7_double_lists
kinan keshkeh
 
PPTX
Programming for engineers in python
Hoang Nguyen
 
DOCX
Lisp programming
ilias ahmed
 
PPTX
Lecture 6: linked list
Vivek Bhargav
 
PDF
Constructs and techniques and their implementation in different languages
OliverYoung22
 
PPTX
CSE240 Doubly Linked Lists
Garrett Gutierrez
 
PPTX
Doubly Linked List || Operations || Algorithms
Shubham Sharma
 
PPT
Ch17
Abbott
 
2Bytesprog2 course_2014_c7_double_lists
kinan keshkeh
 
Programming for engineers in python
Hoang Nguyen
 
Lisp programming
ilias ahmed
 
Lecture 6: linked list
Vivek Bhargav
 
Constructs and techniques and their implementation in different languages
OliverYoung22
 
CSE240 Doubly Linked Lists
Garrett Gutierrez
 
Doubly Linked List || Operations || Algorithms
Shubham Sharma
 
Ch17
Abbott
 

What's hot (15)

PPTX
Link list
Syeda Javeria
 
PPT
Linked list
Ajharul Abedeen
 
PPTX
11 15 (doubly linked list)
Salim Shadman Ankur
 
PPTX
Variables 2
Jesus Obenita Jr.
 
PDF
Linked list Output tracing
Samsil Arefin
 
PPTX
Linked list
RahulGandhi110
 
PPTX
LINKED LISTS
Dhrthi Nanda
 
PPTX
Linked list
Arbind Mandal
 
PPTX
Cs419 lec5 lexical analysis using dfa
Arab Open University and Cairo University
 
PDF
Linked List Static and Dynamic Memory Allocation
Prof Ansari
 
PDF
Theta notation
Rajesh K Shukla
 
PDF
LR Parsing
Eelco Visser
 
PDF
Data structure doubly linked list programs
iCreateWorld
 
PPT
Section 1-5
chrismac47
 
Link list
Syeda Javeria
 
Linked list
Ajharul Abedeen
 
11 15 (doubly linked list)
Salim Shadman Ankur
 
Variables 2
Jesus Obenita Jr.
 
Linked list Output tracing
Samsil Arefin
 
Linked list
RahulGandhi110
 
LINKED LISTS
Dhrthi Nanda
 
Linked list
Arbind Mandal
 
Cs419 lec5 lexical analysis using dfa
Arab Open University and Cairo University
 
Linked List Static and Dynamic Memory Allocation
Prof Ansari
 
Theta notation
Rajesh K Shukla
 
LR Parsing
Eelco Visser
 
Data structure doubly linked list programs
iCreateWorld
 
Section 1-5
chrismac47
 
Ad

Viewers also liked (17)

PPTX
Ppt of operations on one way link list
Sukhdeep Kaur
 
PPTX
E learning y b-learning
Diana Osorio Paredes
 
PPTX
Single linked list
Sayantan Sur
 
PPT
Unit i
Durga Devi
 
PPSX
Data Structure (Circular Linked List)
Adam Mukharil Bachtiar
 
PPT
Unit vi(dsc++)
Durga Devi
 
PPT
Unit iii(dsc++)
Durga Devi
 
PPTX
Doubly linked list (animated)
DivyeshKumar Jagatiya
 
PPT
Unit ii(dsc++)
Durga Devi
 
PPT
header, circular and two way linked lists
student
 
PPT
Linked lists
GowriKumar Chandramouli
 
PPT
Circular linked list
dchuynh
 
PPTX
Doubly linked list
Fahd Allebdi
 
PPT
linked list
Narendra Chauhan
 
PPS
Single linked list
jasbirsingh chauhan
 
PPT
Linked lists
SARITHA REDDY
 
PPTX
Linked list
akshat360
 
Ppt of operations on one way link list
Sukhdeep Kaur
 
E learning y b-learning
Diana Osorio Paredes
 
Single linked list
Sayantan Sur
 
Unit i
Durga Devi
 
Data Structure (Circular Linked List)
Adam Mukharil Bachtiar
 
Unit vi(dsc++)
Durga Devi
 
Unit iii(dsc++)
Durga Devi
 
Doubly linked list (animated)
DivyeshKumar Jagatiya
 
Unit ii(dsc++)
Durga Devi
 
header, circular and two way linked lists
student
 
Circular linked list
dchuynh
 
Doubly linked list
Fahd Allebdi
 
linked list
Narendra Chauhan
 
Single linked list
jasbirsingh chauhan
 
Linked lists
SARITHA REDDY
 
Linked list
akshat360
 
Ad

Similar to 2Bytesprog2 course_2014_c6_single linked list (20)

PPTX
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
PDF
Unit - 2.pdf
AravindAnand21
 
PPTX
Ll.pptx
chin463670
 
PPTX
linked list.pptx
chin463670
 
PDF
Double linked list c8
Omar Al-Sabek
 
PPTX
Linked List.pptx
PoonamPatil120
 
PPTX
Linked lists a
Khuram Shahzad
 
PPTX
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
ssusere1e8b7
 
DOCX
What is Linked List in C.docx
nona800027
 
PPT
Unit7 C
arnold 7490
 
PDF
Cpp lernaufgabe linked_list
Ibrahim El-Torbany
 
PPTX
CIRCULAR LINKED LIST _
SwatiHans10
 
PPT
Sorting
Saurabh Mishra
 
PPTX
C Exam Help
Programming Exam Help
 
PDF
In an ancient land, the beautiful princess Eve had many suitors She d.pdf
ezzi552
 
PPTX
Data structure
Arvind Kumar
 
PDF
2Bytesprog2 course_2014_c5_pointers
kinan keshkeh
 
PPT
Mi 103 linked list
Amit Vats
 
PPT
Computer notes - Binary Search
ecomputernotes
 
PPT
Operations on linked list
Sumathi Kv
 
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
Unit - 2.pdf
AravindAnand21
 
Ll.pptx
chin463670
 
linked list.pptx
chin463670
 
Double linked list c8
Omar Al-Sabek
 
Linked List.pptx
PoonamPatil120
 
Linked lists a
Khuram Shahzad
 
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
ssusere1e8b7
 
What is Linked List in C.docx
nona800027
 
Unit7 C
arnold 7490
 
Cpp lernaufgabe linked_list
Ibrahim El-Torbany
 
CIRCULAR LINKED LIST _
SwatiHans10
 
C Exam Help
Programming Exam Help
 
In an ancient land, the beautiful princess Eve had many suitors She d.pdf
ezzi552
 
Data structure
Arvind Kumar
 
2Bytesprog2 course_2014_c5_pointers
kinan keshkeh
 
Mi 103 linked list
Amit Vats
 
Computer notes - Binary Search
ecomputernotes
 
Operations on linked list
Sumathi Kv
 

More from kinan keshkeh (20)

PDF
10 Little Tricks to Get Your Class’s Attention (and Hold It)
kinan keshkeh
 
PDF
Simpson and lagranje dalambair math methods
kinan keshkeh
 
PDF
Shapes and calculate (area and contour) / C++ oop concept
kinan keshkeh
 
PDF
Shapes and calculate (area and contour) / C++ oop concept
kinan keshkeh
 
PDF
GeneticAlgorithms_AND_CuttingWoodAlgorithm
kinan keshkeh
 
PDF
Algorithm in discovering and correcting words errors in a dictionary or any w...
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c9_graph
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c8_units
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c4_binaryfiles
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c3_txtfiles
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c2_records
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
PDF
2 BytesC++ course_2014_c13_ templates
kinan keshkeh
 
PDF
2 BytesC++ course_2014_c12_ polymorphism
kinan keshkeh
 
PDF
2 BytesC++ course_2014_c11_ inheritance
kinan keshkeh
 
PDF
2 BytesC++ course_2014_c10_ separate compilation and namespaces
kinan keshkeh
 
PDF
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
kinan keshkeh
 
10 Little Tricks to Get Your Class’s Attention (and Hold It)
kinan keshkeh
 
Simpson and lagranje dalambair math methods
kinan keshkeh
 
Shapes and calculate (area and contour) / C++ oop concept
kinan keshkeh
 
Shapes and calculate (area and contour) / C++ oop concept
kinan keshkeh
 
GeneticAlgorithms_AND_CuttingWoodAlgorithm
kinan keshkeh
 
Algorithm in discovering and correcting words errors in a dictionary or any w...
kinan keshkeh
 
2Bytesprog2 course_2014_c9_graph
kinan keshkeh
 
2Bytesprog2 course_2014_c8_units
kinan keshkeh
 
2Bytesprog2 course_2014_c4_binaryfiles
kinan keshkeh
 
2Bytesprog2 course_2014_c3_txtfiles
kinan keshkeh
 
2Bytesprog2 course_2014_c2_records
kinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
2 BytesC++ course_2014_c13_ templates
kinan keshkeh
 
2 BytesC++ course_2014_c12_ polymorphism
kinan keshkeh
 
2 BytesC++ course_2014_c11_ inheritance
kinan keshkeh
 
2 BytesC++ course_2014_c10_ separate compilation and namespaces
kinan keshkeh
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
kinan keshkeh
 

Recently uploaded (20)

PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
PDF
Become an Agentblazer Champion Challenge
Dele Amefo
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PPTX
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
PDF
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
PDF
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pdf
Certivo Inc
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
PPTX
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PDF
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PDF
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
Become an Agentblazer Champion Challenge
Dele Amefo
 
Activate_Methodology_Summary presentatio
annapureddyn
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pdf
Certivo Inc
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
Explanation about Structures in C language.pptx
Veeral Rathod
 

2Bytesprog2 course_2014_c6_single linked list

  • 1. Prog_2 course- 2014 2 bytes team Kinan keshkeh IT Engineering-Damascus University 3rd year
  • 2. Single linked list Locomotive and trailer
  • 3. Why linked list ? When we deal with arrays sometimes we are exposed to the problem is ((constant Reservation in memory )) That means the size of the array already constant and we can’t change it in program We can use pointer to set up data structure Similar array but it allows us to change their size in the program during execution This data structure is named ((linked list))
  • 4. Linked list are divided into : Single linked list Double linked list Circular linked list In the second year
  • 5. Single linked list : list is elements of record type one of it fields pointer on another record Consists of a list of elements that are arranged where each element is assigned with the following element- with pointer, as in the following example: Definition : p type type type type nil H d u a <type>; char integer 11 10 22 7 Type ptr=^node; node = record ; Key : next : ptr; end ; Var p : ptr;
  • 6. Create list : We can create a single linked list of two ways : From the last node to the first one(FILO) 1 4 multiples Ex : Write procedure to create single linked list include Multiples number (3) Limited within the domain of 1 to 14, where the list is building from the last node to the first one 4 nodes numbers are { 3,6,9,12 } Type ptr=^node Node=record Key : integer ; Next : ptr ; End ;
  • 7. Procedure create1(var p1:ptr); Var temp: ptr; x,i : integer; Begin for i:=1 to 4 do begin end ; End ; p1:=nil; x:=3; new ( temp ); temp^.key :=x; temp^.next := p1 ; p1 := temp; x := x+3; x i p1 Nil next Nil temp 12 3 1 9 p1 3 p1 6 12 6 3 2 9 temp p1 4 temp temp p1
  • 8. Create list : From the first node to the last one (FIFO) : 2 Ex : Write procedure to create single linked list include Multiples number (5) Limited within the domain of 1 to 20, where the list is building from the first node to the last one Procedure create2( var p2 :ptr ); Var temp:ptr; x,i : integer; Begin x :=5; new(p2); p2.key:=x; temp:=p2; For i:=2 to 4 do Begin new (temp^.next); temp:= temp^.next; x:=x+5; temp^.key:=x; end; temp^.next :=nil; End ;
  • 9. Procedure create2( var p2 :ptr ); Var temp:ptr; x,i : integer; Begin For i:=2 to 4 do Begin temp:= temp^.next; x:=x+5; end; End ; temp^.key:=x; temp^.next :=nil; new(p2); x :=5; P2^.key:=x; temp:=p2; new (temp^.next); next 20 i 15 10 x 5 5 2 p2 nil 3 temp We create the first node befor we start the repetitive loop temp 10 20 temp temp 15 4 Each list must end with nil
  • 10. Procedure Add element to the list : To add element to the list we must keep attention to three cases : 1.The list is empty 2.Add in the beginning of the list 3.Add in the middle or at the end of the list Guide: to add a node to the list you must have two pointers , one refers to the previous node and the other refers to the following node ,from Place Addition 9 8 3 P 5 {general}
  • 11. Ex : We have a list each node in it takes the following form : Name Age next Key =record Assuming that the list arranged by (Name) Write procedure to add node to the list where does not change the list arrangement Type Std=record age: integer; name: string; end; Ptr = ^node; node=record key: std; next: ptr; end; Let’s do it ….
  • 12. Procedure addelem(var p : ptr ; vstd : std ) ; Var pre,s,temp : ptr ; located : Boolean ; Begin new(temp); temp^.key :vstd; temp^.next :nil; If p=nil then p:=temp else begin s :=p; pre:=nil; located :=false; While (s<>nil) and (not located) do Begin If (s^.key.name < vstd.name) then begin pre:=s; s:=s^.next; end else located:=true; end; temp^.next:=s; if s=p then p:=temp; else pre^.next:=temp; end ; End; {procedure} {Create node } {Add in the middle or at the end of the list } {List is empty} {Find the right place} {Add in the beginning of the list} { initialize }
  • 13. next Hussam p nil 22 Salma 18 Jad 25 name Age {new(temp); temp^.key :vstd; temp^.next :nil; Ahmed 20 Ahmed 20 here next Hussam p nil 22 Salma 18 Jad 25 Ahmed 20 {Add in the beginning of the list }
  • 14. next Hussam 22 Salma 18 Jad 25 p Kinan 20 here next Hussam 22 Salma 18 Jad 25 p Kinan 20 {Add in the middle of the list }
  • 15. Procedure delete node from the list : To delete node from the list we must take into account three cases : 1.Element to be deleted does not exist 2.Deletion in the beginning of the list 3.Deletion in the middle or at the end of the list P Pre
  • 16. Procedure deletenode(var p:Ptr; var flag:char; key:integer) Var temp,s :ptr; located:=Boolean; Begin If p=nil then flag:=‘3’ Else Begin if p^.key=key then begin flag:=‘1’; temp:=p; p:=p^.next; dispose(temp); end else begin s:=p; located:=false; While (s^.next<>nil) and (not located) do Begin if (s^.next^.key<>key) then s:=s^.next else located:=true; end; If located then begin flag:=‘1’; temp:=s^.next; s^.next:=s^.next^.next; dispose(temp); end Else flag:=‘2’; end; end; End; {List is empty} {Deletion in the beginning of the list} {Find the element you want to delete} {Do the deletion} {element not found} {initialize}
  • 17. p S next 20 15 10 5 temp nil next 20 10 5 p nil {Deletion in the middle of the list }
  • 18. Homework: ان سًأنح 1 : نذ اٌُ سهسهح )لائحح يشتثطح ي طشف واحذ ( ع اُصشها يشتثح تٌض كم ع صُش سجم حٌتىي عهى ) id , name ) أكتة تش اَيجا قٌىو ت قُم انع اُصش راخ انق حًٍ id انفشد حٌ إنى يهف ث اُئ يع يشاعاج حزف انع صُش ان قًُىل يثاششج تعذ قَهه إنى ان هًف ثى طثاعح ان هًف وانسهسهح ان سًأنح 2 : أكتة الإجشائ اٍخ وانتىاتع انتان حٍ :  إجشائ حٍ نهثحث ع أول ع صُش أكثش ي n ي قائ حً ي الأعذاد ان ىًجثح يشتثح تصاعذ اٌ تح ثٍ عٌ ذٍ )- 1 ( ف حال نى تٌى انعثىس عهى عذد أكثش ي n  إجشائ حٍ نهثحث ع أخش ع صُش أصغش ي m ي قائ حً ي الأعذاد ان ىًجثح ان شًتثح تصاعذ اٌ  إجشائ حٍ لإضافح ع صُش ) k ( ف ان كًا ان اًُسة ف انسهسهح  إجشائ حٍ تقىو تتثذ مٌ ع صُش ون كٍ m,n ي انسهسهح ان ذًخهح أستخذو الإجشائ اٍخ انساتقح ف تش اَيج سئ سٍ قٌىو تإ شَاء سهسهح ي الأعذاد انصح حٍح ان شًتثح تصاعذ اٌ عهى انشكم انتان 2,4,6….192} { تتشت ةٍ ) FIFO ) ان سًانح 3 : أكتة تش اَيج لإ شَاء سهسهت الأونى )1P ( عذد ع اُصشها ) n ( ذٌخهها ان سًتخذو تتشت ةٍ ) FIFO ( وانثا حٍَ) P2 ) عذد ع اُصشها ) m ( ذٌخهها ان سًتخذو تتشت ةٍ ) FILO ( ثى قى تحزف انع اُصش انت تقثم انقس حً عهى 3 ف انسهسهح الأونى ثى قى تذيج انسهسهت تسهسهح واحذج تثذأ ب p2 وت تُه ب p1 Good luck….. Kinan 
  • 19. Group : group link Mobile phone- Kinan : 0994385748 Facebook account : kinan’s account 2 bytes team