linked lists c7

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

Upload: omar-al-sabek

Post on 16-Apr-2017

105 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Linked lists c7

Pascal Programming Language

Omar ElSabek & Fayez GhazzawiIT Engineering3th year – UNKNOWN Department

programming II

Page 2: Linked lists c7

Sets

Record

Files (Text & Binary)

Pointers

Linked Lists

Unit

Course Index :

Page 3: Linked lists c7
Page 4: Linked lists c7

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

Page 5: Linked lists c7

There are two forms for Linked Lists:

1. Single Linked Lists “SLL”

2. Double Linked Lists “DLL”

Page 6: Linked lists c7

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

Page 7: Linked lists c7

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

Page 8: Linked lists c7

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

Page 9: Linked lists c7

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

Page 10: Linked lists c7

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;

Page 11: Linked lists c7

temp^.next := S;

if (L_S = S) then

L_S := temp

else

P^.next := temp;

end;

End.

Page 12: Linked lists c7

It has two cases:

1. Delete the first element

2. Delete any other element

Page 13: Linked lists c7

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

Page 14: Linked lists c7

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;

Page 15: Linked lists c7

if (not located) then

flag := ‘2’

else

begin

flag := ‘1’;

temp := S^.next;

S^.next := temp^.next;

dispose(temp);

end;

end;

End.

Page 16: Linked lists c7