Class Notes 9/19 CS 240

Class Notes 9/19 CS 240

<p>Class Notes 9/19 – CS 240</p><p> Stack data structure is LIFO. (Last In First Out)  You do not need a 'with' statement in the body of the package implementation to link it to the specification. That happens automatically  In the stack specification package:  package stack is Both the package specification and implementation headers end with 'is'  Type  type myArray is array (1..50) of integer;  type stackType is record; An array is homogeneous and a record is heterogeneous  aStack : myArray;  top : integer;  Operations  procedure push (someStack : IN OUT stackType someValue : IN integer);  procedure pop (someStack : IN OUT stackType someValue : OUT ?);  procedure makeEmpty (someStack : IN OUT stackType);  <optional> procedure peek (someStack : IN stackType someValue : OUT integer);  function isEmpty (someStack : IN stackType) return boolean;  function isFull (someStack : IN stackType) return boolean;  In the stack implementation package: (Stub this out and compile correctly before attempting to write all of the code.) (Documentation needs to be added to this to outline the preconditions and postconditions of each procedure and function.)  package body stack is  Type does not have to be redefined, the line from the spec will be used.  Operations The procedure headers are the same as in the specification, I will use ellipses in place of them.  procedure push (someStack : IN OUT stackType someValue : IN integer) is full : boolean; begin --if the stack isn't full, we can push something on to it --ask whether it's full full := isFull (someStack); if not (full) then --put the value on the stack someStack.top := someStack.top + 1; someStack.myArray(someStack.top) := someValue; end if; end push;  procedure pop (...) is begin null; end pop;  procedure peek (...) is begin null; end peek;  procedure makeEmpty (...) is begin someStack.top := 0; end makeEmpty;  function isEmpty (...) return boolean is result : boolean; begin result := false; if someStack.top = 0 then result := true; end if; return result; end isEmpty; <only have one return statement per function>  function isFull (...) return boolean is result : boolean; begin result := false; if (someStack.top = 20) then result := true; end if; return result; end isFull;</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    2 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us