Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pascal Programming Pointers and Dynamic Variables.

Similar presentations


Presentation on theme: "Pascal Programming Pointers and Dynamic Variables."— Presentation transcript:

1 Pascal Programming Pointers and Dynamic Variables

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

3 Pascal Programming n An assignment operator or read statement can establish the value of a dynamic variable. n A dynamic variable can be accessed by a write statement, by being an actual parameter or any other usual means.

4 Pascal Programming n Dynamic variables differ from ordinary variables in two ways... n 1. Dynamic variables may be created and destroyed by the program. n 2. Dynamic variables have no names in Pascal. n To refer to a dynamic variable Pascal uses a pointer variable.

5 Pascal Programming n Pointer variables... n... are declared n... have a type. n... have an associated identifier. n... point to a dynamic variable. n Syntax –var Pointer: ^(type); continues...

6 Pascal Programming n The type associated with the pointer is the domain type. n The pointer can only point to that type... another type requires an additional pointer or a redirection of the pointer. n The predefined procedure new can be used to point to a new dynamic variable.

7 Pascal Programming n Pascal has a working phrase—the thing pointed to by Pointer... n...this translates into pointer ^. n Pointer=P n The value of P points to a dynamic variable— not P points to... n The assignment operator can redirect the pointer – P:=P1 n After redirection, you need to check to see that P1^ points to the appropriate type.

8 Pascal Programming n Remember Nodes and Pointers need to be declared. n Pascal wants pointer type definition to precede node type definition. n nil, is a constant like the Boolean true or false. n It can be used as an end marker. –P^.LastNode:=nil

9 Pascal Programming n Linked lists... n... consist of nodes with one pointer field. n... the pointers order the nodes into a list. n... the first node is called the head. n... the pointer would be called head because it points to the head of the linked list. n... nodes can be added at the head, the end or at a predetermined location.

10 Pascal Programming n Binary trees... n...have two pointers. n...programs can address every node of a tree by traversing it. n... Branching from the first node leads us to a right subtree and a left subtree. n Traversing can be accomplished by addressing the left then the right subtree, returning to the root node in the middle.

11 Pascal Programming n Linked lists may be used for many of the same uses as arrays. n However, a program can alter the size of a linked list (not an array)... n...nodes can be inserted and deleted. n These differences give linked lists new utility. n An empty list can be created by... –Head := nil


Download ppt "Pascal Programming Pointers and Dynamic Variables."

Similar presentations


Ads by Google