I haven't been able to do algoritm, so i find out from people blog.
i will share what i take, check it out
single linked list
#include "stdio.h"
#include "stdlib.h"
#include "iostream.h"
struct node
{
int no;
char nama[10];
float datang, keluar;
struct node *next;
};
typedef struct node *simpul; char nama[10];
void main()
{
simpul baru, head=NULL, tail=NULL, temp;
int pilih;
do {
printf("**************************\n");
printf("* MENU NASABAH *\n");
printf("* 1. Insert Data *\n");
printf("* 2. View Data *\n");
printf("* 3. Search Data *\n");
printf("* 4. Delete Data *\n");
printf("* 5. Update Data *\n");
printf("* 6. Exit Program *\n");
printf("**************************\n");
printf(" Silahkan Anda Pilih: ");
scanf("%d", &pilih);
printf("__________________________\n");
switch(pilih)
{case 1:
char OK;
do {
baru = (simpul) malloc(sizeof (struct node));
printf("Masukkan Nomor Nasabah: ");
scanf("%d", &baru->no);
fflush(stdin);
printf("Masukkan Nama: ");
gets(baru->nama);
fflush(stdin);
printf("Masukkan Waktu Kedatangan: ");
scanf("%g",&baru->datang);
fflush(stdin);
printf("Masukkan Waktu Keluar: ");
scanf("%g",&baru->keluar);
fflush(stdin);
baru->next = NULL;
if (head == NULL)
{ head = baru; tail = baru; }
else
{ tail->next = baru; tail = baru; }
cout<<"Apakah Anda Ingin Mengulang Meng-Insert Data (y/n) ? ";
cin>>OK;
cout<<endl; }
while(OK=='y');
break;
case 2: do { int a;
printf("tampilkan secara\n1. maju\nmasukkan pilihan:");
scanf("%d",&a);
if(a==1)
{ temp = head;
while(temp!=NULL)
{ printf("\n%d ", temp->no);
printf("%s\n",temp->nama);
printf("%g\n",temp->datang);
printf("%g\n",temp->keluar);
temp = temp->next; } printf("\n"); }
cout<<"Apakah Anda Ingin Mengulang Meng-View Data (y/n) ? ";
cin>>OK;
cout<<endl; }
while(OK=='y');
break;
case 3: do { int cari,a;
printf("Cari Angka: "); scanf("%d", &cari);
printf("pencarian secara\n1. maju\nmasukkan pilihan:");
scanf("%d",&a);
if(a==1)
{ temp = head;
while((temp!=NULL)&&(temp->no!=cari))
{ temp = temp->next; }
if(temp != NULL && temp->no == cari)
printf("SELAMAT...!!! Data Ditemukan\n");
else
printf("MAAF...!!! Data Tidak Ditemukan\n");
printf("\n"); }
cout<<"Apakah Anda Ingin Mengulang Meng-Search Data (y/n) ? ";
cin>>OK;
cout<<endl; }
while(OK=='y');
break;
case 4: do { int hapus; char jwb; simpul next = NULL;
printf("Hapus Nasabah: "); scanf("%d", &hapus); temp = head;
while((temp!=NULL)&&(temp->no!=hapus))
{ temp = temp->next; }
if(temp != NULL && temp->no == hapus)
{ cout<<"Yakin Dihapus? (y/t)"; cin>>jwb;
if(jwb == 'y')
{ if(temp->next != NULL )
{ temp->next; }
else if (temp == head && head->next == NULL)
{ head = NULL; }
else if (temp == head && head->next != NULL)
{ (head->next)->next=NULL; head = head->next; }
free(temp); }
else
printf("OK...!?!!?!, Data Batal Dihapus");}
else
printf("MAAF...!!! Data Tidak Ditemukan\n\n\n");
cout<<"Apakah Anda Ingin Mengulang Meng-Delete Data (y/n) ? ";
cin>>OK;
cout<<endl; }
while(OK=='y');
break;
case 5:
do
{
//prev=NULL;
int cari;
printf("Cari No Nasabah: ");
scanf("%d", &cari);
temp = head;
while((temp!=NULL)&&(temp->no!=cari))
{
temp = temp->next;
}
if(temp != NULL && temp->no == cari)
{
printf("masukkan no baru = ");
scanf("%d",&temp->no);
printf("masukkan nama baru = ");
scanf("%s",&temp->nama);
printf("Masukkan Waktu Kedatangan: ");
scanf("%g",&baru->datang);
printf("Masukkan Waktu Keluar: ");
scanf("%g",&baru->keluar);
}
else
printf("MAAF...!!! Data Tidak Ditemukan\n");
cout<<"Apakah Anda Ingin Mengulang Meng-Update Data (y/n) ? ";
cin>>OK;
cout<<endl;
}
while(OK=='y');
break;
}
}while (pilih!=6); }
#include "stdlib.h"
#include "iostream.h"
struct node
{
int no;
char nama[10];
float datang, keluar;
struct node *next;
};
typedef struct node *simpul; char nama[10];
void main()
{
simpul baru, head=NULL, tail=NULL, temp;
int pilih;
do {
printf("**************************\n");
printf("* MENU NASABAH *\n");
printf("* 1. Insert Data *\n");
printf("* 2. View Data *\n");
printf("* 3. Search Data *\n");
printf("* 4. Delete Data *\n");
printf("* 5. Update Data *\n");
printf("* 6. Exit Program *\n");
printf("**************************\n");
printf(" Silahkan Anda Pilih: ");
scanf("%d", &pilih);
printf("__________________________\n");
switch(pilih)
{case 1:
char OK;
do {
baru = (simpul) malloc(sizeof (struct node));
printf("Masukkan Nomor Nasabah: ");
scanf("%d", &baru->no);
fflush(stdin);
printf("Masukkan Nama: ");
gets(baru->nama);
fflush(stdin);
printf("Masukkan Waktu Kedatangan: ");
scanf("%g",&baru->datang);
fflush(stdin);
printf("Masukkan Waktu Keluar: ");
scanf("%g",&baru->keluar);
fflush(stdin);
baru->next = NULL;
if (head == NULL)
{ head = baru; tail = baru; }
else
{ tail->next = baru; tail = baru; }
cout<<"Apakah Anda Ingin Mengulang Meng-Insert Data (y/n) ? ";
cin>>OK;
cout<<endl; }
while(OK=='y');
break;
case 2: do { int a;
printf("tampilkan secara\n1. maju\nmasukkan pilihan:");
scanf("%d",&a);
if(a==1)
{ temp = head;
while(temp!=NULL)
{ printf("\n%d ", temp->no);
printf("%s\n",temp->nama);
printf("%g\n",temp->datang);
printf("%g\n",temp->keluar);
temp = temp->next; } printf("\n"); }
cout<<"Apakah Anda Ingin Mengulang Meng-View Data (y/n) ? ";
cin>>OK;
cout<<endl; }
while(OK=='y');
break;
case 3: do { int cari,a;
printf("Cari Angka: "); scanf("%d", &cari);
printf("pencarian secara\n1. maju\nmasukkan pilihan:");
scanf("%d",&a);
if(a==1)
{ temp = head;
while((temp!=NULL)&&(temp->no!=cari))
{ temp = temp->next; }
if(temp != NULL && temp->no == cari)
printf("SELAMAT...!!! Data Ditemukan\n");
else
printf("MAAF...!!! Data Tidak Ditemukan\n");
printf("\n"); }
cout<<"Apakah Anda Ingin Mengulang Meng-Search Data (y/n) ? ";
cin>>OK;
cout<<endl; }
while(OK=='y');
break;
case 4: do { int hapus; char jwb; simpul next = NULL;
printf("Hapus Nasabah: "); scanf("%d", &hapus); temp = head;
while((temp!=NULL)&&(temp->no!=hapus))
{ temp = temp->next; }
if(temp != NULL && temp->no == hapus)
{ cout<<"Yakin Dihapus? (y/t)"; cin>>jwb;
if(jwb == 'y')
{ if(temp->next != NULL )
{ temp->next; }
else if (temp == head && head->next == NULL)
{ head = NULL; }
else if (temp == head && head->next != NULL)
{ (head->next)->next=NULL; head = head->next; }
free(temp); }
else
printf("OK...!?!!?!, Data Batal Dihapus");}
else
printf("MAAF...!!! Data Tidak Ditemukan\n\n\n");
cout<<"Apakah Anda Ingin Mengulang Meng-Delete Data (y/n) ? ";
cin>>OK;
cout<<endl; }
while(OK=='y');
break;
case 5:
do
{
//prev=NULL;
int cari;
printf("Cari No Nasabah: ");
scanf("%d", &cari);
temp = head;
while((temp!=NULL)&&(temp->no!=cari))
{
temp = temp->next;
}
if(temp != NULL && temp->no == cari)
{
printf("masukkan no baru = ");
scanf("%d",&temp->no);
printf("masukkan nama baru = ");
scanf("%s",&temp->nama);
printf("Masukkan Waktu Kedatangan: ");
scanf("%g",&baru->datang);
printf("Masukkan Waktu Keluar: ");
scanf("%g",&baru->keluar);
}
else
printf("MAAF...!!! Data Tidak Ditemukan\n");
cout<<"Apakah Anda Ingin Mengulang Meng-Update Data (y/n) ? ";
cin>>OK;
cout<<endl;
}
while(OK=='y');
break;
}
}while (pilih!=6); }
*****************************************************************************88
double linked list
#include "stdio.h"
#include "stdlib.h"
#include "iostream.h"
struct node
{
int npm;
char nama[10];
struct node *next;
struct node *prev;
};
typedef struct node *simpul;
void main()
{
simpul baru, head=NULL, tail=NULL, temp, prev;
int pilih;
do
{
printf("**************************\n");
printf("* MENU *\n");
printf("* 1. Insert Data *\n");
printf("* 2. View Data *\n");
printf("* 3. Search Data *\n");
printf("* 4. Delete Data *\n");
printf("* 5. Update Data *\n");
printf("* 6. Exit Program *\n");
printf("**************************\n");
printf(" Silahkan Anda Pilih: ");
scanf("%d", &pilih);
printf("__________________________\n");
switch(pilih)
{case 1:
char OK;
do
{
baru = (simpul) malloc(sizeof (struct node));
printf("Masukkan NPM: "); scanf("%d", &baru->npm);fflush(stdin);
printf("Masukkan Nama: "); gets(baru->nama);
baru->next = NULL;
baru->prev = NULL;
if (head == NULL)
{
head = baru; tail = baru;
}
else
{ baru->prev = tail;
tail->next = baru; tail = baru;
}
cout<<"Apakah Anda Ingin Mengulang Meng-Insert Data (y/n) ? ";
cin>>OK;
cout<<endl;
}
while(OK=='y');
break;
case 2: do
{ int a;
printf("tampilkan secara\n1. maju\n2. mundur\nmasukkan pilihan:");
scanf("%d",&a);
if(a==1)
{ temp = head;
while(temp!=NULL)
{
printf("\n%d ", temp->npm);
cout<<temp->nama<<endl;
temp = temp->next;
}
printf("\n");
}
if(a==2)
{ temp = tail;
while(temp!=NULL)
{
printf("\n%d ", temp->npm);
cout<<temp->nama<<endl;
temp = temp->prev;
}
printf("\n");
}
cout<<"Apakah Anda Ingin Mengulang Meng-View Data (y/n) ? ";
cin>>OK;
cout<<endl;
}
while(OK=='y');
break;
case 3: do
{ int cari,a;
printf("Cari Angka: "); scanf("%d", &cari);
printf("pencarian secara\n1. maju\n2. mundur\nmasukkan pilihan:");
scanf("%d",&a);
if(a==1)
{ temp = head;
while((temp!=NULL)&&(temp->npm!=cari))
{
temp = temp->next;
}
if(temp != NULL && temp->npm == cari)
printf("SELAMAT...!!! Data Ditemukan\n");
else
printf("MAAF...!!! Data Tidak Ditemukan\n");
printf("\n");
}
if (a==2)
{ temp = tail;
while((temp!=NULL)&&(temp->npm!=cari))
{
temp = temp->prev;
}
if(temp != NULL && temp->npm == cari)
printf("SELAMAT...!!! Data Ditemukan\n");
else
printf("MAAF...!!! Data Tidak Ditemukan\n");
printf("\n");
}
cout<<"Apakah Anda Ingin Mengulang Meng-Search Data (y/n) ? ";
cin>>OK;
cout<<endl;
}
while(OK=='y');
break;
case 4: do
{ int hapus; char jwb; simpul prev = NULL;
printf("Hapus Angka: "); scanf("%d", &hapus); temp = head;
while((temp!=NULL)&&(temp->npm!=hapus))
{
prev = temp; temp = temp->next;
}
if(temp != NULL && temp->npm == hapus)
{
cout<<"Yakin Dihapus? (y/t)"; cin>>jwb;
if(jwb == 'y')
{
if(temp->next != NULL && temp->prev != NULL)
{
prev->next = temp->next;
(temp->next)->prev=prev;
}
else if (temp->next == NULL)
{
prev->next = NULL;
}
else if (temp == head && head->next == NULL)
{
head = NULL;
}
else if (temp == head && head->next != NULL)
{
(head->next)->prev=NULL;
head = head->next;
}
free(temp);
}
else
printf("OK...!?!!?!, Data Batal Dihapus");
}
else
printf("MAAF...!!! Data Tidak Ditemukan\n\n\n");
cout<<"Apakah Anda Ingin Mengulang Meng-Delete Data (y/n) ? ";
cin>>OK;
cout<<endl;
}
while(OK=='y');
break;
case 5:
do
{
prev=NULL;
int cari;
printf("Cari Angka: ");
scanf("%d", &cari);
temp = head;
/*while((temp!=NULL)&&(temp->npm!=cari))
{
temp = temp->next;
}*/
if(temp != NULL && temp->npm == cari)
{
printf("masukkan NPM baru = ");
scanf("%d",&temp->npm);
printf("masukkan Nama baru = ");
scanf("%s",&temp->nama);
}
else //if(temp == NULL)
printf("MAAF...!!! Data Tidak Ditemukan\n");
cout<<"Apakah Anda Ingin Mengulang Meng-Update Data (y/n) ? ";
cin>>OK;
cout<<endl;
}
while(OK=='y');
break;
}
}while (pilih!=6);
}
#include "stdlib.h"
#include "iostream.h"
struct node
{
int npm;
char nama[10];
struct node *next;
struct node *prev;
};
typedef struct node *simpul;
void main()
{
simpul baru, head=NULL, tail=NULL, temp, prev;
int pilih;
do
{
printf("**************************\n");
printf("* MENU *\n");
printf("* 1. Insert Data *\n");
printf("* 2. View Data *\n");
printf("* 3. Search Data *\n");
printf("* 4. Delete Data *\n");
printf("* 5. Update Data *\n");
printf("* 6. Exit Program *\n");
printf("**************************\n");
printf(" Silahkan Anda Pilih: ");
scanf("%d", &pilih);
printf("__________________________\n");
switch(pilih)
{case 1:
char OK;
do
{
baru = (simpul) malloc(sizeof (struct node));
printf("Masukkan NPM: "); scanf("%d", &baru->npm);fflush(stdin);
printf("Masukkan Nama: "); gets(baru->nama);
baru->next = NULL;
baru->prev = NULL;
if (head == NULL)
{
head = baru; tail = baru;
}
else
{ baru->prev = tail;
tail->next = baru; tail = baru;
}
cout<<"Apakah Anda Ingin Mengulang Meng-Insert Data (y/n) ? ";
cin>>OK;
cout<<endl;
}
while(OK=='y');
break;
case 2: do
{ int a;
printf("tampilkan secara\n1. maju\n2. mundur\nmasukkan pilihan:");
scanf("%d",&a);
if(a==1)
{ temp = head;
while(temp!=NULL)
{
printf("\n%d ", temp->npm);
cout<<temp->nama<<endl;
temp = temp->next;
}
printf("\n");
}
if(a==2)
{ temp = tail;
while(temp!=NULL)
{
printf("\n%d ", temp->npm);
cout<<temp->nama<<endl;
temp = temp->prev;
}
printf("\n");
}
cout<<"Apakah Anda Ingin Mengulang Meng-View Data (y/n) ? ";
cin>>OK;
cout<<endl;
}
while(OK=='y');
break;
case 3: do
{ int cari,a;
printf("Cari Angka: "); scanf("%d", &cari);
printf("pencarian secara\n1. maju\n2. mundur\nmasukkan pilihan:");
scanf("%d",&a);
if(a==1)
{ temp = head;
while((temp!=NULL)&&(temp->npm!=cari))
{
temp = temp->next;
}
if(temp != NULL && temp->npm == cari)
printf("SELAMAT...!!! Data Ditemukan\n");
else
printf("MAAF...!!! Data Tidak Ditemukan\n");
printf("\n");
}
if (a==2)
{ temp = tail;
while((temp!=NULL)&&(temp->npm!=cari))
{
temp = temp->prev;
}
if(temp != NULL && temp->npm == cari)
printf("SELAMAT...!!! Data Ditemukan\n");
else
printf("MAAF...!!! Data Tidak Ditemukan\n");
printf("\n");
}
cout<<"Apakah Anda Ingin Mengulang Meng-Search Data (y/n) ? ";
cin>>OK;
cout<<endl;
}
while(OK=='y');
break;
case 4: do
{ int hapus; char jwb; simpul prev = NULL;
printf("Hapus Angka: "); scanf("%d", &hapus); temp = head;
while((temp!=NULL)&&(temp->npm!=hapus))
{
prev = temp; temp = temp->next;
}
if(temp != NULL && temp->npm == hapus)
{
cout<<"Yakin Dihapus? (y/t)"; cin>>jwb;
if(jwb == 'y')
{
if(temp->next != NULL && temp->prev != NULL)
{
prev->next = temp->next;
(temp->next)->prev=prev;
}
else if (temp->next == NULL)
{
prev->next = NULL;
}
else if (temp == head && head->next == NULL)
{
head = NULL;
}
else if (temp == head && head->next != NULL)
{
(head->next)->prev=NULL;
head = head->next;
}
free(temp);
}
else
printf("OK...!?!!?!, Data Batal Dihapus");
}
else
printf("MAAF...!!! Data Tidak Ditemukan\n\n\n");
cout<<"Apakah Anda Ingin Mengulang Meng-Delete Data (y/n) ? ";
cin>>OK;
cout<<endl;
}
while(OK=='y');
break;
case 5:
do
{
prev=NULL;
int cari;
printf("Cari Angka: ");
scanf("%d", &cari);
temp = head;
/*while((temp!=NULL)&&(temp->npm!=cari))
{
temp = temp->next;
}*/
if(temp != NULL && temp->npm == cari)
{
printf("masukkan NPM baru = ");
scanf("%d",&temp->npm);
printf("masukkan Nama baru = ");
scanf("%s",&temp->nama);
}
else //if(temp == NULL)
printf("MAAF...!!! Data Tidak Ditemukan\n");
cout<<"Apakah Anda Ingin Mengulang Meng-Update Data (y/n) ? ";
cin>>OK;
cout<<endl;
}
while(OK=='y');
break;
}
}while (pilih!=6);
}
*********************************************************************
source: segokarak.blogspot.com
No comments:
Post a Comment