2bytesprog2 course_2014_c6_single linked list
TRANSCRIPT
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
ne
xt
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}
Homework: ( الئحح يشتثطح ي طشف واحذ )نذا سهسهح : 1انسأنح
, id)عاصشها يشتثح تض كم عصش سجم حتىي عهى name )
انفشدح إنى idأكتة تشايجا قىو تقم انعاصش راخ انقح يهف ثائ يع يشاعاج حزف انعصش انقىل يثاششج تعذ قهه
إنى انهف ثى طثاعح انهف وانسهسهح
:أكتة اإلجشائاخ وانتىاتع انتانح : 2انسأنح
إجشائح نهثحث ع أول عصش أكثش يn ي قائح يف حال نى ( 1-)األعذاد انىجثح يشتثح تصاعذا تحث عذ
nتى انعثىس عهى عذد أكثش ي
إجشائح نهثحث ع أخش عصش أصغش يm ي قائح ي األعذاد انىجثح انشتثح تصاعذا
إجشائح إلضافح عصش(k ) ف انكا اناسة ف انسهسهح
إجشائح تقىو تتثذم عصش ونكm,n ي انسهسهح انذخهح
أستخذو اإلجشائاخ انساتقح ف تشايج سئس قىو تإشاء سهسهح ي األعذاد انصححح انشتثح تصاعذا عهى انشكم
( FIFO)تتشتة } {192.…2,4,6انتان
عذد ( 1P(أكتة تشايج إلشاء سهسهت األونى: 3انسانح ( P2)وانثاح( FIFO)ذخهها انستخذو تتشتة ( n)عاصشها
ثى قى ( FILO (ذخهها انستخذو تتشتة ( m)عذد عاصشها ف انسهسهح 3تحزف انعاصش انت تقثم انقسح عهى
p2األونى ثى قى تذيج انسهسهت تسهسهح واحذج تثذأ ب p1وتته ب
Good luck…..
Kinan
Group : group link
Mobile phone- Kinan : 0994385748
Facebook account : kinan’s account
2 bytes team