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

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

Contoh Program Double Linked List dalam Bahasa C

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