110 likes | 352 Views
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. . Pascal Programming .
E N D
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.
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.
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.
Pascal Programming • Pointer variables . . . • . . . are declared • . . . have a type. • . . . have an associated identifier. • . . . point to a dynamic variable. • Syntax • var • Pointer: ^(type); continues . . .
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.
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.
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
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.
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.
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