Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Info 3.2. Chapter 3.2 Recursive Data Structures Part 1 : Linear Lists."— Presentation transcript:

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

2 2 Info 3.2. A Recursive Data Structure Example : A Pedigree FatherMother Name MygrandfatherMygrandmotherMygrandfatherMygrandmother MyfatherMymother Myself Person :

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

4 4 Info 3.2. Recursive Data Structures Implementation by variant pointers FatherMother Name TYPE Link = POINTER TO Person; Person = RECORD Name : String; Father, Mother : Link END FatherMother Name FatherMother Name FatherMother Name FatherMother Name FatherMother Name

5 5 Info 3.2. Variables Number, size and address of variables is KNOWNUNKNOWN at compile time Such variables are called STATICDYNAMIC They are declared in a block named created at run time anonymous

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

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

8 8 Info 3.2. Pointers Syntax POINTER Type POINTERTO Type Dynamic Variable ^ Pointer Identifier

9 9 Info 3.2. Common Recursive Data Structures Linear Lists Binary Trees

10 10 Info 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. This is an example sihT si elpmaxe na

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

12 12 Info 3.2. Linear List Example Linked 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 This is an example

13 13 Info 3.2. Linear List Example Linked 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 This is an example

14 14 Info 3.2. Linear List Example Linked 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 This is an example

15 15 Info 3.2. Linear List Example Linked 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 This is an example

16 16 Info 3.2. Linear List Example Linked 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 This is an example

17 17 Info 3.2. Linear List Example Linked 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 This is an example

18 18 Info 3.2. Linear List Example Linked 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 This is an example

19 19 Info 3.2. Linear List Example Linked 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 This is an example

20 20 Info 3.2. Linear List Example Linked 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 This is an example

21 21 Info 3.2. Linear List Example Linked 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 h This is an example

22 22 Info 3.2. Linear List Example Linked 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 h This is an example

23 23 Info 3.2. Linear List Example Linked 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 h This is an example

24 24 Info 3.2. Linear List Example Linked 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 h This is an example

25 25 Info 3.2. Linear List Example Linked 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 h This is an example i

26 26 Info 3.2. Linear List Example Linked 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 h This is an example i

27 27 Info 3.2. Linear List Example Linked 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 h This is an example i

28 28 Info 3.2. Linear List Example Linked 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 h This is an example i

29 29 Info 3.2. Linear List Example Linked 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 h This is an example is

30 30 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling *) p First T h s is

31 31 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling *) p First T h s is

32 32 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling *) p First T h si is

33 33 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling *) p First T h si is

34 34 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling *) p First T h sih is

35 35 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling *) p First T h sih is

36 36 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling *) p First T h sihT is

37 37 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); First:= First^.Next; END; WrLn; END;(* Line handling *) p First T h sihT is

38 38 Info 3.2. Linear List Example Linked 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 h This is an example is

39 39 Info 3.2. Linear List Example Linked 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 h This is an example is

40 40 Info 3.2. Linear List Example Linked 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 h This is an example is

41 41 Info 3.2. Linear List Example Linked 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 h This is an example isi

42 42 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) p First T his

43 43 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) p First T h s is

44 44 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) p First T h s is

45 45 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) p First T h s is

46 46 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) p First T h s i

47 47 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) p First T h si i

48 48 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) p First T h si i

49 49 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) p First T h si i

50 50 Info 3.2. Linear List Example Linked List Writing WHILE First # NIL DO WrChar(First^.Ch); p := First; First:= First^Next; DISPOSE(p) END; WrLn; END;(* Line handling *) p First T h si

51 51 Info 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

52 52 Info 3.2. List Insertion (After) WHILE p^.Key > q^.Key DO p := p^.Next END; q^.Next := p^.Next; p^.Next := q p 1 q 12 34556 3

53 53 Info 3.2. List Insertion (After) WHILE p^.Key > q^.Key DO p := p^.Next END; q^.Next := p^.Next; p^.Next := q p 1 12 34556 3 q

54 54 Info 3.2. List Insertion (After) WHILE p^.Key > q^.Key DO p := p^.Next END; q^.Next := p^.Next; p^.Next := q p 1 12 34556 3 q

55 55 Info 3.2. List Insertion (After) WHILE p^.Key > q^.Key DO p := p^.Next END; q^.Next := p^.Next; p^.Next := q p 1 12 34556 3 q

56 56 Info 3.2. List Insertion (After) WHILE p^.Key > q^.Key DO p := p^.Next END; q^.Next := p^.Next; p^.Next := q p 1 123 4556 3 q

57 57 Info 3.2. List Insertion (Before) WHILE(p^.Key > q^.Key)AND(p^.Next # NIL)DO p := p^.Next END; q^.Next := p^.Next; p^.Next := q; aux := p^.Key; p^.Key := q^.Key; q^.Key := aux p 1 q 12 37889 5

58 58 Info 3.2. List Insertion (Before) WHILE(p^.Key > q^.Key)AND(p^.Next # NIL)DO p := p^.Next END; q^.Next := p^.Next; p^.Next := q; aux := p^.Key; p^.Key := q^.Key; q^.Key := aux p 1 q 12 37889 5

59 59 Info 3.2. List Insertion (Before) WHILE(p^.Key > q^.Key)AND(p^.Next # NIL)DO p := p^.Next END; q^.Next := p^.Next; p^.Next := q; aux := p^.Key; p^.Key := q^.Key; q^.Key := aux p 1 q 12 37889 5

60 60 Info 3.2. List Insertion (Before) WHILE(p^.Key > q^.Key)AND(p^.Next # NIL)DO p := p^.Next END; q^.Next := p^.Next; p^.Next := q; aux := p^.Key; p^.Key := q^.Key; q^.Key := aux p 1 q 12 37889 5

61 61 Info 3.2. List Insertion (Before) WHILE(p^.Key > q^.Key)AND(p^.Next # NIL)DO p := p^.Next END; q^.Next := p^.Next; p^.Next := q; aux := p^.Key; p^.Key := q^.Key; q^.Key := aux p 1 q 123 7889 5

62 62 Info 3.2. List Insertion (Before) WHILE(p^.Key > q^.Key)AND(p^.Next # NIL)DO p := p^.Next END; q^.Next := p^.Next; p^.Next := q; aux := p^.Key; p^.Key := q^.Key; q^.Key := aux p 1 q 125 7889 3

63 63 Info 3.2. Deletion from List p 5 12 56739 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 *)

64 64 Info 3.2. Deletion from List p 5 12 56739 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

65 65 Info 3.2. Deletion from List p 5 12 56739 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

66 66 Info 3.2. Deletion from List p 5 12 26739 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

67 67 Info 3.2. Deletion from List p 5 12 26739 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

68 68 Info 3.2. Deletion from List p 5 122 6739 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

69 69 Info 3.2. Deletion from List p 5 12 6739 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

70 70 Info 3.2. List Search WHILE (p^.Key # Wanted)AND(p^.Next # NIL) DO p := p^.Next END; p 1 12 37889 Search time proportional to length of list

71 71 Info 3.2. List Search WHILE (p^.Key # Wanted)AND(p^.Next # NIL) DO p := p^.Next END; p 1 12 37889 Search time proportional to length of list

72 72 Info 3.2. List Search p Sentinel 12 37889 Sentinel^.Key := Wanted; WHILE(p^.Key # Wanted)DO p := p^.Next END; Search time proportional to length of list


Download ppt "1 Info 3.2. Chapter 3.2 Recursive Data Structures Part 1 : Linear Lists."

Similar presentations


Ads by Google