Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!uakari.primate.wisc.edu!eng.ufl.edu!wasp.eng.ufl.edu!daili From: daili@wasp.eng.ufl.edu (DaiLi) Newsgroups: comp.lang.c Subject: Pascal --> C (Stack implementation) Message-ID: <1991Feb8.041824.7805@eng.ufl.edu> Date: 8 Feb 91 04:18:24 GMT Sender: news@eng.ufl.edu Reply-To: daili@wasp.eng.ufl.edu () Organization: U of Florida Engineering Computer Support Facility Lines: 86 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;