// sort(array, size, currpos): sorts the array positions currpos..size-1 // --------------------------- // if currpos < size-1: // make a recursive call to sort positions currpos+1..size-1 // move the value in array[currpos] into its correct sorted position using // tmp = size-1 // while tmp > currpos AND array[tmp] < array[tmp-1] // swap the contents of array positions tmp and tmp+1 // subtract one from tmp
class linklist {
private:
struct node {
int value;
node* next;
};
node* front;
node* back;
public:
linklist(); // initializes list to null
~linklist(); // deallocates all nodes
// the bool functions return true if they succeed, false otherwise
bool insert(int i); // inserts node with value i at front of list
bool remove(); // removes and deletes element at back of list
bool look(int p, int &i); // lookup value in position p
void reverse(); // reverses order of nodes in the list
// (e.g. 1->3->7->2 would become 2->7->3->1)
};
class datamix {
protected:
char c;
int i;
double d;
public:
datamix();
~datamix();
void print();
};
template <class T>
class lump {
private:
T value1;
T value2;
public:
lump() { }
~lump() { }
void print() { cout << value1 << "," << value2 << endl; }
void set(T v1, T v2) { value1 = v1; value2 = v2; }
void swap() { T tmp = value1; value1 = value2; value2 = tmp; }
};
Assuming all appropriate libraries have been included,
write a main routine that uses lump as follows:
class assignment {
protected:
string Author;
string Assign;
float Value;
float Mark;
public:
assignment();
~assignment();
void print();
void set(string auth="", string asgn="", float val=0, float mk=0);
void get(string &auth, string &asgn, float &val float &mk);
};
class infoexc: public exception{
protected:
string info;
double val;
public:
excinfo(string i="excinfo", double v=0) { info = i; val = v; }
~excinfo() { }
void what() { cout << i << "(" << val << ")"; }
};
Write a code segment (this does not need to be a complete function or program)
that uses try/throws/catches to accomplish the following:
#include <iostream>
#include <string>
using namespace std;
class list {
protected:
struct node {
string data;
node* next;
};
node* front;
node* back;
public:
list() {
front = NULL;
back = NULL;
}
~list() {
while (front) {
node *tmp = front;
front = front->next;
if (front == NULL) back = NULL;
delete tmp;
}
}
string remove() {
string s = "";
if (front) {
s = front->data;
front = front->next;
}
return s;
}
void insert(std::string s) {
node* n = new node;
n->data = s;
n->next = front;
if (!front) back = n;
front = n;
}
void print() {
node *tmp = front;
while (tmp) {
cout << tmp->data << endl;
tmp = tmp->next;
}
}
};
|
class queue: public list {
public:
queue() { }
~queue() { }
void enqueue(string s) {
insert(s);
}
string dequeue() {
return remove();
}
void print() {
cout << "queue: ";
list::print();
}
};
class rotQ: public queue {
public:
rotQ() { }
~rotQ() { }
void rotate() {
if (front) {
string s = remove();
insert(s);
}
}
void print() {
cout << "rot: ";
queue::print();
}
};
void printstuff(list* L) {
if (L) L->print();
}
int main()
{
rotQ R;
list L;
queue Q;
L.insert("L1");
Q.enqueue("Q2");
R.insert("R3");
printstuff(&L);
printstuff(&Q);
printstuff(&R);
}
|
GOOD LUCK WITH THE REST OF YOUR EXAMS, AND HAVE A GREAT SUMMER!