diff --git a/list.cpp b/list.cpp index fe0655c..03664cf 100644 --- a/list.cpp +++ b/list.cpp @@ -7,6 +7,8 @@ void createList(List &L) { */ //-------------your code here------------- your code here + first(L) = NULL; + last(L) == NULL; //---------------------------------------- @@ -20,6 +22,13 @@ address allocate(infotype x) { address P; //-------------your code here------------- your code here + P = new elmlist; + info(P).ID = x.ID; + info(P).name = x.name; + info(P).rank = x.rank; + info(P).score = x.score; + next(P) = NULL; + prev(P) = NULL; //---------------------------------------- @@ -32,6 +41,7 @@ void deallocate(address &P) { */ //-------------your code here------------- your code here + delete P; //---------------------------------------- @@ -44,6 +54,14 @@ void insertFirst(List &L, address P) { */ //-------------your code here------------- your code here + if (first(L) != NULL) { + next(P) = first(L); + prev(first(L)) = P; + first(L) = P; + } else { + first(L) = P; + last(L) = P; + } //---------------------------------------- @@ -56,6 +74,9 @@ void insertLast(List &L, address P) { */ //-------------your code here------------- your code here + next(last(L)) = P; + prev(P) = last(L); + last(L) = P; //---------------------------------------- @@ -71,6 +92,10 @@ address findElm(List L, infotype x) { address P; //-------------your code here------------- your code here + P = first(L); + while (P != NULL && info(P).ID != x.ID) { + P = next(P); + } //---------------------------------------- @@ -84,6 +109,16 @@ void deleteFirst(List &L, address &P) { */ //-------------your code here------------- your code here + if (first(L) == last(L)) { + P = first(L); + first(L) = NULL; + last(L) = NULL; + } else if (first(L) != NULL) { + P = first(L); + first(L) = next(P); + next(P) = NULL; + prev(first(L)) = NULL; + } @@ -97,6 +132,16 @@ void deleteLast(List &L, address &P) { */ //-------------your code here------------- your code here + if (last(L) != NULL) { + P = last(L); + last(L) = prev(P); + prev(P) = NULL; + next(last(L)) = NULL; + } else if (first(L) == last(L)) { + P = first(L); + first(L) = NULL; + last(L) = NULL; + } @@ -110,6 +155,12 @@ void printInfo(List L) { */ //-------------your code here------------- your code here + address P = first(L); + while (P != NULL) { + view_data(info(P)); + P = next(P); + } + cout << endl; //---------------------------------------- @@ -124,6 +175,16 @@ void insertAfter(List &L, address Prec, address P) { */ //-------------your code here------------- your code here + if (first(L) == NULL) { + insertFirst(L, P); + } else if (first(L) != last(L) && Prec != NULL) { + next(P) = next(Prec); + P -> prev = Prec; + prev(next(Prec)) = P; + next(Prec) = P; + } else if (Prec != NULL) { + insertLast(L, P); + } //---------------------------------------- @@ -136,6 +197,17 @@ void deleteAfter(List &L, address Prec, address &P) { */ //-------------your code here------------- your code here + if (first(L) == last(L) && Prec != NULL) { + deleteFirst(L, P); + } else if (next(Prec) != last(L) && Prec != NULL) { + P = next(Prec); + next(Prec) = next(P); + prev(next(P)) = Prec; + next(P) = NULL; + prev(P) = NULL; + } else if (Prec != NULL) { + deleteLast(L, P); + } //---------------------------------------- diff --git a/list.h b/list.h index c21344f..ec2a3ce 100644 --- a/list.h +++ b/list.h @@ -37,12 +37,17 @@ typedef struct elmlist *address; struct elmlist{ //------------- your code here ----------- + infotype info; + address next; + address prev; //---------------------------------------- }; struct List{ //------------- your code here ----------- + address first; + address last; //---------------------------------------- diff --git a/main.cpp b/main.cpp index 9e0b483..70ebace 100644 --- a/main.cpp +++ b/main.cpp @@ -53,6 +53,56 @@ void mainMenu() { P = allocate(X); insertFirst(L,P) break; + case 1: + X = create_data(); + P = allocate(X); + insertFirst(L,P) + if (findElm(L, info(P)) == NULL) { + insertFirst(L, P); + } + break; + case 2: + printInfo(L); + break; + case 3: + cout << "Masukkan ID yang ingin dicari : "; + cin >> X.ID; + P = findElm(L, X); + if (P != NULL) { + view_data(info(P)); + } else { + cout << "DATA TIDAK DITEMUKAN!\n"; + } + break; + case 4: + cout << "Masukkan ID yang ingin diedit : "; + cin >> X.ID; + P = findElm(L, X); + if (P != NULL) { + edit_data(info(P)); + } else { + cout << "DATA TIDAK DITEMUKAN!\n"; + } + break; + case 5: + cout << "Masukkan ID yang ingin didelete : "; + cin >> X.ID; + if (findElm(L, X) != NULL) { + deletebyID(L, X.ID); + } else { + cout << "DATA TIDAK DITEMUKAN" << endl; + } + break; + case 6: + savePassedMember(L, L_passed); + break; + case 7: + printInfo(L_passed); + break; + } + if (choice == 0) { + break; + } } } while(true); diff --git a/my_data.cpp b/my_data.cpp index 68b9d77..26394ed 100644 --- a/my_data.cpp +++ b/my_data.cpp @@ -2,9 +2,9 @@ #include "my_data.h" /** - CLASS : - NAME : - STUDENT ID : + CLASS :IF-43-05 + NAME :Muhammad Ghifari Adrian + STUDENT ID :1301194034 **/ mytype create_data() { @@ -16,6 +16,15 @@ mytype create_data() { // =========================== // YOUR CODE HERE your code here + cout << "Masukkan ID : "; + cin >> d.ID; + cout << "Masukkan Nama : "; + cin >> d.name; + cout << "Masukkan ranking : "; + cin >> d.rank; + cout << "Masukkan score : "; + cin >> d.score; + @@ -33,6 +42,7 @@ void view_data(mytype d) { // =========================== // YOUR CODE HERE your code here + cout << d.ID << " - " << d.name << " - "<< d.rank << " - " << d.score << endl; @@ -51,6 +61,12 @@ void edit_data(mytype &d) { // =========================== // YOUR CODE HERE your code here + cout << "Masukkan pergantian nama : "; + cin >> d.name; + cout << "Masukkan pergantian rank : "; + cin >> d.rank; + cout << "Masukkan pergantian score : "; + cin >> d.score; diff --git a/operation.cpp b/operation.cpp index c2dc0b0..77f00b4 100644 --- a/operation.cpp +++ b/operation.cpp @@ -15,6 +15,17 @@ void insertAndSort(List &L, infotype x) { //-------------your code here------------- your code here + address p, p1; + p = first(L); + if (p == NULL || info(p).ID > x.ID) { + insertFirst(L, allocate(x)); + } else if (findElm(L, x) == NULL) { + while (p != NULL && info(p).ID < x.ID) { + p1 = p; + p = next(p); + } + insertAfter(L, p1, allocate(x)); + } //---------------------------------------- @@ -30,6 +41,17 @@ void deletebyID(List &L, int id_x) { address Prec, P; //-------------your code here------------- your code here + P = first(L); + if (P == first(L) && id_x == info(P).ID) { + deleteFirst(L, P); + } else { + while (P != NULL) { + if (id_x == info(P).ID && first(L) != last(L)) { + deleteAfter(L, prev(P), P); + } + P = next(P); + } + } //---------------------------------------- @@ -44,6 +66,22 @@ void savePassedMember(List &L, List &L2){ address P; //-------------your code here------------- your code here + P = first(L); + address Q = P; + while (Q != NULL) { + P = Q; + if (info(P).score > 80) { + insertAndSort(L2, info(P)); + Q = next(Q); + if (prev(P) == NULL) { + deleteFirst(L, P); + } else { + deleteAfter(L, prev(P), P); + } + } else { + Q = next(Q); + } + } //----------------------------------------