Rabu, 18 Oktober 2017

LA STRUKDAT 2

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node{
structt node *prev;
int info;
struct node *next;
};
typedef struct node *simpul;
int main(){
simpul baru, head=NULL, temp;
int pilih, cari, data;
do {
 printf("\n");
 printf("=====================");
 printf("  Menu   ");
 printf("=====================");
 printf("  1. Insert Depan    ");
 printf("  2. View            ");
 printf("  3. Search          ");
 printf("  4. Delete Depan    ");
 printf("  5. Delete Belakang ");
 printf("  6. Keluar          ");
 printf("Pilih : ");
 scanf("%d",&pilih);
 switch(pilih){
 
  case 1:
   printf("Data Masuk : ");
   scanf("%i",&data);
   baru = (simpul) malloc(sizeof (struct node));
   baru->info = data;
   baru->next = NULL;
   baru->prev = NULL;
  
   if(head == NULL){
    head = baru;
    tail = baru;
   }
   else {
    baru->prev = tail;
    tail->next = baru;
    tail = baru;
   }
   break;
  
  case 2:
   printf("Dari Head\n");
   temp = head;
   while(temp!=NULL){
    printf("%i",temp->info);
    temp = temp->next;
   }
   printf("\nDari Tail\n");
   temp = tail;
  
   while(temp!=NULL){
    printf("%i", temp->info);
    temp = temp->prev;
   }
   printf("\n");
   break;
  
  case 3:
   printf("Cari Angka : ");
   scanf("%i", &cari);
   temp = head;
   temp = tail;{
    temp = temp->next;
   }
   if(temp != NULL && temp->info == cari){
    printf("Data Ditemukan");
    printf("\n");
   }
   break;
  
  case 4:
   temp = head;
   head = head->next;
   if(head != NULL)
    head->prev = (NULL);
   if(head == NULL)
    tail = NULL;
   free(temp);
   break;
  
  case 5:
   temp = tail;
   tail = tail->prev;
   if(tail != NULL)
    tail->next = NULL;
   if(tail == NULL)
    head == NULL;
   free(temp);
   break;
 }
}
while(pilih!=6);
}

Tidak ada komentar:

Posting Komentar