forked from CSUF-CPSC-131-Fall2019/Data-Structures-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList_main.cpp
More file actions
68 lines (52 loc) · 1.68 KB
/
DoublyLinkedList_main.cpp
File metadata and controls
68 lines (52 loc) · 1.68 KB
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
#include <iostream>
#include <stdexcept>
#include <string>
#include "DoublyLinkedList.hpp"
using std::cout;
using std::string;
using std::to_string;
using std::ostream;
using std::endl;
class Student
{
private:
string name_;
int numOfSemesters_ = 0;
public:
Student() = default;
Student( string name, int nsem = 1 ) : name_( name ), numOfSemesters_( nsem ) {}
void updateNSemesters() { numOfSemesters_++; }
void name( const string & name ) { name_ = name; }
void semesters( int n ) { numOfSemesters_ = n; }
friend ostream& operator<<( ostream& os, const Student& student );
};
ostream& operator<<( ostream& os, const Student& student )
{
os << "Name: " << student.name_;
os << ". No. of semesters= " << student.numOfSemesters_ << endl;
return os;
}
int main() {
DoublyLinkedList<Student> students;
Student s;
for (int i = 0; i < 5; i++) {
s.name("Student_" + to_string(i));
s.semesters(2);
students.prepend(s);
}
DoublyLinkedList<Student> classRoster( students ); // copy constructor
for( int i = 1; i <= 5; i++ )
{
s.name( "Student_" + to_string( i*10 ) );
s.semesters( 2 );
classRoster.append( s );
}
students = classRoster; // assignment operator
cout << classRoster.front() << classRoster.back();
while (!students.empty()) {
cout << students.front();
students.pop_front();
}
}
// template struct Node<Student>;
// template class DoublyLinkedList<Student>; // good idea to explicitly instantiate the templated class to force the compiler to check it