C++ data structure assignment
For the Singly-Linked List class at:
SLinkedList.h
// Data Structures and Algorithms in C++, Goodrich, Tamassia, and Mount, 2nd Ed., 2011. |
template class SLinkedList; // forward declaration to be used when declaring SNode |
class SNode { // singly linked list node |
E elem; // linked list element value |
SNode *next; // next item in the list |
friend class SLinkedList; // provide SLinkedList access |
class SLinkedList { // a singly linked list |
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 |
SNode* head; // head of the list |
int n; // number of items |
SLinkedList::SLinkedList() // constructor |
bool SLinkedList::empty() const // is list empty? |
return head == NULL; // can also use return (n == 0); |
E& SLinkedList::front() // return front element |
if (empty()) throw length_error("empty list"); |
SLinkedList::~SLinkedList() // destructor |
while (!empty()) removeFront(); |
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 |
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 |
int SLinkedList::size() const { // list size |
}
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
// Data Structures and Algorithms in C++, Goodrich, Tamassia, and Mount, 2nd Ed., 2011. |
enum { DEF_CAPACITY = 100 }; // default stack capacity |
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 |
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 |
} // number of items in the stack |
template bool ArrayStack::empty() const |
template // return top of stack |
const E& ArrayStack::top() const { |
if (empty()) throw length_error("Top of empty stack"); |
template // push element onto the stack |
void ArrayStack::push(const E& e) { |
if (size() == capacity) throw length_error("Push to full stack"); |
template // pop the stack |
if (empty()) throw length_error("Pop from empty stack"); |
// print all elements on stack |
void ArrayStack::printAll() { |
if (empty()) throw length_error("Empty stack"); |
cout << "Elements in stack: "; |
for (int i = t; i >= 0; i--) |
cout << "{" << S[i] << "} "; |
}
ArrayStack_main.cpp
int main(int argc, const char * argv[]) { |
int size = intStack.size(); |
cout << "Size of the stack now: " << size << endl; |
cout << "Top of the stack = " << intStack.top() << endl; |
cout << "Top of the stack = " << intStack.top() << endl; |
catch (length_error &err) { |
cout << "Error condition: " << err.what() << endl; |
bool isEmpty = intStack.empty(); |
cout << "Is stack empty (yes=true, no=false)" << isEmpty << endl; |
catch (length_error &err) { |
cout << "Error condition: " << err.what() << endl; |
}
Show only this new method:
#include "ArrayStack.h" template void SLinkedList::printReverse() { // cout elements in reverse } |
May I have screen shot if possible?