Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Stack writing in Pascal

Posted on 1998-12-16
3
Medium Priority
?
494 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 150 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Files go missing when using DFS (Distributed File System) Replication and how to recover them and fix it.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

688 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