Pascal Sets and Pointers

Sets
A Pascal set is a collection of elements, it is formed by combining members of an ordinal datatype or subrange of ordinal datatype.

Sets can be declared through type declaration, or variable declaration e.g. Type identifier = set of ordinal datatype;

var identifier : set of ordinal datatype; To create a set of numeric digits from 0 to 25, the following declaration may be used. Type num0to25 = set of 0 .. 25;      vowels = set of ('A', 'E', 'I', 'O', 'U');

Set operators
+ Union       - Combines sets without repeating duplicate values. - Difference  - Removes values in second set from first set leaving non matching values. * Intersection - Returns common elements that occur in both sets.

Pointers
A pointer is a variable that stores the address of another variable. For the variables declared in the var declarations part, each havas a unique addresses in the computer memory (the variables adress can be stored in the pointer). Pointers are used in building complex data structures like ques, stacks asnd binary trees. The data structures created using pointers can easily increase and decrease in size (their muximum size need not be declared). A pointer variable is capable of having the address of a dynamic variable. The memory size of a dynamic variable is allocated during execution time.

Pointer variable declaration
The pointer variable needs to be declared before its use. The caret /circumflex (^) is used in pointer declaration.

Syntax: var identifiername  :	^datatype; Example: var ptrpointer  :     ^integer; In the above case, ptrpointer is a pointer to an integer.

Singly linked list
In singly linked lists, each node has at least one data field and address field. The address field (link field) contains the address of the next node in the structure. The Head pointer points to the first node in the linked list and the link field of the last node has a NIL in it, this is to signify that it points to none. You can be able to access any node provided you know the location of the first node. Wherever head = NIL, then the list is empty.

Example:



Adding data:



Concept of linked list using pointers
Linked lists are important in situations where we dont know the amount of memory required by the linear array in advance.

Example: (*Program to insert a record, delete a record and display all the records*) Type syear  =   ^stdrec; stdrec = record stdname          :      string; stdexamno      :      integer; link                     :       syear; end; var markrec    :    syear; procedure inserting( var markrec  : syear); var  front, rear, next:   syear; present   :   boolean; begin new(front); write('Enter Examno   :'); readln(front^.stdexamno); write(' Name     :'); readln(front^.stdname); rear  :=  nil; next  :=  markrec; present  :=  false; while (next <> nil) and not present do                    if (next^.stdexamno >front^.stdexamno) then present := true else begin rear   :=   next; next  :=   next^.link; end; front^.link :=   next; if rear =  nil  then markrec :=  front else rear^.link  :=  front; end;

procedure deletion(var markrec  :  syear); var rear, next   :  syear; tempoexamnum   :  integer; present  :   boolean; begin write ('Enter Examno to Delete'); readln(tempoexamnum); if (markec^.stdexamno  =  tempoexamnum) then markrec :=   markrec^.link else begin rear  :=   markrec; next :=  markrec^.link; present :=  false; while (next <> nil)  and not present do begin if (next^.stdexamno = tempoexamnum) then begin present := true; rear^.link :=  next^.link; end else begin rear :=  next; next :=  next^.link; end; end; end; dispose(markrec); end; procedure displaying (markrec : syear); begin writeln; while (markrec <> nil) do             begin writeln('Examno: ', markrec^.stdexamno); writeln(' Name:  ',markrec^.stdname); writeln; markrec :=  markrec^.link; end; end; begin initializing(markrec); deleting(markrec); displaying(markrec); end.