#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
struct node{
struct node *previous;
int info;
struct node *next;
};
typedef struct node *simpul;
void main()
{
simpul baru, head=NULL, tail=NULL, temp;
int pilih;
do
{
printf("MENU\n");
printf("1. Insert Depan\n");
printf("2. View\n");
printf("3. Search\n");
printf("4. Delete Depan\n");
printf("PILIH: ");
scanf("%d", &pilih);
switch(pilih)
{
case 1:
int data;
printf("Data Masuk: ");
scanf("%i", &data);
baru = (simpul) malloc(sizeof (struct node));
baru->info = data;
baru->next = NULL; //tidak menuju simpul mana2
baru->previous = NULL;
if (head == NULL) //khusus simpul pertama LL
{
head = baru; //pointer head, tail, baru sama
tail = baru;
}
else //untuk simpul2 berikutnya
{
baru->next = head;
head->previous = baru;
head = baru;
}
break;
case 2:
printf("Dari HEAD\n");
temp = head; //tampilkan mulai dr depan
while(temp!=NULL) //ulangi sampai temp bernilai NULL
{
printf("%i ", temp->info);
temp = temp->next; //geser temp ke belakang
}
printf("\nDari Tail\n");
temp = tail; //tampilkan mulai dr depan
while(temp!=NULL) //ulangi sampai temp bernilai NULL
{
printf("%i ", temp->info);
temp = temp->previous; //geser temp ke belakang
}
printf("\n");
break;
case 3:
int cari;
printf("Cari Angka: ");
scanf("%i", &cari);
temp = head;
while((temp!=NULL)&&(temp->info!=cari))
{
temp = temp->next;
}
if(temp != NULL && temp->info == cari)
printf("Data Ditemukan");
else //if(temp == NULL)
printf("Data Tidak Ditemukan");
printf("\n");
break;
case 4://hapus depan
temp = head;
head = head->next;
if (head != NULL)
head->previous = NULL;
if (head == NULL)
tail = NULL;
free(temp);
break;
}
}while (pilih!=5);
}
No comments:
Post a Comment