๐ Daftar Isi
Deklarasi Elemen Antrian
#include <stdio.h>
#include <stdbool.h>
struct node
{
int data;
struct node* next;
};
typedef struct node* item;
Struktur Queue
struct queue
{
int count;
item front;
item rear;
};
typedef struct queue* antrian;
Inisialisasi Queue
void initialize(antrian q)
{
q->count = 0;
q->front = NULL;
q->rear = NULL;
}
Fungsi Insert
void q_insert(antrian q, int value)
{
item new_item;
new_item = (item)malloc(sizeof(struct node));
new_item->data = value;
new_item->next = NULL;
if(!isempty(q))
{
q->rear->next = new_item;
q->rear = new_item;
}
else
{
q->rear = q->front = new_item;
}
q->count++;
}
Fungsi Remove
void q_remove(antrian q)
{
item tmp;
tmp = q->front;
q->front = q->front->next;
q->count--;
free(tmp);
}
Fungsi Display
void display(antrian q)
{
item n;
n = q->front;
if(q->front == NULL)
{
printf("\nQueue masih kosong\n");
}
else
{
while(n != NULL)
{
printf("%d\n", n->data);
n = n->next;
}
}
}
Source Code Lengkap
#include <stdio.h>
#include <stdbool.h>
struct node
{
int data;
struct node* next;
};
typedef struct node* item;
struct queue
{
int count;
item front;
item rear;
};
typedef struct queue* antrian;
void initialize(antrian q)
{
q->count = 0;
q->front = NULL;
q->rear = NULL;
}
bool isempty(antrian q)
{
return (q->rear == NULL);
}
void q_insert(antrian q, int value)
{
item new_item;
new_item = (item)malloc(sizeof(struct node));
new_item->data = value;
new_item->next = NULL;
if(!isempty(q))
{
q->rear->next = new_item;
q->rear = new_item;
}
else
{
q->rear = q->front = new_item;
}
q->count++;
}
void q_remove(antrian q)
{
item tmp;
tmp = q->front;
q->front = q->front->next;
q->count--;
free(tmp);
}
void display(antrian q)
{
item n;
n = q->front;
if(q->front == NULL)
{
printf("\nQueue masih kosong\n");
}
else
{
while(n != NULL)
{
printf("%d\n", n->data);
n = n->next;
}
}
}
int main()
{
antrian q;
q = (antrian)malloc(sizeof(struct node));
initialize(q);
q_insert(q,10);
q_insert(q,20);
q_insert(q,30);
q_insert(q,40);
printf("Queue sebelum dequeue\n");
display(q->front);
q_remove(q);
printf("Queue setelah dequeue\n");
display(q->front);
return 0;
}
Output
Materi Lengkap
Silakan baca juga beberapa artikel menarik kami tentang Queue, daftar lengkapnya adalah sebagai berikut.