Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Course: CS3420-60 Data Structures
- // Name: Kendrick, Jessica
- // Assignment: P1.1
- // Date assigned: 01/23/2020
- // Date due: 02/04/2020
- // Date handed in: 02/04/2020
- // Remark: The program defines a class container which holds an array and contains functions
- // to manipulate it. An example container ex is made. Five values are put into the
- // back and then deleted to test the validity of the functions.
- #include "stdafx.h"
- #include <iostream>
- #include <algorithm>
- #include <numeric>
- using namespace std;
- class container
- {
- friend ostream& operator<<(ostream& out, container &cobj);
- // Postcondition: display the contents of container object cobj in the format shown in the below sample outputs
- public:
- container();
- // Postcondition: data member n is initialized to -1 and all elements in the empty arr array are initialized to zero
- bool isEmpty();
- // Postcondition: returns true if nothing is stored in the container; returns false otherwise
- bool isFull();
- // Postcondition: returns true if the container object (i.e., the arr array) is full; returns false otherwise
- int size();
- // Postcondition: returns the “size” which is the actual number of elements currently stored in the containe robject; size <= capacity
- int capacity();
- // Postcondition: returns the storage capacity of the container.
- bool insertBack(int val);
- // Precondition: the container object is not full
- // Postcondition: if arr array is not full, n is incremented by 1; returns true with val is inserted at the end of the arr array
- // Otherwise, returns false; the value is not inserted and program execution continues.
- bool deleteBack();
- // Precondition: The array must not be empty
- // Postcondition: the last element stored in the array is logically removed and the size of the is decremented by 1
- bool insertFront(int val);
- // Precondition: the container is not full
- // Postcondition: returns true if the container is not full and val is inserted at the front of the array; n is incremented by 1; and all
- // existing elements are shifted one step to the right. Otherwise, returns false; program execution continues
- bool deleteFront();
- // Precondition: the container must not be empty
- // Postcondition: returns true and the front element is deleted; and n is decremented by 1;
- // returns false otherwise; also, arr[1]... arr[size-1] of the container object is shifted one step left to close the gap.
- private:
- static const int CAPACITY = 10; // physical size of the arr array or the storage capacity of a container object
- int arr[CAPACITY]; // arr array can store up to CAPACITY (10 in our case) integers
- int n; // n is used as the subscript for the arr array. n is initialized to -1 for an empty array
- // Each time a new value is inserted into the arr array, n must first be incremented
- // by 1. Since n has been initialized to -1, the first inserted value is stored in arr[0],
- // and the 2nd inserted value will be in arr[1], etc. and the nth inserted value will be
- // stored in arr[n – 1]. Obviously, n + 1 represents the actual number of elements
- // stored in the array after n rounds of insertion.
- };
- // Postcondition: data member n is initialized to -1 and all elements in the empty arr array are initialized to zero
- container::container() {
- n = -1;
- fill_n(arr, 10, 0);
- }
- // Postcondition: returns true if nothing is stored in the container; returns false otherwise
- bool container::isEmpty() {
- if (size() == 0) {
- return true;
- }
- return false;
- }
- // Postcondition: returns true if the container object (i.e., the arr array) is full; returns false otherwise
- bool container::isFull() {
- if (size() == CAPACITY) {
- return true;
- }
- return false;
- }
- // Postcondition: returns the “size” which is the actual number of elements currently stored in the containe robject; size <= capacity
- int container::size() {
- return (n + 1);
- }
- // Postcondition: returns the storage capacity of the container.
- int container::capacity() {
- return CAPACITY;
- }
- // Precondition: the container object is not full
- // Postcondition: if arr array is not full, n is incremented by 1; returns true with val is inserted at the end of the arr array
- // Otherwise, returns false; the value is not inserted and program execution continues.
- bool container::insertBack(int val) {
- if (!isFull()) {
- n++;
- arr[n] = val;
- return true;
- }
- return false;
- }
- // Precondition: The array must not be empty
- // Postcondition: the last element stored in the array is logically removed and the size of the is decremented by 1
- bool container::deleteBack() {
- if (!isEmpty()) {
- arr[n] = 0;
- n--;
- return true;
- }
- return false;
- }
- // Precondition: the container is not full
- // Postcondition: returns true if the container is not full and val is inserted at the front of the array; n is incremented by 1; and all
- // existing elements are shifted one step to the right. Otherwise, returns false; program execution continues
- bool container::insertFront(int val) {
- if (!isFull()) {
- memmove(arr + 1, arr, size());
- arr[0] = val;
- n++;
- return true;
- }
- return false;
- }
- // Precondition: the container must not be empty
- // Postcondition: returns true and the front element is deleted; and n is decremented by 1;
- // returns false otherwise; also, arr[1]... arr[size-1] of the container object is shifted one step left to close the gap.
- bool container::deleteFront() {
- if (!isEmpty()) {
- arr[0] = 0;
- n--;
- memmove(arr, arr + 1, size());
- return true;
- }
- return false;
- }
- // Postcondition: display the contents of container object cobj in the format shown in the below sample outputs
- ostream& operator<<(ostream& out, container &cobj) {
- out << "Container storage capacity = " << cobj.CAPACITY
- << "\nCurrently, container contains " << cobj.size() << " elements."
- << "\nThe contents of the container:\n";
- if (cobj.isEmpty()) {
- out << "*** Container is currently empty!";
- }
- else {
- for (int i = 0; i < cobj.size(); ++i) {
- out << cobj.arr[i] << "\t";
- }
- out << "\n\n";
- }
- return out;
- }
- int main() {
- container ex;
- cout << ex;
- cout << "\n\nThe container object is empty!"
- << "\nCurrently, the container object contains 0 elemet(s) or value(s)"
- << "\n\nWe now insert 5 values at the back of the array, one at a time:\n";
- for (int i = 1; i < 6; ++i) {
- ex.insertBack(i * 10);
- cout << ex;
- }
- cout << "We now delete all 5 values from the back of the array, one at a time:\n";
- for (int i = 0; i < 5; ++i) {
- ex.deleteBack();
- cout << ex;
- }
- cout << "\n\nWe try to delete from an empty container:";
- ex.deleteBack();
- cout << "\nCannot delete from an empty container!\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement