Solved

Stack writing in Pascal

Posted on 1998-12-16
3
472 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
A safe way to clean winsxs folder from your windows server 2008 R2 editions
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now