1 / 72

Recursive Data Structures Implementation: Linear Lists

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.

mrosas
Download Presentation

Recursive Data Structures Implementation: Linear Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3.2 Recursive Data Structures Part 1 : Linear Lists

  2. Name Father Mother A Recursive Data StructureExample : A Pedigree Person : Myself Myfather Mymother Mygrandfather Mygrandmother Mygrandfather Mygrandmother

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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))

  8. Type POINTER TO Pointer Identifier ^ Pointers Syntax POINTER Type Dynamic Variable

  9. Common RecursiveData Structures Linear Lists Binary Trees

  10. 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

  11. Linear List ExampleData Declarations TYPE Link = POINTER TO Item; Item = RECORD Ch : CHAR; Next : Link END; VAR First, p : Link

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. 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 *)

  43. 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

  44. 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

  45. 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

  46. 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

  47. 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

  48. 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

  49. 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

  50. 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

More Related