Class Notes 9/19 – CS 240

 

l        Stack data structure is LIFO. (Last In First Out)

l        You do not need a 'with'  statement in the body of the package implementation to link it to the specification. That happens automatically

l        In the stack specification package:

l        package stack is

            Both the package specification and implementation headers end with 'is'

l        Type

l        type myArray is array (1..50) of integer;

l        type stackType is record;

            An array is homogeneous and a record is heterogeneous

l        aStack : myArray;

l        top : integer;

l        Operations

l        procedure push (someStack : IN OUT stackType

                                                someValue : IN integer);

l        procedure pop (someStack : IN OUT stackType

                                                someValue : OUT ?);

l        procedure makeEmpty (someStack : IN OUT stackType);

l        <optional> procedure peek (someStack : IN stackType

                                                                        someValue : OUT integer);

l        function isEmpty (someStack : IN stackType) return boolean;

l        function isFull (someStack : IN stackType) return boolean;

l        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.)

l        package body stack is

l        Type does not have to be redefined, the line from the spec will be used.

l        Operations

            The procedure headers are the same as in the specification, I will use ellipses in     place of them.

l        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;

l        procedure pop (...) is

            begin

                        null;

            end pop;

l        procedure peek (...) is

            begin

                        null;

            end peek;

l        procedure makeEmpty (...) is

            begin

                        someStack.top := 0;

            end makeEmpty;

l        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>

l        function isFull (...) return boolean is

            result : boolean;

            begin

                        result := false;

                        if (someStack.top = 20) then

                                    result := true;

                        end if;

                        return result;

            end isFull;