[̲] [] [] [ ]

 

11.3 ᒺ.

 

ᒺ, , , . ᒺ . ᒺ, , . ᒺ .

 

. ᒺ , . ,

 

Type PA = ^A;

A = Object

constructor Create(i: word);

...

end;

PB= ^B;

B = Object(A)

...

end;

Var p1: PA; p2: PB;

 

PA PB A B . ᒺ, , , , . , ᒺ. , ᒺ ᒺ , , p1:=p2.

new. new, , new(p1). , ᒺ . . , ᒺ A :

new(p1); p1^.Create(n)

 

new:

 

new(p1, Create(n))

 

new . , ᒺ ( ᒺ) . new:

 

p1:=new(PA, Create(n))

 

new , , ᒺ; ᒺ new. , , .

 

dispose, , dispose(p1). . :

 

p1:=p2; dispose(p1)

 

, ᒺ, p2, B, , ᒺ A, p1 (B A). dispose(p1) , ᒺ A . , ᒺ, , . , procedure destructor. :

 

destructor Done;

 

dispose:

 

dispose(p1, Done)

 

, ᒺ, . . , , ᒺ, .

 

. 11.1 ᒺ

 

ᒺ ᒺ (. 11.1). . ᒺ , , ᒺ. . 11.1 ᒺ , .

ᒺ (AnyObject). ᒺ AnyObject . AnyObject, AnyRef AnyThing. AnyObject Init Done.

 

Unit AnyThing;

 

interface

 

Type

AnyRef = ^AnyObject;

AnyObject = Object { }

Constructor Init; { }

Destructor Done; Virtual; { }

end;

 

implementation

 

Constructor AnyObject.Init;

begin end;

 

Destructor AnyObject.Done;

begin end;

 

end.

 

 

AnyDeque. Dref Delem ( dt AnyObject). . . , bg, en , Init, Empty, PutBg, GetBg, PutEn, GetEn, Clear.

ᒺ, , . ij GetBg GetEn , AnyRef. AnyDeque GetEn PutEn, GetBg PutBg , . , ᒺ (Dispose(p^.dt,Done)).

 

 

{ ᒺ }

Unit AnyDeque;

 

interface

 

Uses AnyThing;

 

Type

Dref = ^Delem;

Delem = Record { }

dt: AnyRef; { }

next,prev: Dref

end;

Deque = Object { }

private

bg,en: Dref; { }

public

Procedure Init; { }

Function Empty: Boolean; { ? }

Procedure PutBg(D: AnyRef);{ }

Function GetBg: AnyRef; { }

Procedure PutEn(D: AnyRef);{ }

Function GetEn: AnyRef; { }

Destructor Clear; { }

end;

 

implementation

 

Procedure Deque.Init;

begin

bg:=nil; en:=nil

end;

 

Function Deque.Empty;

begin

Empty:=(bg = nil) and (en = nil)

end;

 

Procedure Deque.PutBg;

var p: Dref;

begin

New(p); p^.dt:=D; p^.prev:=nil; p^.next:=bg;

if bg <> nil then bg^.prev:=p

else en:=p;

bg:=p

end;

 

Function Deque.GetBg;

var p: Dref;

begin

if bg = nil then begin

writeln('Deque.GetBg: '); halt

end;

p:=bg; GetBg:=p^.dt; bg:=p^.next;

if bg = nil then en:=nil

else bg^.prev:=nil;

dispose(p)

end;

 

.................

 

Destructor Deque.Clear;

var p,p1: Dref;

begin

p:=bg;

while p <> nil do begin

Dispose(p^.dt,Done);

p1:=p; p:=p^.next;

Dispose(p1)

end;

bg:=nil; en:=nil

end;

 

end.

 

 

ᒺ , . n , 1 n. m . . n- , , , , . . , , n m.

Player () AnyObject. No , Init n, Show Done.Player ᒺ.

CounterAnyThingAnyDeque. (n) (m). nPlayer 1 n. New(PlayerRef,Init(i)) ᒺ Player, Init(i) ᒺ. GetEn dq -. , AnyRef PlayerRef. , Player AnyObject. ϳ , (m-1) , m- .

pl:=PlayerRef(dq.GetBg). dq.GetBg AnyRef. pl:=dq.GetBg, pl Player, AnyObject. pl AnyRef, pl:=dq.GetBg pl^ AnyObject Player. : PlayerRef(dq.GetBg) PlayerRef, pl. , , ᒺ Player. ᒺ.

