linked lists c7
TRANSCRIPT
Pascal Programming Language
Omar ElSabek & Fayez GhazzawiIT Engineering3th year – UNKNOWN Department
programming II
Sets
Record
Files (Text & Binary)
Pointers
Linked Lists
Unit
Course Index :
So the Linked List is:
Juuust like an array with unlimited elements ,because all the elements are reserved inside the memory in “Run Time”So the Linked Lists were born so there is no constant size for any arrayWe can say that the Linked List is an array with inconstant size
There are two forms for Linked Lists:
1. Single Linked Lists “SLL”
2. Double Linked Lists “DLL”
It has two elements:
1. The Variable(s) with Data type(s) “the same with all the Linked List elements”
2. The References which refers to the next element in the Linked ListThis Reference has the value of (nil) in the last element in the Linked List
Program test1;
Type
L_P = ^L_R;
L_R = record
num : integer;
next : L_P;
end;
Now let’s meet with some procedures which are used in SLL
It has three cases:
1. Insert an element in the first of SLL
2. Insert an element in the last of SLL
3. Insert an element anywhere else
Procedure Insert (var L_S: L_P; numb: integer)
Var
P,S,temp : L_P; located : boolean
Begin
new(temp);
temp^.num := numb;
temp^.next := nil;
if (L_S = nil) then
L_S := temp
else
begin
S := L_S;
located := false;
while (S <> nil) and (not located) do
begin
if (S^.num < numb) then
begin
P := S;
S := S^.next;
end
else
located := true;
end;
temp^.next := S;
if (L_S = S) then
L_S := temp
else
P^.next := temp;
end;
End.
It has two cases:
1. Delete the first element
2. Delete any other element
Procedure Delete(var L_S: L_P; numb: integer;
flag: char)
Var
S,temp : L_P; located : boolean
Begin
if (numb = L_S^.num) then
begin
flag := ‘1’;
temp := L_S;
L_S := L_S^.next;
dispose(temp);
end
else
begin
S := L_S;
located := false;
while (S^.next <> nil) and (not located) do
begin
if (S^.next^.num <> numb) then
S := S^.next
else
located := true;
end;
if (not located) then
flag := ‘2’
else
begin
flag := ‘1’;
temp := S^.next;
S^.next := temp^.next;
dispose(temp);
end;
end;
End.