Sample Solution to Lab #2

A sample driver with numbered comments.

/////////////////////////////////////////////////////////////////////
// Student Name: Artie Fufkin
// Student ID  : 123-45-6789
// Course      : CS 162
// Assignment  : Lab #2
// Program File: stack.cpp
// Due date    : 7/20/98
//
// description : This is the implemention file for functions 
//               specified in stack.h which manipulate stacks
//               Manipulations include:
//              * create stacks ( constructors )
//		* add an item to the stack
//		* remove an item from the stack
//	        * read item on top of the stack
//	        * check if the the stack is empty
//		* check if the stack is full
//		* display the stack
//		* get the number of items on the stack
//		* get the size of the stack
// 
//////////////////////////////////////////////////////////////////////

#include "stack.h"     // for functions in this file 
#include <iostream.h>  // for I/O
#include <iomanip.h>   // for data manipulation

//////////////////////////////////////////////////////////////////////
// Constructor (default)
//
//   Creates a stack of size MAX_SIZE by setting the private data size 
//   to MAX_SIZE and the top to -1.
//           
//   Inputs: None.
//
//  Outputs: None.
//
Stack::Stack(void)
{
	top = -1;
	size = MAX_SIZE;
}


//////////////////////////////////////////////////////////////////////
// Constructor 
//
//   Creates a stack of size 'initial_size'. If the 'initial_size' 
//   is < 0, then the size of the stack is set to 0. If the
//   'initial_size' is > MAX_SIZE then the size of the stack is
//   set to MAX_SIZE. If 0 <= initial_size <= MAX_SIZE then size 
//   is set to initial_size. Also set top to -1.
//           
//   Inputs: initial-size - an integer that specifies the size of 
//	                        the stack
//  Outputs: None.
//
Stack::Stack(int initial_size)
{
	if ( initial_size  < 0 )
		size = 0;
	else if ( initial_size > MAX_SIZE )
		size = MAX_SIZE;
	else
		size = initial_size;

	top = -1;
}


/////////////////////////////////////////////////////////////////////////
// Function: pushItem 
//
//   Adds an item to the stack . if the stack is full, give error 
//   message, otherwise set the new top value items[top + 1] to 
//   item, and increment the top by one
//           
//   Inputs: item - an integer that is added to the statck
//
//  Outputs: None.
//
void Stack::pushItem(int item)
{
	if ( isFull() )
		cout << "Error:stack is full" << endl;
	else
		items[++top] = item;
	
}

/////////////////////////////////////////////////////////////////////////
// Function: popItem 
//
//   Removes the item that is at the top of the stack.If the stack
//   is empty, give  error message and return 0, otherwise return
//   the top item of the stack items[top], and decrement the top
//   by one. 
//           
//   Inputs: None.
//
//  Outputs: Returns the integer at the top of the stack
//
int Stack::popItem(void)
{
    if ( isEmpty() )
    {
	cout << "Error: Stack is empty" << endl;
	return 0;
    }
    else
    {
        return items[top--];
    }

}

	
/////////////////////////////////////////////////////////////////////////
// Function: peekAtTop 
//
//   Returns the integer that is at the top of the stack without 
//   removing it. If the stack is empty, give error message and 
//   return 0, otherwise return top item of the stack items[top]. 
//           
//   Inputs: None.
//
//  Outputs: Returns the integer at the top of the stack
//
int Stack::peekAtTop(void) const
{
	if ( isEmpty() )
	{
		cout << "Error: Stack is empty" << endl;
		return 0;
	}
	else
	{
   		return items[top];
	}
	
}

	


/////////////////////////////////////////////////////////////////////////
// Function: isEmpty
//
//   Checks if the stack is empty. If the index of top < 0, the stack
//   is empty, return TRUE, otherwise return FALSE.
//
//   Inputs: None.
//
//  Outputs: Returns TRUE if the stack is empty, otherwise, FALSE
//
Boolean  Stack::isEmpty(void) const
{
	Boolean empty;

	if (top < 0 )
		empty = TRUE;
	else
		empty = FALSE;

	return empty;
}


/////////////////////////////////////////////////////////////////////////
// Function: isFull
//
//   Checks if the stack is full. If the index of the top is >= 
//   ( size - 1 ), the stack is full, return TRUE, othewise return 
//   FALSE.
//
//   Inputs: None.
//
//  Outputs: Returns TRUE if the stack is full, otherwise, FALSE
//
Boolean Stack::isFull(void) const
{
	Boolean full;

	if ( top >= (size - 1) )
		full = TRUE;
	else
		full = FALSE;

	return full;
}


/////////////////////////////////////////////////////////////////////////
// Function: display
//
//   Prints out the contents of the stack to the screen
//
//   Inputs: None.
//
//  Outputs: Displays the stack's contents on the screen
//
void Stack::display(void) const
{
    for (int i = 0; i <= top; i++)
        cout << setw(3) << items[i] << " ";

    cout << endl;
}

////////////////////////////////////////////////////////////////////////
// Function: getCount
//
//   Returns the number of items on the stack using the index value
//   of the top of the stack. Count should be top + 1.
//
//   Inputs: None.
//
//  Outputs: The number of items on the stack
//
int Stack::getCount(void) const
{
	return ( top + 1 );
}


/////////////////////////////////////////////////////////////////////////
// Function: getSize
//
//   Returns the size of the stack 
//
//   Inputs: None.
//
//  Outputs: The size of the stack
//
int Stack::getSize(void) const
{
	return ( size );
}

Back to Outline