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