Question: C++ data structure assignment For the Singly-Linked List class at: SLinkedList.h // Code from: // Data Structures and Algorithms in C++, Goodrich, Tamassia, and Mount,
C++ data structure assignment
For the Singly-Linked List class at:
SLinkedList.h
| // Code from: |
| // Data Structures and Algorithms in C++, Goodrich, Tamassia, and Mount, 2nd Ed., 2011. |
| // |
| #pragma once |
| #include |
| using namespace std; |
| template class SLinkedList; // forward declaration to be used when declaring SNode |
| template |
| class SNode { // singly linked list node |
| private: |
| E elem; // linked list element value |
| SNode *next; // next item in the list |
| friend class SLinkedList; // provide SLinkedList access |
| }; |
| template |
| class SLinkedList { // a singly linked list |
| public: |
| SLinkedList(); // empty list constructor |
| ~SLinkedList(); // destructor |
| bool empty() const; // is list empty? |
| E& front(); // return front element |
| void addFront(const E& e); // add to front of list |
| void removeFront(); // remove front item list |
| int size() const; // list size |
| private: |
| SNode* head; // head of the list |
| int n; // number of items |
| }; |
| template |
| SLinkedList::SLinkedList() // constructor |
| : head(NULL), n(0) { } |
| template |
| bool SLinkedList::empty() const // is list empty? |
| { |
| return head == NULL; // can also use return (n == 0); |
| } |
| template |
| E& SLinkedList::front() // return front element |
| { |
| if (empty()) throw length_error("empty list"); |
| return head->elem; |
| } |
| template |
| SLinkedList::~SLinkedList() // destructor |
| { |
| while (!empty()) removeFront(); |
| } |
| template |
| void SLinkedList::addFront(const E& e) { // add to front of list |
| SNode* v = new SNode; // create new node |
| v->elem = e; // store data |
| v->next = head; // head now follows v |
| head = v; // v is now the head |
| n++; |
| } |
| template |
| void SLinkedList::removeFront() { // remove front item |
| if (empty()) throw length_error("empty list"); |
| SNode* old = head; // save current head |
| head = old->next; // skip over old head |
| delete old; // delete the old head |
| n--; |
| } |
| template |
| int SLinkedList::size() const { // list size |
| return n; |
}
Add a method void SLinkedList::printReverse() that prints every element in the linked list in reverse (i.e., the first node is printed last). Do not use recursion but instead use a (array-based) stack as a temporary data structure. An implementation of an array-based stack and an example of how to use it are given here:
ArrayStack.h
| // 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; |
}
ArrayStack_main.cpp
| // |
| // main for ArrayStack |
| // |
| #include |
| #include "ArrayStack.h" |
| int main(int argc, const char * argv[]) { |
| // create a stack |
| ArrayStack intStack; |
| try { |
| intStack.push(5); |
| intStack.push(3); |
| int size = intStack.size(); |
| cout << "Size of the stack now: " << size << endl; |
| intStack.printAll(); |
| intStack.pop(); |
| intStack.printAll(); |
| intStack.push(7); |
| intStack.printAll(); |
| intStack.pop(); |
| intStack.printAll(); |
| cout << "Top of the stack = " << intStack.top() << endl; |
| intStack.printAll(); |
| intStack.pop(); |
| intStack.printAll(); |
| cout << "Top of the stack = " << intStack.top() << endl; |
| intStack.printAll(); |
| } |
| catch (length_error &err) { |
| cout << "Error condition: " << err.what() << endl; |
| } |
| try { |
| intStack.top(); |
| intStack.printAll(); |
| bool isEmpty = intStack.empty(); |
| cout << "Is stack empty (yes=true, no=false)" << isEmpty << endl; |
| } |
| catch (length_error &err) { |
| cout << "Error condition: " << err.what() << endl; |
| } |
| return EXIT_SUCCESS; |
}
Show only this new method:
| #include "ArrayStack.h" template void SLinkedList::printReverse() { // cout elements in reverse } |
May I have screen shot if possible?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
