1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#include <iostream>
#include <iomanip>
#include <string>
#include <memory>
using namespace std;
struct Course{
string m_courseName;//e.g. COMSC-165
string m_term;//FA2016
int m_units;
string m_grade;
Course(string courseName, string term, int units, string grade)
: m_courseName (courseName), m_term (term), m_units (units), m_grade (grade) {}
};
ostream& operator << (ostream& os, const Course& c)
{
os << setw(11) << c.m_courseName << setw(8) << c.m_term << setw(8) << c.m_units << setw(8) << c.m_grade << '\n';
return os;
}
struct queue_Node
{
Course m_itsCourse;
shared_ptr<queue_Node> m_itsNext;//not unique_ptr because operator = deleted, used below;
queue_Node (Course itsCourse) : m_itsCourse (itsCourse) {}
};
struct queue_list
{
shared_ptr<queue_Node> rear;
shared_ptr<queue_Node> front;
int m_num_nodes = 0;
queue_list() : rear (nullptr), front (nullptr) {} //this precludes weak_ptr as well;
void queue_enqueue();
void queue_dequeue();
void queue_display ()const;
};
int main()
{
queue_list q;
bool fQuit = false;
while(!fQuit)
{
cout<<"\nEnter your choice:";
cout<<"\n1. ENQUEUE\t2. DEQUEUE\t3. DISPLAY\t4. EXIT\n";
int choice;
cin>>choice;
switch(choice)
{
case 1:
q.queue_enqueue();
break;
case 2:
q.queue_dequeue();
break;
case 3:
q.queue_display();
break;
case 4:
cout << "Goodbye \n";
fQuit = true;
break;
default:
cout<<"Invalid Choice";
break;
}
}
}
void queue_list::queue_enqueue()
{
cout << "Enter course name: \n";
string courseName;
cin >> courseName;
cout << "Enter a term: \n";
string term;
cin >> term;
cout << "Enter number of units: \n";
int units;
cin >> units;
cout << "Enter the grade you received: ('X' if the course is in progress) \n";
string grade;
cin >> grade;
Course c(courseName, term, units, grade);
shared_ptr<queue_Node> p (new queue_Node(c));
p -> m_itsNext = nullptr;
if(front == nullptr)
{
front = p;
}
else
{
rear -> m_itsNext = p;
}
rear = p;
m_num_nodes++;
cout << "New Course inserted \n";
}
void queue_list::queue_dequeue()
{
if (front == nullptr)
{
cout << "Empty queue_list \n";
}
else
{
shared_ptr <queue_Node> temp;
temp = front;
front = front -> m_itsNext;
cout << "Course dequed is: " << temp ->m_itsCourse.m_courseName << '\n';
}
m_num_nodes --;
}
void queue_list::queue_display() const
{
if(front == nullptr)
{
cout << "Empty queue_list \n";
}
else
{
cout << "Number of queue items: " << m_num_nodes << '\n';
cout.setf(ios::left, ios::adjustfield);
cout << setw(11) << "COURSE" << setw(8) << "TERM" << setw(8) << "UNITS" << setw(8) << "GRADE" << '\n';//table header
cout << setw(11) << "______" << setw(8) << "____" << setw(8) << "_____" << setw(8) << "_____" << '\n';//table header
shared_ptr <queue_Node> p = front;
while (p != nullptr)
{
cout << p -> m_itsCourse;
p = p -> m_itsNext;
}
}
}
|