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

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

Embed Size (px)

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 error

Citation preview

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

1Info 3.2.

Chapter 3.2

Recursive Data StructuresPart 1 :

Linear Lists

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

2Info 3.2.

A Recursive Data Structure

Example : A Pedigree

Father Mother

NameMygrandfather Mygrandmother Mygrandfather Mygrandmother

Myfather Mymother

MyselfPerson :

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

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

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

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

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

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

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

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

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

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

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

8Info 3.2.

Pointers Syntax

POINTER Type

POINTER TO Type

Dynamic Variable

^Pointer Identifier

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

9Info 3.2.

Common Recursive

Data StructuresLinear Lists

Binary Trees

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

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

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

11Info 3.2.

Linear List ExampleData Declarations

TYPE Link = POINTER TO Item; Item = RECORD Ch : CHAR; Next : Link END;

VAR First, p : Link

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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