#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
struct node{
int info;
struct node *next;
};
typedef struct node *simpul;
void main()
{
simpul baru, front=NULL, rear=NULL, temp;
int pilih;
do
{
printf("MENU\n");
printf("1. ENQUEUE\n");
printf("2. VIEW\n");
printf("4. DEQUEUE\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
if (front == NULL) //khusus simpul pertama LL
{ front = baru;
rear = baru;
}
else //untuk simpul2 berikutnya
{ rear->next = baru;
rear = baru;
}
break;
case 2:
temp = front; //tampilkan mulai dr depan
while(temp!=NULL) //ulangi sampai temp NULL
{ printf("%i ", temp->info);
temp = temp->next; //geser temp ke belakang
}
printf("\n");
break;
case 4:
if(front != NULL)
{ temp = front;
printf("Data keluar = %d\n", temp->info);
front = temp->next;
free(temp);
printf("\n");
}
else printf("Queue sudah kosong!\n");
break;
}
}while (pilih!=5);
}
No comments:
Post a Comment