Solved

Stack writing in Pascal

Posted on 1998-12-16
3
478 Views
Last Modified: 2008-02-26
How can I write procedure empty and puch under the following situation ?
unit StackADT;
{Provides type Stack, a sequence permitting
 insertions and deletions only a the front.}

interface

  const
         MaxStackSize = 100;

  type
         StackElement = Integer;
         StackSize    = 0..MaxStackSize;
         StackIndex   = 1..MaxStackSize;
         Stack = record
                         size : StackSize;
                         val  : array [StackIndex]
                                of StackElement;
                 end;

  procedure MakeEmpty (var s {input/output} : Stack);
  {Make s = the empty stack.}

  procedure Push (var s {input/output} : Stack;
                  e {input} : StackElement);
  {Inserts e at front of stack s.}

  procedure Pop (var s {input/output} : Stack;
                 var v {output} : StackElement);
  {Requires s not to be empty; removes first element of s and
   stores in v.}

  function Top (s {input} : Stack) : StackElement;
  {Requires s not to be empty; returns first element of s.}

  function Empty (s {input} : Stack) : Boolean;
  {Returns (s is empty).}

implementation

  procedure MakeEmpty (var s {input/output} : Stack);
  begin {MakeEmpty}
         *** Student Input ***
  end; {MakeEmpty}

  procedure Push (var s {input/output} : Stack;
                  e {input} : StackElement);
  begin {Push}
         if s.size = MaxStackSize then
                     WriteLn('Error - Can''t push onto full stack.')
         else
               begin
                      *** Student Input ***
                      *** Student Input ***
               end
  end; {Push}

  procedure Pop (var s {input/output} : Stack;
                 var v {output} : StackElement);
  begin {Pop}
         if Empty(s) then
                     WriteLn('Error - Can''t pop empty stack.')
         else
               begin
                      *** Student Input ***
                      *** Student Input ***
               end
  end; {Pop}

  function Top (s {input} : Stack) : StackElement;
  begin {Top}
         if Empty(s) then
                     WriteLn('Error - No top of empty stack.')
         else
               *** Student Input ***
  end; {Top}

  function Empty (s {input} : Stack) : Boolean;
  begin {Empty}
         *** Student Input ***
  end; {Empty}

end. {StackADT}
0
Comment
Question by:zoe_chui
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 50 total points
ID: 1216544
In empty, you have to return a boolean that depends on the condition of size=0.  If size=0, the stack is empty.  Size is your stack pointer.


function Empty (s {input} : Stack) : Boolean;
  begin {Empty}
    Empty := (s.size = 0);      
  end; {Empty}


When you push, you simply increment the stack pointer, then store the value you want to push in that particular part of the array pointed to by the stack pointer.

procedure Push (var s {input/output} : Stack;
                  e {input} : StackElement);
  begin {Push}
         if s.size = MaxStackSize then
                     WriteLn('Error - Can''t push onto full stack.')
         else
               begin
                 s.size := s.size + 1;
                 s.val[size] := e;
               end
  end; {Push}


If you want to pop a value, you do the opposite:  return the value pointed to be size, then decrement the stack pointer.  You already have sufficient error checking in both functions.
0
 

Author Comment

by:zoe_chui
ID: 1216545
Hi,

The function empty is O.K. but when the procedure Push was not work, it got a "unknown indentifier" during compile in the following statement :
s.val[size] := e;

But when I change to s.val[1] := e;
It can complie suessfully !
Does it mean I make it correct ?
Pls advise me ?

Thanks

Zoe
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1216546
Change it to s.val[s.size].

My mistake...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your phone running out of space to hold pictures?  This article will show you quick tips on how to solve this problem.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question