Pascal --> C (Stack implementation)
DaiLi
daili at wasp.eng.ufl.edu
Fri Feb 8 15:18:24 AEST 1991
Greetings...
Could anybody out there translate the following pascal
stack implementation program into C? thanks...
please email to me....
A sequential implementation of the ADT Stack in Pascal
const
maxstack = maximum size of stack
type
itemtype = desired type of stack item
stack = record
Items : array [1..maxstack] of itemtype
Top : 0..maxstack
end
{Basic stack operations - sequential implementation:
Create, IsEmpty, Push, Pop, and StackTop }
{ ------------------------------------------------------
Create: create an empty stack S.
------------------------------------------------------ }
Procedure Create(var S : stack);
begin
S.Top : = 0
end;
{ ------------------------------------------------------
IsEmpty: Detemine if stack S is empty
------------------------------------------------------ }
function IsEmpty(S : stack) : boolean;
begin
IsEmpty := (S.Top<1)
end;
{ ------------------------------------------------------
Push: Add newitem to stack S. The operation fails if the
stack is full (has maxstack items). The flag success
indicates whether the operation succeeded.
------------------------------------------------------ }
procedure Push(var S : stack; newitem : itemtype;
var success : boolean);
begin
if S.Top = maxstack
then success := false
else
begin
S.Top := S.Top + 1;
S.Items[S.Top] := newitem;
success := true
end
end;
{ ------------------------------------------------------
Pop: Remove from stack S the item that was most recently
added. The operation fails if the stack is empty. The flag
success indicates whether the operation succeeded.
------------------------------------------------------ }
procedure Pop(var S : stack; var success : boolean);
begin
if S.Top <1
then success := false
else
begin
S.Top := S.Top - 1;
success := true
end
end;
{ ------------------------------------------------------
StackTop: Retrieve into getitem the item fro stack S that
was most recently added, leaving S unchanged. The operation
fails if the stack is empty. The flag success indicates
whether te operation succeeded.
------------------------------------------------------ }
procedure StackTop(S: stack; var getitem: itemtype;
var success : boolean);
begin
if S.Top < 1
then success := false
else
begin
getitem := S.Items[S.Top];
success := true
end
end;
More information about the Comp.lang.c
mailing list