| Код | type TMyType = integer;
TStack = ^TNode; TNode = record value: TMyType; next: TStack; end; {-----------------------------------------------} {Занесение элемента в стек} procedure Push(var theStack: TStack; theValue: TMyType); var node: TStack; begin new(node); node^.value := theValue; node^.next := theStack;
theStack := node; end; {-----------------------------------------------} {Вывод наэкран всех элементов стека} procedure Print(theStack: TStack); var node: TStack; begin node := theStack; while (node <> nil) do begin write(node^.value:3); node := node^.next; end; writeln; end; {-----------------------------------------------} {Формирование стека заданной длинны. Формируется случайным образом} procedure PushRandomValue(var theStack: TStack; theCount: word); var i: word; begin theStack := nil; for i := 1 to theCount do begin Push(theStack, random(2*theCount) + 1); end;
end; {-----------------------------------------------} {Перемещение элементов одного стека в другой} procedure Move(var theStackTo, theStackFrom: TStack); var node: TStack; begin while (theStackFrom <> nil) do begin node := theStackFrom; theStackFrom := theStackFrom^.next; node^.next := theStackTo; theStackTo := node; end; end; {-----------------------------------------------}
var stack1, stack2: TStack;
begin randomize;
PushRandomValue(stack1, random(7) + 3); PushRandomValue(stack2, random(7) + 3); write('[1] : '); Print(stack1); write('[2] : '); Print(stack2); writeln; Move(stack2, stack1); write('[1] : '); Print(stack1); write('[2] : '); Print(stack2); end.
|
Бинарник с исходником ниже |