I understand that it is the responsibility of every member of the Carolina community to uphold an maintain the academic standards and integrity of the University of South Carolina. Any member of the University community, who has reasonable grounds to believe that an infraction of the Code of Student Academic Responsibility has occurred, has an obligation to report the alleged violation.
I certify that I have neither given nor received unauthorized aid on this exam.
O(N2)
10 11 1 2 3 4 5 6 7 8 9 The third element must me either 1 or 10, the fourth element must be 2 or 9, and so on...
#include<vector> using namespace std; template <class T> class Node { T element; Node * next; public: Node(){}; Node(const T & x, Node<T> * n = 0) : element(x), next(n) {}; Node(const Node<T> & n) : element(n.element), next(n.next) {}; ~Node() { delete next;}; }; int main() { Node<int> a(1); Node<int> b(2, &a); Node<int> c(3, &b); } It gives an access violation. If a is deleted first then, when b gets deleted it will try to delete a, which has already been deleted so we get an access violation (or segmentation fault, or whatever...the program crashes. There are two ways to fix it. One way is just to get rid of the delete in the destructor, the other way is to make the copy constructor actually copy the contents of n.next. Either way gets full credit. The best way will depend on how this class will be used. Also, main should return a int. I did not take points for this.
using namespace std; template <class T> class Node { public: T element; Node * next; Node(){}; Node(const T & x, Node<T> * n = 0) : element(x), next(n){}; }; template <class T> class List { Node<T> * root; public: List() : root(0){}; List(Node<T> * r) : root(r) {}; void eliminateNth(int n); // implement this }; template <class T> void List<T>:: eliminateNth(int n){ Node<T> * prev = root; if (n == 0) { //special case, not graded for Node<T> *old = root; root = root->next; delete old; return; } int i = 0; for (Node<T> *nd = root; nd !=0; ++i, prev = nd, nd = nd->next){ if (i == n) {//delete this node prev->next = nd->next; Node<T> *temp = nd->next; delete nd; nd = temp; return; } } } Some of you did not check if n was bigger than the size of the list, or if n==0 (special case). I did not take points off for that.
template <class Etype> class Stack { public: Stack( ); ~Stack( ) { delete [ ] Array; } virtual void Push( const Etype & X ); virtual void Pop( ); virtual const Etype & Top( ) const; virtual int IsEmpty( ) const { return TopOfStack == -1; } virtual int IsFull( ) const { return 0; } void MakeEmpty( ) { TopOfStack = -1; } private: int MaxSize; int TopOfStack; Etype *Array; }; template <class Etype> class FinickyStack { virtual void Push( const Etype & X ) { if (x <= Top()) return; Stack::Push(X); } } Notice that TopOfStack is a private data member so you cannot access it directly.
We need operator*, operator++, and operator!= (or operator==). operator* returns what the iterator is pointing at operator++ increments the iterator and operator!= compares it with some other iterator. (i.e. are we done). Many of you named the iterators used in the book. While I did not take points off for that I would like to remind you that those iterators are only used in that book. The C++ STL names the above iterators as the standard iterators. That is, these are the ones you have been using in all your PSs; these are the ones everyones uses; these are the ones that should be second nature to you.
template <class T> class Node { public: Node * firstChild; Node * nextSibling; T element; Node(const T & e, Node * f, Node * n) : element(e), firstChild(f), nextSibling(n) {}; } int main() { //write statements here int main(){ Node<int> n4(4,0,0); Node<int> n9(9,&n4,0); Node<int> n1(1,0,0); Node<int> n2(2,0,&n7); Node<int> n10(10,0,0); Node<int> n8(8,&n2,&n1); Node<int> n7(7,&n9,0); Node<int> root(1,&n7, 0); }
Post-order means "first handle the children then the node" which leads to: 4 9 7 2 1 8 10 10 I also accepted the incorrect "first handle the data members then the node" which leads to: 4 9 1 2 10 8 7 1
The solutions to this problem and the ones below will be posted outside my door by Monday.