Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

/*Program Double Linked List*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct siswa Dnode;
struct siswa
{
int no;
char nama [20];
float nilai;
Dnode *prev,*next;
};
int allocate_node();
void menu();
void insert_node();
void sisip_awal();
void sisip_after(int);
void sisip_before(int);
void sisip_akhir();
void delete_node();
void hapus_awal();
void hapus_after(int);
void hapus_akhir();
void tampil_node();
void tampil_FIFO();
void tampil_LIFO();
void free_node(Dnode *);
Dnode *head=NULL,*baru;
main()
{
menu();
}
int allocate_node()
{
baru=(Dnode *)malloc(sizeof(Dnode));
if(baru==NULL)
{
printf("Pemesanan memori gagal!!!");
exit(0);
}
else

{
printf("Nomor : ");scanf("%d",&baru->no);
printf("Nama : ");scanf("%s",&baru->nama);
printf("Nilai : ");scanf("%f",&baru->nilai);
baru->next=NULL;
baru->prev=NULL;
return 1;
}
}
void menu()
{
int p;
printf("Menu Utama :\n");
printf("1. Insert node\n");
printf("2. Delete node\n");
printf("3. Tampilkan existing Double Linked List\n");
printf("4. Exit program\n");
printf("Masukkan pilihan anda : ");scanf("%d",&p);
switch(p)
{
case 1:
insert_node();
break;
case 2:
delete_node();
break;
case 3:
tampil_node();
break;
case 4:
exit(0);
break;
default:
printf("Masukan anda salah!!!\n");
exit(0);
break;
}
}
void insert_node()
{
int i,alokasi,key;

printf("Menu Insert :\n");


printf("1. Insert di awal\n");
printf("2. Insert setelah node tertentu\n");
printf("3. Insert sebelum node tertentu\n");
printf("4. Insert di akhir\n");
printf("Masukkan pilihan anda : ");scanf("%d",&i);
alokasi=allocate_node();
if(alokasi==1)
{
switch(i)
{
case 1:
sisip_awal();
break;
case 2:
printf("Data dimasukkan setalah nomor :
");scanf("%d",&key);
sisip_after(key);
break;
case 3:
printf("Data dimasukkan sebelum nomor :
");scanf("%d",&key);
sisip_before(key);
break;
case 4:
sisip_akhir();
break;
default:
printf("Masukan anda salah!!!\n");
exit(0);
break;
}
}
}
void delete_node()
{
int d,key;
printf("Menu Delete :\n");
printf("1. Delete di awal\n");
printf("2. Delete setelah node tertentu\n");
printf("3. Delete di akhir\n");
printf("Masukkan pilihan anda : ");scanf("%d",&d);

switch(d)
{
case 1:
hapus_awal();
break;
case 2:
printf("Data dihapus setelah nomor : ");scanf("%d",&key);
hapus_after(key);
break;
case 3:
printf("Data dihapus sebelum nomor : ");scanf("%d",&key);
hapus_akhir(key);
break;
default:
printf("Masukan anda salah!!!\n");
exit(0);
break;
}
}
void tampil_node()
{
int t;
printf("Menu Tampil :\n");
printf("1. Tampil F.I.F.O\n");
printf("2. Tampil L.I.F.O\n");
printf("Masukkan pilihan anda : ");scanf("%d",&t);
switch(t)
{
case 1:
tampil_FIFO();
break;
case 2:
tampil_LIFO();
break;
default:
printf("Masukan anda salah!!!\n");
exit(0);
break;
}
}
void sisip_awal()

{
if(head==NULL)
head=baru;
else
baru->next=head;
head->prev=baru;
head=baru;
}
void sisip_after(int key)
{
Dnode *after,*bantu;
after=head;
while(after->no!=key)
after=after->next;
bantu=after->next;
baru->next=after->next;
baru->prev=after;
after->next=baru;
bantu->prev=baru;
}
void sisp_before(int key)
{
Dnode *before,*prevbefore;
before=head;
do
{
prevbefore=before;
before=before->next;
}while(before->no!=key);
{
baru->next=before;
baru->prev=prevbefore;
prevbefore->next=baru;
before->prev=baru;
}
}
void sisip_akhir()
{
Dnode *tail;
tail=head;

while(tail->next!=NULL)
tail=tail->next;
tail->next=baru;
baru->prev=tail;
}
void hapus_awal()
{
Dnode *hapus;
hapus=head;
head=hapus->next;
free_node(hapus);
}
void hapus_after(int key)
{
Dnode *hapus,*after,*bantu;
after=head;
while(after->no!=key)
after=after->next;
if(after->next==NULL)
{
printf("Tidak ada data yang bisa dihapus\n");
hapus=after->next;
}
if(hapus->next==NULL)
hapus_akhir();
else
{
bantu=hapus->next;
after->next=bantu;
bantu->prev=hapus;
free_node(hapus);
}
}
void hapus_akhir()
{
Dnode *prevhapus, *hapus;
hapus=head;
while(hapus->next!=NULL)
{
prevhapus=hapus;
hapus=hapus->next;

}
free_node(hapus);
prevhapus->next=NULL;
}
void tampil_FIFO()
{
Dnode *baca;
baca=head;
while(baca!=NULL)
{
printf("\n");
printf("Nomor : %d\n",baca->no);
printf("Nama : %s\n",baca->nama);
printf("Nilai : %g\n",baca->nilai);
printf("\n");
baca=baca->next;
}
menu();
}
void tampil_LIFO()
{
Dnode *baca;
baca=head;
while(baca->next!=NULL)
baca=baca->next;
while(baca!=NULL)
{
printf("Nomor : %d",baca->no);
printf("Nama : %s",baca->nama);
printf("Nilai : %f",baca->nilai);
}
}
void free_node(Dnode *p)
{
free(p);
p=NULL;
}

You might also like