CSCI 132 Data Structures--Spring 2014

    Home | | Syllabus | | Assignments | | Lecture Notes

    Laboratory 3 Solutions

    Solution to ext_stack.cc code:

    /********************************************************************
    * Name: Brenda Student
    * Date: 2/6/14
    * Course: CSCI 132 Section 01
    * Assignment: Lab 3
    * Instructor: Royden
    * Program: ext_stack.cc
    * Purpose:  Implementation of extended stack class
    *********************************************************************/
    
    #include "ext_stack.h"
    
    /********************************************************************
    *Function: void Extended_stack :: clear( )
    *Pre: None
    *Post: Clears all entries from the current stack.
    *********************************************************************/
    void Extended_stack :: clear( ) 
    {
      count = 0;
    } // end clear()
    
    /********************************************************************
    *Function: bool Extended_stack :: full( )
    *Pre: None
    *Post: Returns true if stack is full and false otherwise
    *********************************************************************/
    bool Extended_stack::full( ) const {
      return count == maxstack;
    } //end full()
    
    /********************************************************************
    *Function: bool Extended_stack :: size( )
    *Pre: None
    *Post: Returns the current number of items in the stack
    *********************************************************************/
    int Extended_stack::size( ) const {
      return count;
    } //end size()
    
    /********************************************************************
    *Function: int Extended_stack :: sum( )
    *Pre: None
    *Post: Returns the sum of all the items in the stack.
    *********************************************************************/
    int Extended_stack :: sum( ) const {
      int sum = 0;               //sum of stack entries
      int tempCount = count;     //counter to track position in array
    
      while (tempCount > 0) {
          sum += entry[tempCount - 1];
          tempCount -= 1;
      } //end while
    
      return sum;
    } //end sum()
    
    

    Home | | Syllabus | | Assignments | | Lecture Notes