Thursday, 14 May 2009

Program Queue Memakai Linked List

#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);
}

Monday, 11 May 2009

Contoh Program Queue Menggunakan Array Bahasa C

#include "stdio.h"
void main()
{ int queue[5];
int depan = -1;
int belakang = -1;
int pilihan, data, i;

do{
printf("MENU\n");
printf("1. ENQUEUE\n2. DEQUEUE\n3. VIEW\n4. EXIT\n");
printf("Pilihan = "); scanf("%d", &pilihan);
switch (pilihan)
{
case 1: //enqueue
//apakah queue belum penuh?
if (belakang < 4 )
{ printf("Data Masuk = "); scanf("%d", &data);
queue[belakang+1] = data;
belakang++;
if (belakang == 0)
depan = 0;
}
else
printf("Queue penuh!\n");

break;
case 2: //dequeue
//apakah queue belum kosong?
if (depan <= belakang)
{ printf("Data keluar = %d\n", queue[depan]);
depan++;
}
else
printf("Queue kosong!\n");
break;
case 3:
for(i=depan; i<=belakang; i++)
printf("%d ", queue[i]);
printf("\n");
break;
}
}while (pilihan != 4);
}

Artikel terkait:
Queue dengan Liked List
Queue Array Java
Queue Linked List Java
Queue Library Java