Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#1 [5 points] a) Assume that you store the names of courses in an array-based stack (of capacity 6). Draw a sketch of the contents

#1 [5 points]

a) Assume that you store the names of courses in an array-based stack (of capacity 6). Draw a sketch of the contents of this data structure, specifically of class ArrayStack. Make sure to show the ArrayStack object with its member variables.

b) Complete the following main function to create your stack above (Your code should go in only the indicated space - do not change any other code).

#include

#include "ArrayStack.h"

using namespace std;

int main()

{

// YOUR CODE TO CREATE THE STACK GOES HERE

}

c) Assume that you store the names of courses in a linked-list-based stack. Draw a sketch of the contents of this data structure, specifically of class LinkedStack. Make sure to show the LinkedStack object with its member variables.

d) Assume that you store the names of courses in an array-based queue (of capacity 6). Draw a sketch of the contents of this data structure, specifically of class ArrayQueue. Make sure to show the ArrayQueue object with its member variables.

e) Consider creating an application where you have to print out the weekly schedule of classes for every week - the output would be something like:

Week 1: CPSC 131.01

Week 1: MATH 270A.03

Week 2: CPSC 131.01

Week 2: MATH 270A.03

Week 3: CPSC 131.01

Week 3: MATH 270A.03

...

What data structure would be most appropriate to store the list of courses, a stack or a queue? Do not write code, instead describe the reasoning for your choice in 3-4 sentences.

this was in class arrayStack

// Code based on: // Data Structures and Algorithms in C++, Goodrich, Tamassia, and Mount, 2nd Ed., 2011. // #pragma once #include using namespace std; template class ArrayStack { enum { DEF_CAPACITY = 100 }; // default stack capacity public: ArrayStack(int cap = DEF_CAPACITY); // constructor from capacity int size() const; // number of items in the stack bool empty() const; // is the stack empty? const E& top() const; // get the top element void push(const E& e); // push element onto stack void pop(); // pop the stack void printAll(); // print all elements on stack to cout private: // member data E* S; // array of stack elements int capacity; // stack capacity int t; // index of the top of the stack }; template ArrayStack::ArrayStack(int cap) : S(new E[cap]), capacity(cap), t(-1) { } // constructor from capacity template int ArrayStack::size() const { return (t + 1); } // number of items in the stack template bool ArrayStack::empty() const { return (t < 0); } // is the stack empty? template // return top of stack const E& ArrayStack::top() const { if (empty()) throw length_error("Top of empty stack"); return S[t]; } template // push element onto the stack void ArrayStack::push(const E& e) { if (size() == capacity) throw length_error("Push to full stack"); S[++t] = e; } template // pop the stack void ArrayStack::pop() { if (empty()) throw length_error("Pop from empty stack"); --t; } // print all elements on stack template void ArrayStack::printAll() { if (empty()) throw length_error("Empty stack"); cout << "Elements in stack: "; for (int i = t; i >= 0; i--) cout << "{" << S[i] << "} "; cout << endl; }

this was in class linkedStack

// Code based on: // Data Structures and Algorithms in C++, Goodrich, Tamassia, and Mount, 2nd Ed., 2011. // #pragma once #include #include "SLinkedList.h" using namespace std; template class LinkedStack { // stack as a linked list public: LinkedStack() : stack(), numberOfElements(0) {} // constructor int size() const {return numberOfElements;} // number of items in stack bool empty() const { return numberOfElements == 0; } // is the stack empty? E& top(); // the top element void push(const E& e); // push element onto stack void pop(); // pop the stack void printTop(); // print top element private: // member data SLinkedList stack; // stack of elements int numberOfElements; // number of elements }; // get the top element template E& LinkedStack::top() { if (empty()) throw length_error("Top of empty stack"); return stack.front(); } // push element onto stack template void LinkedStack::push(const E& e) { ++numberOfElements; stack.addFront(e); } // pop the stack template void LinkedStack::pop() { if (empty()) throw length_error("Pop from empty stack"); --numberOfElements; stack.removeFront(); } // print top element on stack template void LinkedStack::printTop() { if (empty()) throw length_error("Empty stack"); cout<< "Top of current stack: {" << stack.front() << "}" << endl; }

this was in class arrayQueue

// Code based on: // Data Structures and Algorithms in C++, Goodrich, Tamassia, and Mount, 2nd Ed., 2011. // #pragma once #include using namespace std; template class ArrayQueue { enum { DEF_CAPACITY = 100 }; // default queue capacity public: ArrayQueue(int cap = DEF_CAPACITY); // constructor from capacity int size() const; // number of items in the stack bool empty() const; // is the stack empty? const E& front() const; // get the top element void enqueue(const E& e); // add to back of queue void dequeue(); // remove from front of queue void printAll(); // print all elements on stack to cout private: // member data E* Q; // array of queue elements int capacity; // queue capacity int f; // index of the front of the queue int r; // index of the rear of the queue int n; // number of elements }; template ArrayQueue::ArrayQueue(int cap) : Q(new E[cap]), capacity(cap), f(0), r(0), n(0) { } // constructor from capacity template int ArrayQueue::size() const { return n; } // number of items in the queue template bool ArrayQueue::empty() const { return (n == 0); } // is the stack empty? template // return element at front of queue const E& ArrayQueue::front() const { if (empty()) throw length_error("front of empty queue"); return Q[f]; } template // insert element to back of queue void ArrayQueue::enqueue(const E& e) { if (size() == capacity) throw length_error("enqueue to full queue"); Q[r] = e; r = (r + 1) % capacity; n++; } template // remove element at front of queue void ArrayQueue::dequeue() { if (empty()) throw length_error("enqueue from empty queue"); f = (f + 1) % capacity; n--; } // print all elements on queue template void ArrayQueue::printAll() { if (empty()) throw length_error("Empty queue"); cout << "Elements in queue: "; int front = f; for (int i = 0; i < n; i++) { cout << "{" << Q[front] << "} "; front = (front + 1) % capacity; } cout << endl; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions

Question

What are the three components of an EDI system?

Answered: 1 week ago

Question

When is it appropriate to use a root cause analysis

Answered: 1 week ago

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago