720 likes | 734 Views
This chapter explores the implementation of recursive data structures, specifically focusing on linear lists. It covers the concept of dynamic variables, anonymous types, and the usage of pointers in Modula 2. An example of building a linked list and reversing a text line is provided.
E N D
Chapter 3.2 Recursive Data Structures Part 1 : Linear Lists
Name Father Mother A Recursive Data StructureExample : A Pedigree Person : Myself Myfather Mymother Mygrandfather Mygrandmother Mygrandfather Mygrandmother
Father Mother Recursive Data StructuresImplementation by variant records : Not allowed Known UnKnown Name TYPEPerson = RECORD CASEKnown : BOOLEANOF TRUE: Name : String; Father, Mother : Person | FALSE : (* empty *) END END Syntax error
Name Name Name Name Name Name Father Father Father Father Father Father Mother Mother Mother Mother Mother Mother Recursive Data StructuresImplementation by variant pointers TYPE Link = POINTER TO Person; Person = RECORD Name : String; Father, Mother : Link END
Variables Number, size and address of variables is KNOWN UNKNOWN at compile time Such variables are called STATIC DYNAMIC They are declared in a block named created at run time anonymous
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
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))
Type POINTER TO Pointer Identifier ^ Pointers Syntax POINTER Type Dynamic Variable
Common RecursiveData Structures Linear Lists Binary Trees
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. This is an example sihT si elpmaxe na
Linear List ExampleData Declarations TYPE Link = POINTER TO Item; Item = RECORD Ch : CHAR; Next : Link END; VAR First, p : Link
Linear List ExampleLinked List Building First p WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
First Linear List ExampleLinked List Building p WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
First Linear List ExampleLinked List Building p WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch:=RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T h WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T h WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T h WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T h WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T i h WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T i h WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T i h WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T i h WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T s i h WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Writing First p T s i h WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling *) s
Linear List ExampleLinked List Writing First p T s i h WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling*) s
Linear List ExampleLinked List Writing First p T s i h WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling*) si
Linear List ExampleLinked List Writing First p T s i h WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling*) si
Linear List ExampleLinked List Writing First p T s i h WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling*) sih
Linear List ExampleLinked List Writing First p T s i h WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling*) sih
Linear List ExampleLinked List Writing First p T s i h WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling*) sihT
Linear List ExampleLinked List Writing First p T s i h WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling*) sihT
Linear List ExampleLinked List Building First p T s i h WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T s i h WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T s i h WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Building First p T i s i h WHILE NOT EOF DO First := NIL; WHILE NOT EOLN DO NEW(p); p^.Next := First; First := p; p^.Ch := RdChar(); END; This is an example
Linear List ExampleLinked List Writing First p T s i h WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *)
Linear List ExampleLinked List Writing First p T s i h WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) s
Linear List ExampleLinked List Writing First p T s i h WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) s
Linear List ExampleLinked List Writing First p T s i h WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) s
Linear List ExampleLinked List Writing First p T i h WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) s
Linear List ExampleLinked List Writing First p T i h WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) si
Linear List ExampleLinked List Writing First p T i h WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) si
Linear List ExampleLinked List Writing First p T i h WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) si
Linear List ExampleLinked List Writing First p T h WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) si