Joke Collection Website - Joke collection - pascal problem

pascal problem

囧: Coaches don’t practice as much as students. If you dare to let your students open such an array in the semi-finals, I really admire you = =

Your array range design It's too big, especially the definition of this array: c:array reuses the array. This method is suitable for situations where you no longer need the previous array data. The third method is to use a pointer linked list. For specific methods, you can refer to the following:

Pointer type

11.1 Pointer

11.2 Single linked list

11.1 Pointer

Pointer is a special data type that accesses variables through addresses. It is a dynamic data structure. It can be generated when needed and can be canceled or recycled after use to reduce occupancy. memory space. A pointer variable is different from other types of variables in that it does not occupy data, but an address.

Since the variables of the dynamic data structure are dynamically generated during the execution of the program, they cannot be explained in advance. These variables cannot be given names in advance. They cannot be directly output or displayed by name during access, but only You can use a pointer to get its address and then access it indirectly.

1. Define pointer type

In Turbo Pascal, pointer variables are used to store the address of a certain storage unit, that is, the pointer variable points to a certain storage unit. A pointer variable can only point to a certain type of storage unit. This data type is determined in the definition of the pointer type and is called the base type of the pointer type. The pointer type is defined as follows:

Type name = ^base type name;

For example: type q=^integer;

var a,b,c:q ;

Explanation: q is a pointer type pointing to an integer storage unit, where "^" is the pointer character. a, b, and c are all defined as pointer variables, which can point to an integer storage unit respectively.

The above example can also be described as a variable:

var a,b,c:^integer;

The pointer can also point to a structured storage unit.

For example: type person=record

name:string[10];

sex:(male,female);

age :20..70

end;

var pt:^person;

pt is a pointer variable pointing to the record type person.

2. Dynamic variables

The form of the dynamic storage unit pointed by a pointer, that is, the dynamic variable is as follows:

Pointer variable name^

For example: p^, q^, r^

The pointer variable p and the dynamic variable p^ it points to have the following relationship:

The following statement stores the integer 5 in into the dynamic variable p^ pointed to by p:

p^:=5;

The following statement assigns the value in p^ pointed to by p to the integer variable i:

i:=p^;

If the pointer variable p does not point to any storage unit, the following assignment statement can be used:

p:=nil; < /p>

Nil is a reserved word, which means "empty", which is equivalent to null in C language.

3. Operation of dynamic variables

In the Turob Pascal program , dynamic variables cannot be directly defined by var but are created by calling the standard procedure new. The procedure form is:

new (pointer variable name);

If there are the following variable definition statements:

var p:^integer;

< p> only shows that p is a pointer variable pointing to an integer variable unit, but this integer unit does not exist, and there is no specific address value in the pointer variable p. In the program, you must pass the procedure call statement: new(p); to allocate an integer variable unit in the memory, and put the address of this unit in the variable p. A pointer variable can only store one address. A pointer can only point to one variable unit at the same time.

When the program executes new(p) again, a new integer variable unit is created in the memory, and the address of the new unit is stored in p, thereby losing the address of the old variable unit.

In order to save memory space, some existing dynamic variables that are no longer used should be released using the standard procedure dispose. The process form is: dispose (pointer variable name); it is the reverse process of new (pointer variable name). Its function is to release the storage unit of the dynamic variable pointed to by the pointer variable. For example, after using new(p) and then calling dispose(p), the dynamic variable pointed to by the pointer p is canceled and the memory space is returned to the system. At this time, the value of p is nil.

4. Things to note

1. The difference between P and P^

P is the pointer variable name pointing to the dynamic variable, and P^ is called a dynamic variable or flag variable. The value of P is the first address of P^, and the value of P^ is the same value as the base type.

2. Allocate storage units promptly after definition

After defining a pointer variable, no dynamic storage unit is allocated for the pointer. At this time, the value of P is undefined. Call P ^ will generate a runtime error. If you want to make the pointer available, you can assign a value to the pointer or allocate a storage unit through the NEW() process.

3. Timely recover the storage unit after use

After the pointer is used, the occupied storage space will not be automatically returned. The DISPOSE() process should be used in time to release the storage occupied by P^ unit to avoid wasting limited storage space.

11.2 Singly linked list

The data type of a singly linked list can be defined as follows:

type dlb=^node;

node=record < /p>

data:datatype;

next:dlb;

end;

Example 1 Continuously input a sequence of integers to form a linked list (and start with Record them in dynamic form), when the input number is -1, stop input, and then output the input integers in reverse order.

program lianbiao;

type link=^data;

data=record

num:integer;

next:link;

end;

var p,q:link;

i:integer;

begin

q:=nil;

readln( i);

while i<>-1 do

begin

new(p);

with p^ do

p>

begin

num:=i;

next:=q;

end;

q:=p ;

readln(i);

end;

while p<>nil do

begin

write(p^.num:6);

p:=p^.next;

end;

readln;

end.

(That's all, tired~~~~)