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
|
#include <iostream>
using namespace std;
class Staff {
friend ostream& operator << (ostream& os, Staff& m) {
os << "[" << m.ID << ", " << m.fName << "]";
return os;
}
private :
int ID = 0;
string fName;
public:
Staff() {}
Staff (int ID, string fName) : ID(ID), fName(fName){}
// Staff (string fName) : fName(fName){}
int getID(){return ID;}
string getFName() { return fName; }
void setFName(string fName) {this -> fName = fName;}
friend istream& operator >> (istream& is, Staff& m) {
is >> m.fName;
return is;
}
};
template <typename T>
struct Node {
T info;
Node<T> *next;
};
template <typename T>
class LinkedList {
Node<T> *start;
public:
LinkedList() { start = NULL; }
~LinkedList() {
while (start != NULL){
Node <T> *ptr =start;
start = start -> next;
delete ptr;
}
}
void insertFront (T& newElement) {
Node<T>* newNode = new Node<T>;
newNode->info = newElement;
newNode->next = start;
start = newNode;
}
bool retrieve (int targetindex, T*& retrievedName) {
if (targetindex < 0 || start == NULL)
return false;
Node<T>* ptr = start;
int i = 1;
while (ptr != NULL && i <= targetindex) {
if (i == targetindex) {
retrievedName = &(ptr -> info);
return true;
}
else {
ptr = ptr->next;
++i;
}
}
return false;
}
friend ostream& operator<< (ostream& os, LinkedList<T>& list) {
os << "Staff : \n";
Node<T> *ptr = list.start;
int i = 1;
while (ptr != NULL) {
os << i++ << ". " << ptr->info << endl;
ptr = ptr->next;
}
return os;
}
};
int main() {
LinkedList<Staff> s;
Staff newStaff, *updateName;
int targetIndex;
bool found;
string newStaffName = "Sara";
newStaff = Staff(111, "Elene");
s.insertFront(newStaff);
newStaff = Staff(222, "Mary");
s.insertFront(newStaff);
newStaff = Staff(333, "Jane");
s.insertFront(newStaff);
newStaff = Staff(444, "Eddy");
s.insertFront(newStaff);
newStaff = Staff(555, "Jack");
s.insertFront(newStaff);
cout << s << endl;
cout << "Enter index of staff to be modified : ";
cin >> targetIndex;
found = s.retrieve (targetIndex, updateName);
if (!found)
cout << "No such index\n";
else {
cout << *updateName << " going to be changed\n\n";
*updateName = newStaffName;
// cout << "Enter new name : ";
// cin >> *updateName;
}
cout << s << endl;
}
|