Pascal Programming Pointers and Dynamic Variables.

11
Pascal Programming Pointers and Dynamic Variables

Transcript of Pascal Programming Pointers and Dynamic Variables.

Page 1: Pascal Programming Pointers and Dynamic Variables.

Pascal Programming

Pointers and Dynamic Variables

Page 2: Pascal Programming Pointers and Dynamic Variables.

Pascal Programming

The pointer . . . . . . addresses a location in a list. The pointer points to a node. Dynamic variables . . . . . . have an associated type. . . . almost any type maybe used but a

file type.

Page 3: Pascal Programming Pointers and Dynamic Variables.

Pascal Programming

An assignment operator or read statement can establish the value of a dynamic variable.

A dynamic variable can be accessed by a write statement, by being an actual parameter or any other usual means.

Page 4: Pascal Programming Pointers and Dynamic Variables.

Pascal Programming

Dynamic variables differ from ordinary variables in two ways. . .

1. Dynamic variables may be created and destroyed by the program.

2. Dynamic variables have no names in Pascal.

To refer to a dynamic variable Pascal uses a pointer variable.

Page 5: Pascal Programming Pointers and Dynamic Variables.

Pascal Programming

Pointer variables . . . . . . are declared . . . have a type. . . . have an associated identifier. . . . point to a dynamic variable. Syntax

– var• Pointer: ^(type); continues . . .

Page 6: Pascal Programming Pointers and Dynamic Variables.

Pascal Programming

The type associated with the pointer is the domain type.

The pointer can only point to that type . . . another type requires an additional pointer or a redirection of the pointer.

The predefined procedure new can be used to point to a new dynamic variable.

Page 7: Pascal Programming Pointers and Dynamic Variables.

Pascal Programming

Pascal has a working phrase—the thing pointed to by Pointer . . .

. . .this translates into pointer ^. Pointer=P The value of P points to a dynamic variable—

not P points to . . . The assignment operator can redirect the

pointer – P:=P1 After redirection, you need to check to see that

P1^ points to the appropriate type.

Page 8: Pascal Programming Pointers and Dynamic Variables.

Pascal Programming

Remember Nodes and Pointers need to be declared.

Pascal wants pointer type definition to precede node type definition.

nil, is a constant like the Boolean true or false.

It can be used as an end marker.– P^.LastNode:=nil

Page 9: Pascal Programming Pointers and Dynamic Variables.

Pascal Programming

Linked lists . . . . . . consist of nodes with one pointer field. . . . the pointers order the nodes into a list. . . . the first node is called the head. . . . the pointer would be called head because

it points to the head of the linked list. . . . nodes can be added at the head, the end

or at a predetermined location.

Page 10: Pascal Programming Pointers and Dynamic Variables.

Pascal Programming

Binary trees . . . . . .have two pointers. . . .programs can address every node of a

tree by traversing it. . . . Branching from the first node leads us to

a right subtree and a left subtree. Traversing can be accomplished by

addressing the left then the right subtree, returning to the root node in the middle.

Page 11: Pascal Programming Pointers and Dynamic Variables.

Pascal Programming

Linked lists may be used for many of the same uses as arrays.

However, a program can alter the size of a linked list (not an array). . .

. . .nodes can be inserted and deleted. These differences give linked lists new utility. An empty list can be created by . . .

– Head := nil