Solved

Stack writing in Pascal

Posted on 1998-12-16
3
479 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

763 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