Advertisement
blackhatMS

pr0gram-pak-azhar.c

May 12th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. struct node
  6. {
  7.     struct node *previous;
  8.     int info;
  9.     struct node *next;
  10. };
  11.  
  12. typedef struct node *simpul;
  13.  
  14. void main()
  15. {
  16.     simpul baru, head=NULL, tail=NULL, temp;
  17.     int pilih;
  18.  
  19.     do
  20.     {
  21.         printf("MENU\n");
  22.         printf("1. Insert Depan\n");
  23.         printf("2. View\n");
  24.         printf("3. Search\n");
  25.         printf("4. Delete Depan\n");
  26.         printf("PILIH: ");
  27.         scanf("%d", &pilih);
  28.        
  29.         int data, cari;
  30.         switch(pilih)
  31.         {
  32.             case 1:
  33.                 //int data;
  34.                 printf("Data Masuk: ");
  35.                 scanf("%i", &data);
  36.                 baru = (simpul) malloc(sizeof (struct node));
  37.                 baru->info = data;
  38.                 baru->next = NULL; //tidak menuju simpul mana2
  39.                 baru->previous = NULL;
  40.  
  41.                 if (head == NULL) //khusus simpul pertama LL
  42.                 {
  43.                     head = baru; //pointer head, tail, baru sama
  44.                     tail = baru;
  45.                 }
  46.                 else //untuk simpul2 berikutnya
  47.                 {
  48.                     baru->next = head;
  49.                     head->previous = baru;
  50.                     head = baru;
  51.                 }
  52.                 break;
  53.             case 2:
  54.                 printf("Dari HEAD\n");
  55.                 temp = head; //tampilkan mulai dr depan
  56.                 while(temp!=NULL) //ulangi sampai temp bernilai NULL
  57.                 {
  58.                     printf("%i ", temp->info);
  59.                     temp = temp->next; //geser temp ke belakang
  60.                 }
  61.                 printf("\nDari Tail\n");
  62.                 temp = tail; //tampilkan mulai dr depan
  63.                 while(temp!=NULL) //ulangi sampai temp bernilai NULL
  64.                 {
  65.                     printf("%i ", temp->info);
  66.                     temp = temp->previous; //geser temp ke belakang
  67.                 }
  68.                 printf("\n");
  69.                 break;
  70.             case 3:
  71.                 //int cari;
  72.                 printf("Cari Angka: ");
  73.                 scanf("%i", &cari);
  74.                 temp = head;
  75.                 while((temp!=NULL)&&(temp->info!=cari))
  76.                 {
  77.                     temp = temp->next;
  78.                 }
  79.                 if(temp != NULL && temp->info == cari)
  80.                     printf("Data Ditemukan");
  81.                 else //if(temp == NULL)
  82.                     printf("Data Tidak Ditemukan");
  83.                     printf("\n");
  84.                 break; 
  85.             case 4://hapus depan
  86.                 temp = head;
  87.                 head = head->next;
  88.                 if (head != NULL)
  89.                     head->previous = NULL;
  90.                 if (head == NULL)
  91.                     tail = NULL;
  92.                 free(temp);
  93.                 break;
  94.         }
  95.     }while (pilih!=5);
  96. }
  97. //End of File
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement