Solved

Stack writing in Pascal

Posted on 1998-12-16
3
481 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Extract text from webbrowser 5 1,434
default value in Delphi component 2 526
Client Data Set - Wide Memo 6 1,799
interacting with web form 4 154
After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
Configuring Remote Assistance for use with SCCM
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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