#include stdio.h
#include stdlib.h
typedef struct simpul DNode;
struct simpul
{
int data;
DNode *prev;
DNode *next;
};
void message_gagal();
void message_gagal_delete();
DNode *head = NULL, *tail = NULL, *baru;
void allocate_DNode (int x)
{
baru = (DNode *) malloc (sizeof(DNode));
if(baru==NULL)
{
printf("Alokasi Gagal\n");
exit(1);
}
else
{
baru->data=x;
baru->next=NULL;
baru->prev=NULL;
}
}
void buat_list()
{
printf("Data Masih Kosong, List Akan Dibuat\n");
system("PAUSE");
head = tail = baru;
}
void sisip_awal()
{
if(head==NULL && tail==NULL)
buat_list();
else
{
baru->next=head;
head->prev=baru;
head=baru;
}
}
void sisip_akhir()
{
if(head==NULL && tail==NULL)
buat_list();
else
{
baru->prev=tail;
tail->next=baru;
tail=baru;
}
}
void sisip_sebelum(int s)
{
DNode *before=head;
int cek=0;
if(before->next==NULL)
cek=1;
else
{
while(before->next->data!=s)
{
before=before->next;
if(before->next==NULL)
{
cek = 1;
break;
}
}
}
if(cek==0)
{
baru->prev = before;
baru->next = before->next;
before->next->prev = baru;
before->next = baru;
}
else
message_gagal();
}
void sisip_sesudah(int s)
{
DNode *after=tail;
int cek=0;
if(after->prev == NULL)
cek=1;
else
{
while(after->prev->data!=s)
{
after=after->prev;
if(after->prev==NULL)
{
cek = 1;
break;
}
}
}
if(cek==0)
{
baru->next = after;
baru->prev = after->next;
after->prev->next = baru;
after->prev = baru;
}
else
message_gagal();
}
void free_DNode(DNode *p)
{
free(p);
p=NULL;
printf("Data Telah Terhapus\n");
}
void hapus_satu()
{
DNode *hapus = head;
head = NULL;
tail = NULL;
free_DNode(hapus);
}
void hapus_awal()
{
DNode *hapus = head;
head->next->prev = NULL;
head = head->next;
free_DNode(hapus);
}
void hapus_akhir()
{
DNode *hapus = tail;
tail->prev->next = NULL;
tail = tail->prev;
free_DNode(hapus);
}
void hapus_tengah(int s)
{
DNode *hapus=head;
int cek=0;
while(hapus->data!=s)
{
if(hapus->next==NULL)
{
cek=1;
break;
}
hapus = hapus->next;
}
if(cek==0)
{
hapus->prev->next = hapus->next;
hapus->next->prev = hapus->prev;
free_DNode(hapus);
}
else
message_gagal_delete();
}
void message_gagal()
{
printf("Simpul Baru Tidak Bisa Di Sisipkan\n");
system("PAUSE");
}
void message_gagal_delete()
{
printf("Tidak Ada Data Yang Di Hapus\n");
system("PAUSE");
}
void tampil()
{
DNode *p= head;
printf("\nData Simpul ==> ");
while(p!=NULL)
{
printf("%d ", p->data);
p=p->next;
}
printf("\n\n");
}
void main()
{
head=baru;
int pilih, data, s;
char lagi='y';
while(lagi=='y')
{
system("CLS");
tampil();
printf("*-- MENU --* : \n");
printf("1. Sisip Awal\n");
printf("2. Sisip Akhir\n");
printf("3. Sisip Sebelum Simpul\n");
printf("4. Sisip Sesudah Simpul\n");
printf("5. Hapus Awal\n");
printf("6. Hapus Akhir\n");
printf("7. Hapus Tengah\n");
printf("\nPilih Menu : ");
scanf("%d", &pilih);
switch(pilih)
{
case 1 :
printf("Input Data : ");
scanf("%d", &data);
allocate_DNode(data);
sisip_awal();
break;
case 2 :
printf("Input Data : ");
scanf("%d", &data);
allocate_DNode(data);
sisip_akhir();
break;
case 3 :
printf("Input Data : ");
scanf("%d", &data);
allocate_DNode(data);
if(head==NULL && tail==NULL)
buat_list();
else
{
printf("di Input sebelum : ");
scanf("%d",&s);
if(s==head->data)
sisip_awal();
else
sisip_sebelum(s);
}
break;
case 4 :
printf("Input Data : ");
scanf("%d", &data);
allocate_DNode(data);
if(head==NULL && tail==NULL)
buat_list();
else
{
printf("di Input sesudah : ");
scanf("%d",&s);
if(s==tail->data)
sisip_akhir();
else
sisip_sesudah(s);
}
break;
case 5 :
if(head == NULL && tail == NULL)
message_gagal_delete();
else if(head == tail)
hapus_satu();
else
hapus_awal();
break;
case 6 :
if(head == NULL && tail == NULL)
message_gagal_delete();
else if(head == tail)
hapus_satu();
else
hapus_akhir();
break;
case 7 :
printf("Data Yang Di Hapus : ");
scanf("%d", &data);
if(head == NULL && tail == NULL)
message_gagal_delete();
else if(data == head->data && data == tail->data && head
== tail)
hapus_satu();
else if(data == head->data)
hapus_awal();
else if(data == tail->data)
hapus_akhir();
else
hapus_tengah(data);
break;
}
fflush(stdin);
printf("Lagi (y/n) ? ");
scanf("%c", &lagi);
}
}
#include stdio.h
#include stdlib.h
typedef struct simpul DNode;
struct simpul{
int data;
DNode *next;
DNode *prev;
};
DNode *head=NULL, *tail=NULL, *baru;
void allocate_note(int x) {
baru = (DNode *) malloc (sizeof(DNode));
if(baru==NULL) {
printf("alokasi gagal");
exit(1);
}else{
baru->data = x;
baru->next = NULL;
baru->prev = NULL;
}
}
void cetak_head_tail() {
DNode *p = head;
while(p!=NULL){
printf("%d ", p->data);
p = p->next;
}printf("\n");
}
void sisip_awal(){
baru->next = head;
head->prev = baru;
head = baru;
}
void sisip_akhir() {
tail->next = baru;
baru->prev = tail;
tail = baru;
}
void sisip_sebelum(int x) {
DNode *before = head;
while(before->data != x)
before = before->next;
baru->prev = before->prev;
baru->next = before;
before->prev->next = baru;
before->prev = baru;
}
void sisip_setelah(int x) {
DNode *after = head;
while(after->data != x)
after = after->next;
baru->next = after->next;
baru->prev = after;
after->next->prev = baru;
after->next = baru;
}
void free_Node(DNode *p) {
free(p);
p = NULL;
}
void hapus_awal(){
DNode *hapus = head;
head = head->next;
head->prev = NULL;
free_Node(hapus);
}
void hapus_akhir(){
DNode *hapus = tail;
tail = tail->prev;
tail->next = NULL;
free_Node(hapus);
}
void hapus_simpul(int x) {
DNode *hapus = head;
while(hapus->data != x)
hapus = hapus->next;
hapus->prev->next = hapus->next;
hapus->next->prev = hapus->prev;
free_Node(hapus);
}
void main(){
allocate_note(10);
head=tail=baru;
cetak_head_tail();
allocate_note(5);
sisip_awal();
cetak_head_tail();
allocate_note(12);
sisip_akhir();
cetak_head_tail();
allocate_note(8);
sisip_sebelum(10);
cetak_head_tail();
allocate_note(9);
sisip_setelah(8);
cetak_head_tail();
hapus_awal();
cetak_head_tail();
hapus_akhir();
cetak_head_tail();
hapus_simpul(9);
cetak_head_tail();
}