- Pilih menu dengan mengetikan nomor menu yang telah tersedia. Misalnya: ketikkan nomor “1” untuk meng-Entry Data (Mahasiswa & Buku) kemudian tekan enter.
- Masukkan data-data yang diminta seperti NRP mahasiswa, Nama Mahasiswa, Jumlah buku, dan Kode buku kemudian tekan enter. Tekan tombol enter kembali untuk menampilkan Menu.
- Untuk menampilkan kembali data yang telah diisi ketikkan no “4” kemudian tekan enter
Berikut adalah Source Codenya:
#include
#include
#include
#include
typedef struct type_mhs mhs;
typedef struct type_buku buku;
struct type_mhs
{
int nrp;
char nama[20];
mhs *next;
buku *prev;
};
struct type_buku
{
int kode;
char judul[15];
buku *next;
};
mhs *head, *tail, *baru_mhs, *temp_mhs, *hapus_mhs;
buku *baru_buku, *temp_buku, *hapus_bk;
int temp = 1, valid = 1;
void allocate_mhs(mhs mahasiswa)
{
baru_mhs = (mhs *)malloc(sizeof(mhs));
if(baru_mhs==NULL)
printf("Alokasi Memory Gagal!\n");
else
{
baru_mhs = (mhs *)malloc(sizeof(mhs));
strcpy(baru_mhs->nama, mahasiswa.nama);
baru_mhs->nrp = mahasiswa.nrp;
baru_mhs->next=NULL;
baru_mhs->prev=NULL;
}
}
void allocate_buku(buku book)
{
baru_buku = (buku *)malloc(sizeof(buku));
if(baru_buku==NULL)
printf("Alokasi Memory Gagal!\n");
else
{
baru_buku = (buku *)malloc(sizeof(buku));
baru_buku->kode = book.kode;
strcpy(baru_buku->judul, book.judul);
baru_buku->next = NULL;
}
}
void sisip_awal()
{
baru_mhs->next=head;
head=baru_mhs;
}
void sisip_akhir()
{
tail->next=baru_mhs;
tail=baru_mhs;
}
void sisip_before(int x)
{
temp_mhs=head;
while(temp_mhs->next->nrp!=x)
temp_mhs=temp_mhs->next;
baru_mhs->next=temp_mhs->next;
temp_mhs->next=baru_mhs;
}
void sisip_urut()
{
if(baru_mhs->nrpnrp)
sisip_awal();
else
{
temp_mhs=head;
while(temp_mhs!=NULL)
{
if(baru_mhs->nrp==temp_mhs->nrp)
{
printf("\nNrp %d sudah ada dalam Daftar\n", temp_mhs->nrp);
temp = 0;
break;
}
else if(baru_mhs->nrpnrp)
{
sisip_before(temp_mhs->nrp);
break;
}
else
temp_mhs=temp_mhs->next;
}
if(temp_mhs==NULL)
sisip_akhir();
}
}
void free_x(mhs *x)
{
free(x);
x->next=NULL;
}
void hapus_awal()
{
temp_mhs=head;
head=temp_mhs->next;
free_x(temp_mhs);
}
void hapus_akhir()
{
temp_mhs=head;
while(temp_mhs->next!=tail)
temp_mhs=temp_mhs->next;
tail=temp_mhs;
hapus_mhs=temp_mhs->next;
temp_mhs->next=NULL;
free_x(hapus_mhs);
}
void hapus_after(int x)
{
temp_mhs=head;
while(temp_mhs->next->nrp!=x)
temp_mhs=temp_mhs->next;
hapus_mhs=temp_mhs->next;
temp_mhs->next=hapus_mhs->next;
free_x(hapus_mhs);
}
void hapus_mahasiswa(int x)
{
if(head==tail&&head->nrp==x)
{
temp_mhs=head;
free_x(temp_mhs);
head=tail=NULL;
printf("\nData kosong !!!\n");
valid = 1;
}
else if(head->nrp==x)
hapus_awal();
else
{
if(tail->nrp==x)
hapus_akhir();
else
{
hapus_mhs=head;
while(hapus_mhs!=NULL&&hapus_mhs->nrp!=x)
{
temp_mhs=hapus_mhs;
hapus_mhs=hapus_mhs->next;
}
if(hapus_mhs==NULL)
printf("\n\nNrp %d tidak ada dalam daftar !!!\n\n", x);
else
{
temp_mhs->next=hapus_mhs->next;
free_x(hapus_mhs);
}
}
}
}
void hapus_buku(int nrp)
{
int kode_bk;
char jawab;
buku *temp;
mhs *cari = head;
while(cari != NULL && cari->nrp != nrp)
{
cari = cari->next;
if(cari == NULL)
{
printf("\nNrp %d tidak ada dalam daftar !!!\n\n", nrp);
return;
}
}
hapus_bk = cari->prev;
do
{
hapus_bk = cari->prev;
printf("\tMasukkan Kode buku yang akan dihapus : ");
scanf("%d", &kode_bk);
fflush(stdin);
if(cari->prev->kode == kode_bk)
{
cari->prev = hapus_bk->next;
hapus_bk->next = NULL;
free(hapus_bk);
hapus_bk = NULL;
if(cari->prev == NULL)
{
printf("\nData Kosong !!!\n");
return;
}
else
{
printf("Lagi ? (y/t)");
scanf("%s", &jawab);
fflush(stdin);
puts("");
if(jawab == 'Y' || jawab == 'y')
continue;
else
break;
}
}
while(hapus_bk->kode != kode_bk)
{
temp = hapus_bk;
hapus_bk = hapus_bk->next;
}
temp->next = hapus_bk->next;
hapus_bk->next = NULL;
free(hapus_bk);
hapus_bk = NULL;
printf("Proses Lagi ? (y/t)");
scanf("%s", &jawab);
fflush(stdin);
puts("");
}while(jawab == 'Y' || jawab == 'y');
}
void tampil()
{
temp_mhs=head;
printf("\n****************************");
while(temp_mhs!=NULL)
{
printf("\nNrp : %d\n",temp_mhs->nrp);
printf("Nama : %s\n\n",temp_mhs->nama);
printf("============================\n");
printf("| Kode Buku | Judul Buku |\n");
temp_buku=temp_mhs->prev;
printf("----------------------------\n");
while(temp_buku!=NULL)
{
printf("|%12d|%13s|\n",temp_buku->kode,temp_buku->judul);
temp_buku=temp_buku->next;
}
printf("============================\n");
if(temp_buku==NULL)
temp_mhs=temp_mhs->next;
}
printf("****************************\n");
printf("\n");
}
void main()
{
mhs *mhs_temp;
//buku *book_temp;
mhs mahasiswa;
buku book;
int pil,n, i, nrp, ulang = 1, poin = 1, count;
printf("\t\t\t** PROGRAM PERPUSTAKAAN **\n");
printf("\t\t\t±±±±±±±±±±±±±±±±±±±±±±±±±±\n\n");
while(ulang == 1)
{
temp = 1;
printf("MENU \t : 1. Entry Data. \t 4. Tampilkan Data\n");
printf(" \t 2. Hapus Mahasiswa \t 5. Exit\n");
printf(" \t 3. Hapus Buku\n");
printf("\nInputkan pilihan : ");
scanf("%d",&pil);
fflush(stdin);
if(pil == 1)
{
while(temp == 1){
printf("\nNRP mahasiswa = ");
scanf("%d", &mahasiswa.nrp);
fflush(stdin);
printf("Nama mahasiswa = ");
gets(mahasiswa.nama);
fflush(stdin);
allocate_mhs(mahasiswa);
if(valid == 1)
{
head = tail = baru_mhs;
valid = 0;
}
else
sisip_urut(n);
if(temp == 1)
{
printf("Jumlah buku = ");
scanf("%d", &n);
fflush(stdin);
for(i=1;i<=n;i++) { count = 1; printf("\nBuku ke-%d\n", i); printf("\tKode buku = "); scanf("%d", &book.kode); fflush(stdin); printf("\tNama buku = "); gets(book.judul); fflush(stdin); if(poin == 1) { allocate_buku(book); head->prev=baru_buku;
temp_buku=baru_buku;
poin = 0;
}
else
{
allocate_buku(book);
mhs_temp = head;
//book_temp = mhs_temp->prev;
while(mhs_temp != NULL && /*book_temp*/mhs_temp->prev != NULL)
{
if(/*book_temp->kode*/mhs_temp->prev->kode == baru_buku->kode)
{
printf("\nKode buku sudah ada !!!\n");
count = 0;
break;
}
mhs_temp = mhs_temp->next;
//book_temp = book_temp->next;
}
if(count == 0)
{
i--;
continue;
}
if(i==1)
{
baru_mhs->prev=baru_buku;
temp_buku=baru_buku;
}
else
{
temp_buku->next=baru_buku;
temp_buku=temp_buku->next;
}
}
}
temp = 0;
}
else
{
temp = 1;
continue;
}
}
}
else if(pil == 2)
{
printf("\nNrp Mahasiswa yang ingin dihapus datanya : ");
scanf("%d",&nrp);
fflush(stdin);
hapus_mahasiswa(nrp);
}
else if(pil==3)
{
printf("\nNrp Mahasiswa yang ingin dihapus data bukunya : ");
scanf("%d",&nrp);
fflush(stdin);
hapus_buku(nrp);
}
else if(pil==4)
tampil();
else if(pil==5)
ulang = 0;
else
printf("Pilihan Anda salah!\n");
puts(" ");
}
}
#include
#include
#include
#include
typedef struct type_mhs mhs;
typedef struct type_buku buku;
struct type_mhs
{
int nrp;
char nama[20];
mhs *next;
buku *prev;
};
struct type_buku
{
int kode;
char judul[15];
buku *next;
};
mhs *head, *tail, *baru_mhs, *temp_mhs, *hapus_mhs;
buku *baru_buku, *temp_buku, *hapus_bk;
int temp = 1, valid = 1;
void allocate_mhs(mhs mahasiswa)
{
baru_mhs = (mhs *)malloc(sizeof(mhs));
if(baru_mhs==NULL)
printf("Alokasi Memory Gagal!\n");
else
{
baru_mhs = (mhs *)malloc(sizeof(mhs));
strcpy(baru_mhs->nama, mahasiswa.nama);
baru_mhs->nrp = mahasiswa.nrp;
baru_mhs->next=NULL;
baru_mhs->prev=NULL;
}
}
void allocate_buku(buku book)
{
baru_buku = (buku *)malloc(sizeof(buku));
if(baru_buku==NULL)
printf("Alokasi Memory Gagal!\n");
else
{
baru_buku = (buku *)malloc(sizeof(buku));
baru_buku->kode = book.kode;
strcpy(baru_buku->judul, book.judul);
baru_buku->next = NULL;
}
}
void sisip_awal()
{
baru_mhs->next=head;
head=baru_mhs;
}
void sisip_akhir()
{
tail->next=baru_mhs;
tail=baru_mhs;
}
void sisip_before(int x)
{
temp_mhs=head;
while(temp_mhs->next->nrp!=x)
temp_mhs=temp_mhs->next;
baru_mhs->next=temp_mhs->next;
temp_mhs->next=baru_mhs;
}
void sisip_urut()
{
if(baru_mhs->nrp
sisip_awal();
else
{
temp_mhs=head;
while(temp_mhs!=NULL)
{
if(baru_mhs->nrp==temp_mhs->nrp)
{
printf("\nNrp %d sudah ada dalam Daftar\n", temp_mhs->nrp);
temp = 0;
break;
}
else if(baru_mhs->nrp
{
sisip_before(temp_mhs->nrp);
break;
}
else
temp_mhs=temp_mhs->next;
}
if(temp_mhs==NULL)
sisip_akhir();
}
}
void free_x(mhs *x)
{
free(x);
x->next=NULL;
}
void hapus_awal()
{
temp_mhs=head;
head=temp_mhs->next;
free_x(temp_mhs);
}
void hapus_akhir()
{
temp_mhs=head;
while(temp_mhs->next!=tail)
temp_mhs=temp_mhs->next;
tail=temp_mhs;
hapus_mhs=temp_mhs->next;
temp_mhs->next=NULL;
free_x(hapus_mhs);
}
void hapus_after(int x)
{
temp_mhs=head;
while(temp_mhs->next->nrp!=x)
temp_mhs=temp_mhs->next;
hapus_mhs=temp_mhs->next;
temp_mhs->next=hapus_mhs->next;
free_x(hapus_mhs);
}
void hapus_mahasiswa(int x)
{
if(head==tail&&head->nrp==x)
{
temp_mhs=head;
free_x(temp_mhs);
head=tail=NULL;
printf("\nData kosong !!!\n");
valid = 1;
}
else if(head->nrp==x)
hapus_awal();
else
{
if(tail->nrp==x)
hapus_akhir();
else
{
hapus_mhs=head;
while(hapus_mhs!=NULL&&hapus_mhs->nrp!=x)
{
temp_mhs=hapus_mhs;
hapus_mhs=hapus_mhs->next;
}
if(hapus_mhs==NULL)
printf("\n\nNrp %d tidak ada dalam daftar !!!\n\n", x);
else
{
temp_mhs->next=hapus_mhs->next;
free_x(hapus_mhs);
}
}
}
}
void hapus_buku(int nrp)
{
int kode_bk;
char jawab;
buku *temp;
mhs *cari = head;
while(cari != NULL && cari->nrp != nrp)
{
cari = cari->next;
if(cari == NULL)
{
printf("\nNrp %d tidak ada dalam daftar !!!\n\n", nrp);
return;
}
}
hapus_bk = cari->prev;
do
{
hapus_bk = cari->prev;
printf("\tMasukkan Kode buku yang akan dihapus : ");
scanf("%d", &kode_bk);
fflush(stdin);
if(cari->prev->kode == kode_bk)
{
cari->prev = hapus_bk->next;
hapus_bk->next = NULL;
free(hapus_bk);
hapus_bk = NULL;
if(cari->prev == NULL)
{
printf("\nData Kosong !!!\n");
return;
}
else
{
printf("Lagi ? (y/t)");
scanf("%s", &jawab);
fflush(stdin);
puts("");
if(jawab == 'Y' || jawab == 'y')
continue;
else
break;
}
}
while(hapus_bk->kode != kode_bk)
{
temp = hapus_bk;
hapus_bk = hapus_bk->next;
}
temp->next = hapus_bk->next;
hapus_bk->next = NULL;
free(hapus_bk);
hapus_bk = NULL;
printf("Proses Lagi ? (y/t)");
scanf("%s", &jawab);
fflush(stdin);
puts("");
}while(jawab == 'Y' || jawab == 'y');
}
void tampil()
{
temp_mhs=head;
printf("\n****************************");
while(temp_mhs!=NULL)
{
printf("\nNrp : %d\n",temp_mhs->nrp);
printf("Nama : %s\n\n",temp_mhs->nama);
printf("============================\n");
printf("| Kode Buku | Judul Buku |\n");
temp_buku=temp_mhs->prev;
printf("----------------------------\n");
while(temp_buku!=NULL)
{
printf("|%12d|%13s|\n",temp_buku->kode,temp_buku->judul);
temp_buku=temp_buku->next;
}
printf("============================\n");
if(temp_buku==NULL)
temp_mhs=temp_mhs->next;
}
printf("****************************\n");
printf("\n");
}
void main()
{
mhs *mhs_temp;
//buku *book_temp;
mhs mahasiswa;
buku book;
int pil,n, i, nrp, ulang = 1, poin = 1, count;
printf("\t\t\t** PROGRAM PERPUSTAKAAN **\n");
printf("\t\t\t±±±±±±±±±±±±±±±±±±±±±±±±±±\n\n");
while(ulang == 1)
{
temp = 1;
printf("MENU \t : 1. Entry Data. \t 4. Tampilkan Data\n");
printf(" \t 2. Hapus Mahasiswa \t 5. Exit\n");
printf(" \t 3. Hapus Buku\n");
printf("\nInputkan pilihan : ");
scanf("%d",&pil);
fflush(stdin);
if(pil == 1)
{
while(temp == 1){
printf("\nNRP mahasiswa = ");
scanf("%d", &mahasiswa.nrp);
fflush(stdin);
printf("Nama mahasiswa = ");
gets(mahasiswa.nama);
fflush(stdin);
allocate_mhs(mahasiswa);
if(valid == 1)
{
head = tail = baru_mhs;
valid = 0;
}
else
sisip_urut(n);
if(temp == 1)
{
printf("Jumlah buku = ");
scanf("%d", &n);
fflush(stdin);
for(i=1;i<=n;i++) { count = 1; printf("\nBuku ke-%d\n", i); printf("\tKode buku = "); scanf("%d", &book.kode); fflush(stdin); printf("\tNama buku = "); gets(book.judul); fflush(stdin); if(poin == 1) { allocate_buku(book); head->prev=baru_buku;
temp_buku=baru_buku;
poin = 0;
}
else
{
allocate_buku(book);
mhs_temp = head;
//book_temp = mhs_temp->prev;
while(mhs_temp != NULL && /*book_temp*/mhs_temp->prev != NULL)
{
if(/*book_temp->kode*/mhs_temp->prev->kode == baru_buku->kode)
{
printf("\nKode buku sudah ada !!!\n");
count = 0;
break;
}
mhs_temp = mhs_temp->next;
//book_temp = book_temp->next;
}
if(count == 0)
{
i--;
continue;
}
if(i==1)
{
baru_mhs->prev=baru_buku;
temp_buku=baru_buku;
}
else
{
temp_buku->next=baru_buku;
temp_buku=temp_buku->next;
}
}
}
temp = 0;
}
else
{
temp = 1;
continue;
}
}
}
else if(pil == 2)
{
printf("\nNrp Mahasiswa yang ingin dihapus datanya : ");
scanf("%d",&nrp);
fflush(stdin);
hapus_mahasiswa(nrp);
}
else if(pil==3)
{
printf("\nNrp Mahasiswa yang ingin dihapus data bukunya : ");
scanf("%d",&nrp);
fflush(stdin);
hapus_buku(nrp);
}
else if(pil==4)
tampil();
else if(pil==5)
ulang = 0;
else
printf("Pilihan Anda salah!\n");
puts(" ");
}
}
Tidak ada komentar:
Posting Komentar