double linked list c8

15
Pascal Programming Language Omar ElSabek & Fayez G hazzawi IT Engineering 3 th year UNKNOWN Department programming II

Upload: omar-al-sabek

Post on 14-Apr-2017

135 views

Category:

Education


1 download

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 :

It has three 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.

3. The References which refers to the previous element in the Linked ListThis Reference has the value of (nil) in the first element in the Linked List.

Program test1;

Type

D_P = ^D_R;

D_R = record

num : integer;

next,prev : D_P;

end;

Now let’s meet with some procedures which are used in DLL

It has three cases:

1. Insert an element in the first of DLL

2. Insert an element in the last of DLL

3. Insert an element anywhere else

Procedure Insert (var L_S,L_E: D_P; numb: integer)

Var

S,temp : D_P; located : boolean

Begin

new(temp);

temp^.num := numb;

temp^.next := nil;

temp^.prev := nil;

if (L_S = nil) then

begin

L_S := temp; L_E := temp;

end

else

begin

S := L_S; located := false;

while (S <> nil) and (not located) do

begin

if (S^.num < numb) then

S := S^.next;

else

located := true;

end;

temp^.next := S;

if (L_S = S) then

begin

L_S^.prev := temp;

L_S := temp;

end

else if (S = nil) then

begin

L_E^.next := temp;

temp^.prev := L_E;

L_E := temp;

end

else

begin

S^.prev^.next := temp;

temp^.prev := S^.prev;

S^.prev := temp;

end;

end;

End;

It has three cases:

1. Delete the first element

2. Delete the last element

3. Delete any other element

Procedure Delete(var L_S,L_E: D_P; numb: integer;

flag: char)

Var

S,temp : D_P;

Begin

if (L_S = nil) then flag := ‘0’;

if (numb = L_S^.num) then

begin

flag := ‘1’;

temp := L_S;

L_S := L_S^.next;

L_S^.prev := nil;

dispose(temp);

end

else if (numb = L_E^.num) then

begin

flag := ‘1’;

temp := L_E;

L_E := L_E^.prev;

L_E^.next := nil;

dispose(temp);

end

else

begin

S := L_S;

while (S <> nil) and (S^.num <> numb) do

S := S^.next

if (S = nil) then flag := ‘2’

else

begin

flag := ‘1’;

S^.next^.prev := S^.prev;

S^.prev^.next := S^.next;

dispose(S);

end;

end;

End;