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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
#include<iostream>
#include<iomanip>
#include<cctype>
using namespace std;
struct Node{
int id;
string fname, lname;
Node *next;
Node *data;
};
typedef Node* NodePtr;
void displayList(NodePtr head){ //displays the list
NodePtr p;
p = head;
while(p != NULL){
cout << "<" << p->id << ">" << " " << p->fname << " " << p->lname << endl;
p = p->next;
}
}
void addHead(NodePtr& head, Node newdata){ //add node at head
NodePtr newPtr = new Node;
newPtr->id = newdata.id;
newPtr->fname = newdata.fname;
newPtr->lname = newdata.lname;
newPtr->next = head;
head = newPtr;
}
void addMiddle(NodePtr& p, Node newdata){ //add node at middle
NodePtr newPtr = new Node;
newPtr->id = newdata.id;
newPtr->fname = newdata.fname;
newPtr->lname = newdata.lname;
newPtr->next = p->next;
p->next = newPtr;
}
void addEnd(NodePtr& head, Node newdata){ //add node at end
NodePtr newPtr = new Node;
newPtr->id = newdata.id;
newPtr->fname = newdata.fname;
newPtr->lname = newdata.lname;
newPtr->next = NULL;
NodePtr last = head;
if(last != NULL){
while(last->next != NULL)
last=last->next;
last->next = newPtr;
}
else
head = newPtr;
}
NodePtr searchNode(NodePtr head, int item){ //search for nodes
NodePtr p = head;
NodePtr result = NULL;
bool found=false;
while((p != NULL) && (!found)){
if(p->id == item) {
found = true;
result = p;
}
p = p->next;
}
return result;
}
void deleteNode(NodePtr* p){ //delete node
NodePtr head, data;
if(head == NULL){
cout << "Node can't be deleted from an empty record." << endl;
}
else{
NodePtr curr = head;
NodePtr prev = NULL;
while (curr != NULL){
if(curr->data == data) break;
else{
prev = curr;
curr = curr->next;
}
}
if (curr == 0){
cout << "Record not found. " << endl;
}
else{
if (head == curr){
head = head->next;
}
else{
prev->next = curr->next;
}
delete curr;
}
}
}
void updateNode(NodePtr p, Node newdata){ //update nodes
NodePtr id, fname, lname;
p->id = newdata.id;
p->fname = newdata.fname;
p->lname = newdata.lname;
}
void updateId(NodePtr p, Node newdata){ //update ID
NodePtr id;
p->id = newdata.id;
}
void updateFname(NodePtr p, Node newdata){ //update First name
NodePtr fname;
p->fname = newdata.fname;
}
void updateLname(NodePtr p, Node newdata){ //update Last name
NodePtr lname;
p->lname = newdata.lname;
}
Node getId(){
Node data;
cout << "Enter new ID: " << endl;
cin >> data.id;
return data;
}
Node getFname(){
Node data;
cout << "Enter new Firstname: " << endl;
cin >> data.fname;
return data;
}
Node getLname(){
Node data;
cout << "Enter new Lastname: " << endl;
cin >> data.lname;
return data;
}
Node getData(){
Node data;
cout << endl;
cout << "Enter ID: ";
cin >> data.id;
cout << "Enter First Name: ";
cin >> data.fname;
cout << "Enter Last Name: ";
cin >> data.lname;
cout << endl;
return data;
cout << endl;
}
main(){
NodePtr head, p;
Node data;
int opt, insert, upopt;
head = p = 0;
do{
cout << setw(25) << "STUDENTS RECORD" << endl
<< "--------------------------------------" << endl //main menu of the program
<< "|[1] Add data to the record |" << endl
<< "|[2] View record |" << endl
<< "|[3] Delete data |" << endl
<< "|[4] Update the record |" << endl
<< "|[0] Exit program |" << endl
<< "--------------------------------------" << endl
<< "Choose: ";
cin >> opt;
if (opt == 1){
cout << setw(25) << "STUDENTS RECORD" << endl //asks the user where to insert
<< "--------------------------------------" << endl
<< "|[1] Add record at head |" << endl
<< "|[2] Add record at middle |" << endl
<< "|[3] Add record at tail |" << endl
<< "--------------------------------------" << endl
<< "Choose: ";
cin >> insert;
switch(insert){
case 1:
data = getData();
addHead(head,data);
break;
case 2:
displayList(head);
cout << "Insert after what ID: ";
cin >> opt;
p = searchNode(head,opt);
if(p == NULL){
cout << "Sorry, ID not found!" << endl;
}
else {
data = getData();
addMiddle(p,data);
}
break;
case 3:
data = getData();
addEnd(head,data);
break;
default: cout << "No such option." << endl;
}
}
if (opt != 1){
switch(opt){
case 2: displayList(head); break;
case 3:
displayList(head);
cout << "Delete what ID: " << endl;
cin >> opt;
p = searchNode(head, opt);
if (p == NULL){
cout << "ID not found on the list. " << endl;
}
else
deleteNode(data);
case 4:
displayList(head);
cout << "Update what Node: ";
cin >> opt;
p = searchNode(head, opt);
cout << endl;
if (p == NULL){
cout << "ID not found on the list. " << endl;
}
else {
cout << "Update" << endl
<< "[1] ID" << endl
<< "[2] Firstname" << endl
<< "[3] Lastname" << endl
<< "[4] All" << endl
<< "Choice: ";
cin >> upopt;
if(upopt == 1){
data = getId();
updateId(p, data);
}
else if (upopt == 2){
data = getFname();
updateFname(p, data);
}
else if (upopt == 3){
data = getLname();
updateLname(p, data);
}
else if (upopt == 4){
data = getData();
updateNode(p, data);
}
else cout << "No such option! " << endl;
}
case 0: break;
default: cout << "No such option!" << endl;
}
}
} while (opt != 0);
delete head, p;
system("pause");
}
|