1 info 3.2. chapter 3.2 recursive data structures part 1 : linear lists
DESCRIPTION
3 Info 3.2. Recursive Data Structures Implementation by variant records : Not allowed FatherMother Known Name UnKnown TYPE Person = RECORD CASE Known : BOOLEAN OF TRUE: Name : String; Father, Mother : Person | FALSE : (* empty *) END Syntax errorTRANSCRIPT
1Info 3.2.
Chapter 3.2
Recursive Data StructuresPart 1 :
Linear Lists
2Info 3.2.
A Recursive Data Structure
Example : A Pedigree
Father Mother
NameMygrandfather Mygrandmother Mygrandfather Mygrandmother
Myfather Mymother
MyselfPerson :
3Info 3.2.
Recursive Data StructuresImplementation by variant records : Not allowed
Father Mother
KnownName
UnKnown
TYPE Person = RECORD CASE Known : BOOLEAN OF TRUE: Name : String; Father, Mother : Person | FALSE : (* empty *) END END Syntax error
4Info 3.2.
Recursive Data Structures
Implementation by variant pointersFather Mother
Name TYPE Link = POINTER TO Person; Person = RECORD Name : String; Father, Mother : Link END
Father Mother
NameFather Mother
Name
Father Mother
NameFather Mother
NameFather Mother
Name
5Info 3.2.
VariablesNumber, size and address of variables is
KNOWN UNKNOWN
at compile timeSuch variables are called
STATIC DYNAMICThey are
declared in a blocknamed
created at run timeanonymous
6Info 3.2.
Simple Types
• Ordinal Types• REAL Type• POINTER Type
– Value = address– Allow indirect access to dynamic
(anonymous) variables– Used for
• recursive data structures• hidden data types in modules
7Info 3.2.
Dynamic Variables
• Created at run-time by the procedure NEW(p)• Type determined by declaration of pointer p• Anonymous• Accessed indirectly via the pointer variable p• Can be deleted at run-time by the procedure
DISPOSE(p)• Some versions of Modula 2 use
– ALLOCATE(p, SIZE(type of dynamic variable))
– DEALLOCATE (p, SIZE(type of dynamic variable))
8Info 3.2.
Pointers Syntax
POINTER Type
POINTER TO Type
Dynamic Variable
^Pointer Identifier
9Info 3.2.
Common Recursive
Data StructuresLinear Lists
Binary Trees
10Info 3.2.
Linear List Example• Specification :
– Each line of a text should be reversed
– End Of Line detected by function EOLN– End Of Text detected by function EOF
• Solution :– Read each character of a line into a linear list– Write the characters of the linear list in the
reverse order they were entered.
Thisisan example
sihTsielpmaxe na
11Info 3.2.
Linear List ExampleData Declarations
TYPE Link = POINTER TO Item; Item = RECORD Ch : CHAR; Next : Link END;
VAR First, p : Link
12Info 3.2.
Linear List ExampleLinked List Building
First
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
Thisisan example
13Info 3.2.
Linear List ExampleLinked List Building
First
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
Thisisan example
14Info 3.2.
Linear List ExampleLinked List Building
First
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
Thisisan example
15Info 3.2.
Linear List ExampleLinked List Building
First
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
Thisisan example
16Info 3.2.
Linear List ExampleLinked List Building
First
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
Thisisan example
17Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
T
Thisisan example
18Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
T
Thisisan example
19Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
T
Thisisan example
20Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
T
Thisisan example
21Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
Th
Thisisan example
22Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
Th
Thisisan example
23Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
Th
Thisisan example
24Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
Th
Thisisan example
25Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
Th
Thisisan example
i
26Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
Th
Thisisan example
i
27Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
Th
Thisisan example
i
28Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
Th
Thisisan example
i
29Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
Th
Thisisan example
is
30Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn;END;(* Line handling *)
p
First
Th
s
is
31Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn;END;(* Line handling *)
p
First
Th
s
is
32Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn;END;(* Line handling *)
p
First
Th
si
is
33Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn;END;(* Line handling *)
p
First
Th
si
is
34Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn;END;(* Line handling *)
p
First
Th
sih
is
35Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn;END;(* Line handling *)
p
First
Th
sih
is
36Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn;END;(* Line handling *)
p
First
Th
sihT
is
37Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn;END;(* Line handling *)
p
First
Th
sihT
is
38Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
Th
Thisisan example
is
39Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
Th
Thisisan example
is
40Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
Th
Thisisan example
is
41Info 3.2.
Linear List ExampleLinked List Building
WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END;
p
First
Th
Thisisan example
isi
42Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn;END;(* Line handling *)
p
First
This
43Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn;END;(* Line handling *)
p
First
Th
s
is
44Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn;END;(* Line handling *)
p
First
Th
s
is
45Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn;END;(* Line handling *)
p
First
Th
s
is
46Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn;END;(* Line handling *)
p
First
Th
s
i
47Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn;END;(* Line handling *)
p
First
Th
si
i
48Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn;END;(* Line handling *)
p
First
Th
si
i
49Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn;END;(* Line handling *)
p
First
Th
si
i
50Info 3.2.
Linear List ExampleLinked List Writing
WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn;END;(* Line handling *)
p
First
Th
si
51Info 3.2.
Linear List Operations
• Insert new item after a specific item – Example : ordered list with all key values
appearing at least once.• Insert new item before a specific item
– Example : ordered list with key values missing
• Delete specific item
52Info 3.2.
List Insertion (After)
WHILE p^.Key > q^.Key DO p := p^.NextEND;q^.Next := p^.Next;p^.Next := q
p
1
q
1234556
3
53Info 3.2.
List Insertion (After)
WHILE p^.Key > q^.Key DO p := p^.NextEND;q^.Next := p^.Next;p^.Next := q
p
11234556
3q
54Info 3.2.
List Insertion (After)
WHILE p^.Key > q^.Key DO p := p^.NextEND;q^.Next := p^.Next;p^.Next := q
p
11234556
3q
55Info 3.2.
List Insertion (After)
WHILE p^.Key > q^.Key DO p := p^.NextEND;q^.Next := p^.Next;p^.Next := q
p
11234556
3q
56Info 3.2.
List Insertion (After)
WHILE p^.Key > q^.Key DO p := p^.NextEND;q^.Next := p^.Next;p^.Next := q
p
11234556
3q
57Info 3.2.
List Insertion (Before)
WHILE(p^.Key > q^.Key)AND(p^.Next # NIL)DO p := p^.NextEND;q^.Next := p^.Next; p^.Next := q;aux := p^.Key;p^.Key := q^.Key;q^.Key := aux
p
1
q
1237889
5
58Info 3.2.
List Insertion (Before)
WHILE(p^.Key > q^.Key)AND(p^.Next # NIL)DO p := p^.NextEND;q^.Next := p^.Next; p^.Next := q;aux := p^.Key;p^.Key := q^.Key;q^.Key := aux
p
1
q
1237889
5
59Info 3.2.
List Insertion (Before)
WHILE(p^.Key > q^.Key)AND(p^.Next # NIL)DO p := p^.NextEND;q^.Next := p^.Next; p^.Next := q;aux := p^.Key;p^.Key := q^.Key;q^.Key := aux
p
1
q
1237889
5
60Info 3.2.
List Insertion (Before)
WHILE(p^.Key > q^.Key)AND(p^.Next # NIL)DO p := p^.NextEND;q^.Next := p^.Next; p^.Next := q;aux := p^.Key;p^.Key := q^.Key;q^.Key := aux
p
1
q
1237889
5
61Info 3.2.
List Insertion (Before)
WHILE(p^.Key > q^.Key)AND(p^.Next # NIL)DO p := p^.NextEND;q^.Next := p^.Next; p^.Next := q;aux := p^.Key;p^.Key := q^.Key;q^.Key := aux
p
1
q
1237889
5
62Info 3.2.
List Insertion (Before)
WHILE(p^.Key > q^.Key)AND(p^.Next # NIL)DO p := p^.NextEND;q^.Next := p^.Next; p^.Next := q;aux := p^.Key;p^.Key := q^.Key;q^.Key := aux
p
1
q
1257889
3
63Info 3.2.
Deletion from Listp
51256739
q
WHILE p^.Next # NIL DO IF p^.Key = DelKey THEN p^.Key := p^.Next^.Key; q := p^.Next; p^.Next := p^.Next^.Next; DISPOSE(q); END; (* IF *) p := p^.Next;END (* WHILE *)
64Info 3.2.
Deletion from Listp
51256739
q
WHILE p^.Next # NIL DO IF p^.Key = DelKey THEN p^.Key := p^.Next^.Key; q := p^.Next; p^.Next := p^.Next^.Next; DISPOSE(q); END; (* IF *) p := p^.Next;END (* WHILE *)
DelKey = 5
65Info 3.2.
Deletion from Listp
51256739
q
WHILE p^.Next # NIL DO IF p^.Key = DelKey THEN p^.Key := p^.Next^.Key; q := p^.Next; p^.Next := p^.Next^.Next; DISPOSE(q); END; (* IF *) p := p^.Next;END (* WHILE *)
DelKey = 5
66Info 3.2.
Deletion from Listp
51226739
q
WHILE p^.Next # NIL DO IF p^.Key = DelKey THEN p^.Key := p^.Next^.Key; q := p^.Next; p^.Next := p^.Next^.Next; DISPOSE(q); END; (* IF *) p := p^.Next;END (* WHILE *)
DelKey = 5
67Info 3.2.
Deletion from Listp
51226739
q
WHILE p^.Next # NIL DO IF p^.Key = DelKey THEN p^.Key := p^.Next^.Key; q := p^.Next; p^.Next := p^.Next^.Next; DISPOSE(q); END; (* IF *) p := p^.Next;END (* WHILE *)
DelKey = 5
68Info 3.2.
Deletion from Listp
51226739
q
WHILE p^.Next # NIL DO IF p^.Key = DelKey THEN p^.Key := p^.Next^.Key; q := p^.Next; p^.Next := q^.Next; DISPOSE(q); END; (* IF *) p := p^.Next;END (* WHILE *)
DelKey = 5
69Info 3.2.
Deletion from Listp
5126739
q
WHILE p^.Next # NIL DO IF p^.Key = DelKey THEN p^.Key := p^.Next^.Key; q := p^.Next; p^.Next := p^.Next^.Next; DISPOSE(q); END; (* IF *) p := p^.Next;END (* WHILE *)
DelKey = 5
70Info 3.2.
List Search
WHILE (p^.Key # Wanted)AND(p^.Next # NIL)DO p := p^.NextEND;
p
11237889
Search time proportional to length of list
71Info 3.2.
List Search
WHILE (p^.Key # Wanted)AND(p^.Next # NIL)DO p := p^.NextEND;
p
11237889
Search time proportional to length of list
72Info 3.2.
List Searchp
Sentinel
1237889
Sentinel^.Key := Wanted;WHILE(p^.Key # Wanted)DO p := p^.NextEND;
Search time proportional to length of list