#include<iostream>
#include<string.h>
#include<stdio.h>
#include"modul.h"
using namespace std;
doubly *createList(doubly *L){
L->head = NULL;
L->tail = NULL;
return L;
}
list_mahasiswa *ujung(list_mahasiswa *M){
list_mahasiswa *temp = new list_mahasiswa;
temp = M;
while(temp->next != NULL){
temp = temp->next;
}
return temp;
}
bool cekNIM(doubly *L,char nim[50]){
list_kelas *cKelas = new list_kelas;
list_mahasiswa *cMahasiswa = new list_mahasiswa;
bool status = false;
cKelas = L->head;
while(cKelas != NULL){
cMahasiswa = cKelas->mhs;
while(cMahasiswa != NULL){
if( strcmp(cMahasiswa->info.nim,nim)==0 ){
status = true;
break;
}else{
cMahasiswa = cMahasiswa->next;
}
}
if(status==true){
cKelas = NULL;
}else{
cKelas = cKelas->next;
}
}
return status;
}
void insertKelas(doubly *L,char k[50]){
list_kelas *newKelas = new list_kelas;
strcpy(newKelas->kls,k);
newKelas->mhs = NULL;
newKelas->prev = NULL;
newKelas->next = NULL;
if(L->head == NULL){
L->head = newKelas;
L->tail = newKelas;
cout << "Data kelas berhasil di tambahkan...";
}else{
newKelas->next = L->head;
L->head->prev = newKelas;
L->head = newKelas;
cout << "Data kelas berhasil di tambahkan...";
}
}
void insertMahasiswa(doubly *L,data mhs,char k[50]){
bool ada = false;
list_mahasiswa *newMahasiswa = new list_mahasiswa;
strcpy(newMahasiswa->info.nim,mhs.nim);
strcpy(newMahasiswa->info.nama,mhs.nama);
newMahasiswa->next = NULL;
newMahasiswa->prev = NULL;
list_kelas *current = new list_kelas;
current = L->head;
if(cekNIM(L,mhs.nim) == false){
while(current != NULL){
if(strcmp(current->kls,k)==0){
if(current->mhs == NULL){
current->mhs = newMahasiswa;
cout << "Mahasiswa berhasil ditambahkan..";
ada = true;
break;
}else{
ujung(current->mhs)->next = newMahasiswa;
newMahasiswa->prev = ujung(current->mhs);
cout << "Mahasiswa berhasil ditambahkan..";
ada = true;
break;
}
}else{
current = current->next;
}
}
}else{
cout << "NIM sudah terdaftar...";
ada = true;
}
if(ada == false){
cout << "Kelas belum terdaftar...";
}
}
void deleteMahasiswa(doubly *L,data mhs){
list_kelas *cKelas = new list_kelas;
list_mahasiswa *cMahasiswa = new list_mahasiswa;
list_mahasiswa *temp = new list_mahasiswa;
bool hapus = false;
cKelas = L->head;
while(cKelas != NULL){
cMahasiswa = cKelas->mhs;
temp = cMahasiswa;
while(cMahasiswa != NULL){
if( strcmp(cMahasiswa->info.nim,mhs.nim)==0 ){
if(cMahasiswa->next == NULL){
cMahasiswa->prev->next = NULL;
delete(cMahasiswa);
hapus = true;
cout << "Data mahasiswa sudah dihapus...";
break;
}else{
temp->next = cMahasiswa->next;
delete(cMahasiswa);
hapus = true;
cout << "Data mahasiswa sudah dihapus...";
break;
}
}else{
temp = cMahasiswa;
cMahasiswa = cMahasiswa->next;
}
}
if(hapus==true){
cKelas = NULL;
}else{
cKelas = cKelas->next;
}
}
if(hapus == false){
cout << "Mahasiswa dengan nim "<<mhs.nim<<" tidak terdaftar..."<<endl;
}
}
void viewKelas(doubly *L){
list_kelas *current = new list_kelas;
current = L->head;
int no=1;
while(current != NULL){
cout << "Kelas ke-"<<no<<" : "<<current->kls<<endl;
cout << "Daftar Mahasiswa : "<<endl;
viewMahasiswa(current->mhs);
cout << endl;
no++;
current = current->next;
}
}
void viewMahasiswa(list_mahasiswa *L){
list_mahasiswa *current = new list_mahasiswa;
current = L;
int no=1;
if(current == NULL){
cout << "\tTidak ada mahasiswa.."<<endl;
}else{
while(current != NULL){
cout << "\t Mahasiswa ke-"<<no<<" : "<<current->info.nim<<endl;
no++;
current = current->next;
}
}
}
silahkan di coba
BalasHapus