CSCI 132 Data Structures--Spring 2014

    Home | | Syllabus | | Assignments | | Lecture Notes

    Assignment 3 Solutions

    Solution to deque.cc code:

    /****************************************************
     * File Name: deque.cc
     * Author: Brenda Student
     * Course: CSCI 132
     * Assignment: Homework 3
     * Purpose: Implementation of Queue class
     * Date: February 13, 2014
     *****************************************************/
    
    #include "deque.h"
    
    Deque::Deque()  //optional
    /*
      Pre: None
      Post: The Deque is initialized to be empty.
    */
    { 
    	//nothing to initialize
    }
    
    Error_code Deque::append_front(const Queue_entry &item)
    /*
    Pre: The deque is not full
    Post: item is added to the front of the Deque. If the Deque is full
    return an Error_code of overflow.
    */
    {
      if (count >= maxqueue) {
        return overflow;
      }
      if (front == 0) {
      	front = maxqueue -1;
      } else {
      	front = front -1;
      }
      entry[front] = item;
      count ++;
      return success;
    }
    
    Error_code Deque::serve_front()
    /*
    Pre: the deque has at least one entry
    Post: The front of the Deque is removed. If the Deque
    is empty return an Error_code of underflow.
    */
    {
      return serve();        //Or could write Queue::serve();
    }
    
    Error_code Deque::retrieve_front(Queue_entry &item)const
    /*
    Pre: The deque has at least one entry
    Post: The front of the Deque retrieved to the output parameter item.
    If the Deque is empty return an Error_code of underflow.
    */
    {
      return retrieve(item);
    }
    
    Error_code Deque::append_rear(const Queue_entry &item)
    /*
    Pre: The deque is not full
    Post: item is added to the rear of the Deque. If the Deque is full
    return an Error_code of overflow.
    */
    {
      return append(item);
    }
    
    Error_code Deque::serve_rear()
    /*
    Pre: The deque has at least one entry
    Post: The rear of the Deque is removed. If the Deque
    is empty return an Error_code of underflow.
    */
    {
      if (count <= 0)
        return underflow;
      if (rear == 0) {
      	rear = maxqueue -1;
      } else {
      	rear = rear -1;
      }
      count --;
      return success;
    }
    
    Error_code Deque::retrieve_rear(Queue_entry &item)const
    /*
    Pre: Deque has at least one entry
    Post: The rear of the Deque retrieved to the output parameter item.
    If the Deque is empty return an Error_code of underflow.
    */
    {
      if (count <= 0)
        return underflow;
      item = entry[rear];
      return success;
    }
    
    
    

    Home | | Syllabus | | Assignments | | Lecture Notes