StackDemo

Program StackDemo implements a stack as an array of string. It contains procedure Push to add an item to the stack and function Pop to retrieve the item from the top of the stack. The integer variable StackTop has the value 1 if the stack is empty and otherwise is one more than the index of the last item to be added. (You can write alternative code for Push and Pop if you prefer StackTop to be equal to the index the last item to be added). Function PeekTop returns the string at the top of the stack but does not remove it.

Procedure ReverseLetters pushes letters of a word entered by the user onto an empty stack and then pops the letters and stores them in a new string. Procedure RummyDiscards demonstrates how a stack can be used to simulate a discard pile in a game of cards.

We obtained the following output from program StackDemo.

Output from program StackDemo

Output from program StackDemo

program StackDemo;
  {$APPTYPE CONSOLE}
uses
  SysUtils;
const
  MAX = 30;
var
  StrStack: array [1..MAX] of string;
  StackTop: integer;
  strCards : array[1..4] of string = ('Q'#6, '5'#3, 'A'#4, '3'#5);

procedure Push(TopString: string);
begin
  if StackTop <= MAX then
    begin
      StrStack[StackTop] := TopString;
      inc(StackTop);
    end
  else
    begin
      writeln('Stack full so item could not be added.');
    end;
end;

function Pop: string;
begin
  if StackTop >= 2 then
    begin
      dec(StackTop);
      result := StrStack[StackTop];
    end
  else
    result := '';
end;

function PeekTop: string;
begin
  result := strStack[StackTop - 1];
end;

procedure ReverseLetters;
var
  strInput, strReverse : string;
  Count : integer;
begin
  StackTop := 1;  //Initialise the stack.
  StrReverse :='';
  writeln('This procedure will use a stack to reverse the order of letters.');
  write(#13#10'Please enter a word to be reversed. ' );
  readln(strInput);
  for Count := 1 to length(strInput) do
    Push(strInput[Count]);
  for Count := 1 to length(strInput) do
    strReverse := strReverse + Pop;
  writeln(#13#10'Reversed, ',strInput, ' is ', strReverse,'.'#13#10);
end;

procedure RummyDiscards;
var
  Count : integer;
begin
  writeln(#13#10'This procedure will use a stack to model discards',
          ' in the card game rummy.');
  writeln(#13#10'Close to the end of a round the discard pile is:');
  for Count := 4 downto 1 do
    writeln(strCards[Count]);
  StackTop := 1;  //Initialise the stack.
  writeln(#13#10'These must be pushed onto the empty stack, ',
          strCards[1], ' first.');
  //Add 4 cards to stack
  for Count := 1 to 4 do
    Push(strCards[Count]);
  writeln(#13#10'Player A removes the top discard (which is ', Pop,').');
  writeln('This must be popped from the stack.');
  writeln(#13#10'Player A then discards J'#4,'.');
  writeln('This must be pushed onto the stack.');
  Push('J'#4);
  writeln(#13#10'Player B picks up the top discard, which is ', Pop, '.');
  writeln('This must be popped from the stack.');
  writeln(#13#10'This allows Player B to go out (leaving ',
          PeekTop ,' at the top of the discard pile).');
end;

begin
  ReverseLetters;
  RummyDiscards;
  readln;
end. 

Programming - a skill for life!

Introduction to demonstrations of stacks