Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the stack ADT in Pascal. Solution Implement the stack ADT in Pascal: p

ID: 3820069 • Letter: I

Question

Implement the stack ADT in Pascal.

Explanation / Answer

Implement the stack ADT in Pascal: procedure Create(var S: Stack); begin S.Top := 0 end; function IsEmpty(S: Stack): boolean; begin IsEmpty := S.Top = 0 end; procedure Push(I: Char; var S: Stack); begin if S.Top >= maxstack then writeln('Stack overflow') else begin S.Top := S.Top+1; S.Item[S.Top] := I end end; function Top(S: Stack): char; begin if S.Top = 0 then writeln('Stack underflow') else Top := S.Item[S.Top] end procedure Pop(var S: Stack); begin if S.Top = 0 then writeln('Stack underflow') else S.Top := S.Top - 1 end; The procedure Pop is often defined to combine the effects of a top and a pop instead. In this case, it would be coded as: procedure Pop(var S: Stack; var I: char); begin if S.Top = 0 then writeln('Stack underflow') else begin I := S.Item[S.Top]; S.Top := S.Top - 1 end end;