ϳ pl ᒺ, dispose(pl, Done).

 

 

Program Counter;

 

Uses AnyThing,AnyDeque;

 

TYPE

PlayerRef = ^Player;

Player = Object(AnyObject) {}

No: Integer; { }

Constructor Init(n: Integer); { }

Procedure Show; { }

Destructor Done; Virtual; {}

end;

 

Constructor Player.Init;

begin

No:=n

end;

 

Procedure Player.Show;

begin

writeln(No)

end;

 

Destructor Player.Done;

begin

end;

 

var

dq: Deque; pl: PlayerRef; i,m,n: Integer;

 

begin

write(' : '); Readln(n);

write(' : '); Readln(m);

dq.Init;

for i:=1 to n do begin

dq.PutEn(New(PlayerRef,Init(i)));

end;

writeln(' , ');

while not dq.Empty do begin

for i:=1 to m-1 do begin

dq.PutEn(dq.GetBg)

end;

pl:=PlayerRef(dq.GetBg);

pl^.Show;

dispose(pl,Done)

end;

end.

 

 

C++ ᒺ , new. , Apa.

 

class A {

A(int n);

.................

};

.................

 

A *pa;

 

 

pa = new A(2);

 

A, , ᒺ pa.

, ᒺ delete, ,

 

delete pa;

 

ᒺ, pa.

C++ , delete. , C++, , . A ~A. ᒺ.

 

ᒺ , , ᒺ . AnyObject anything.h. . C++ ᒺ AnyObject.

 

/* anything.h */

 

class AnyObject { //

public:

AnyObject() { };

~AnyObject() { };

};

 

ᒺ anydeque ( anydeque.h anydeque.). Deque Deque ( Init ) ~Deque ( Clear ).

 

/* anydeque.h */

 

#include "anything.h"

 

typedef struct delem *dref;

struct delem { //

AnyObject *dt; //

dref next, prev;

};

 

class Deque { //

dref bg, en; //

public:

Deque(); //

~Deque(); //

int empty(); // ?

void put_bg(AnyObject *d); //

AnyObject *get_bg(); //

void put_en(AnyObject *d); //

AnyObject *get_en(); //

};

 

 

/* anydeque.cpp */

 

#include <stdio.h>

#include <stdlib.h>

#include "anydeque.h"

 

Deque::Deque()

{

bg = en = NULL;

}

 

int Deque::empty()

{

return bg == NULL && en == NULL;

}

 

void Deque::put_bg(AnyObject *d)

{

dref p;

 

p = (dref) malloc(sizeof(struct delem));

p -> dt = d;

p -> prev = NULL;

p -> next = bg;

if (bg == NULL) en = p;

else bg -> prev = p;

bg = p;

}

 

AnyObject *Deque::get_bg()

{

dref p;

AnyObject *d;

 

 

if (bg ==NULL)

{

printf("get_bg: i\n");

exit(1);

}

p = bg;

d = p -> dt;

bg = bg -> next;

if (bg == NULL) en = NULL;

else bg -> prev = NULL;

free(p);

return d;

}

 

.................

 

Deque::~Deque()

{

dref p,p1;

AnyObject *d;

 

 

p = bg;

while (p != NULL)

{

d = p -> dt;

delete d;

p1 = p; p = p -> next;

free(p1);

}

bg = en = NULL;

}

 

Counter Player AnyObject. Player inline-. , m-

 

pl = (Player *) dq.get_bg();

 

(Player *) ᒺ Player.

 

/* Counter */

 

#include <stdio.h>

#include "anydeque.h"

 

class Player: public AnyObject { //

int no; //

public:

Player(int n){ no=n; }; //

void show() { printf("%d\n", no); }; //

~Player() { }; //

};

 

main()

{

Deque dq;

Player *pl;

int i,m,n;

 

printf("' : "); scanf("%d", &n);

printf("' : "); scanf("%d", &m);

for (i=1; i<=n; i++)

dq.put_en(new Player(i));

printf("' , \n");

while (!dq.empty())

{

for (i=1; i<=m-1; i++)

dq.put_en(dq.get_bg());

pl = (Player *) dq.get_bg();

(*pl).show();

delete pl;

}

}

 

[̲] [] [] [ ]