Operasi pada Queue
Disini akan dijelaskan sedikit mengenai implementasi Queue dengan Premtive
(Prioritas).
Tanpa basa basi langsung aja ke codenya:
Pertama ketik code dibawah ini, dan simpan sebagai interface.h:
#include<stdio.h>
#include<stdlib.h>
#define Error(Str) FatalError(Str)
#define FatalError(Str) fprintf(stderr, "%sn", Str), exit(1)
Lalu simpan code dibawah dengan file queue.h:
#ifndef _Queue_h
#define _Queue_h
typedef int ElementType;
struct QueueRecord;
typedef struct QueueRecord *Queue;
Queue CreateQueue(int MaxElements);
void Enqueue(ElementType X, Queue Q);
ElementType Front(Queue Q);
void Dequeue(Queue Q);
void Preemptive(ElementType X, Queue Q);
void DisposeQueue(Queue Q);
void MakeEmpty(Queue Q);
int IsEmpty(Queue Q);
int IsFull(Queue Q);
#endif
Kemudian ketikkan code dibawah sebagai implementasi dan simpan file dengan
queue.c:
#include "queue.h"
#include "fatal.h"
#include <stdlib.h>
struct QueueRecord{
int Capacity;
int Front;
int Rear;
int Size;
ElementType *Array;
};
static int Succ(int value, Queue Q);
void Enqueue(ElementType X, Queue Q){
if(IsFull(Q))
Error("Full Queue");
else{
Q->Size++;
Q->Array[Q->Rear]=X;
Q->Rear = Succ(Q->Rear,Q);
}
}
Queue CreateQueue(int MaxElements){
Queue Q = NULL;
Q=malloc(sizeof(struct QueueRecord));
if(Q==NULL)
FatalError("Out of Space!!!");
Q->Array=malloc(sizeof(ElementType) * MaxElements);
if(Q->Array==NULL)
FatalError("Out of space!!!");
Q->Capacity=MaxElements;
MakeEmpty(Q);
return Q;
}
void Preemptive(ElementType X, Queue Q){
if(IsFull(Q))
Error("Full Queue");
else{
Q->Size++;
Q->Array[Q->Front]=X;
Q->Rear = Succ(Q->Rear,Q);
}
}
ElementType Front(Queue Q){
if(!IsEmpty(Q))
return Q->Array[Q->Front];
Error("Empty Queue");
return 0;
}
void Dequeue(Queue Q){
if(IsEmpty(Q))
Error("Empty queue");
else{
Q->Size--;
Q->Front = Succ(Q->Front, Q);
}
}
void DisposeQueue(Queue Q){
if(Q != NULL ){
free(Q->Array);
free(Q);
}
}
static int Succ(int Value, Queue Q){
if(++Value == Q->Capacity)
Value=0;
return Value;
}
void MakeEmpty(Queue Q){
Q->Size = 0;
Q->Front = 0;
Q->Rear = 0;
}
int IsEmpty(Queue Q){
return Q->Size == 0;
}
int IsFull(Queue Q){
return Q->Size == Q->Capacity;
}
Simpan file keempat dengan nama klien.c yang berfungsi sebagi klien:
#include<stdio.h>
#include "queue.h"
int main(){
Queue Q;
int i;
int n=20;
static int max_queue_size = 20;
Q = CreateQueue(max_queue_size);
Preemptive(90,Q);
printf("%dt",Front(Q));
Dequeue(Q);
for(i=0;i<n;i++){
for(i=0;i<n;i++){
if(Q!=NULL){
if(i%2==0){
Enqueue(i,Q);
}
}
if(i%2==0)
printf("%d,",i);
}
}
DisposeQueue(Q);
return 0;
}
Setelah dicompile maka akan ditampilkan Hasil dari penjumlahan angka genap yang
berderet dari mulai 0 sampai 18.
Hasil dari penjumlahannya adalah angka 90.
Maka deret bilangannya adalah:
90 0,2,4,6,8,10,12,14,16,18.

Operasi pada queue

  • 1.
    Operasi pada Queue Disiniakan dijelaskan sedikit mengenai implementasi Queue dengan Premtive (Prioritas). Tanpa basa basi langsung aja ke codenya: Pertama ketik code dibawah ini, dan simpan sebagai interface.h: #include<stdio.h> #include<stdlib.h> #define Error(Str) FatalError(Str) #define FatalError(Str) fprintf(stderr, "%sn", Str), exit(1) Lalu simpan code dibawah dengan file queue.h: #ifndef _Queue_h #define _Queue_h typedef int ElementType; struct QueueRecord; typedef struct QueueRecord *Queue; Queue CreateQueue(int MaxElements); void Enqueue(ElementType X, Queue Q); ElementType Front(Queue Q); void Dequeue(Queue Q); void Preemptive(ElementType X, Queue Q); void DisposeQueue(Queue Q); void MakeEmpty(Queue Q); int IsEmpty(Queue Q);
  • 2.
    int IsFull(Queue Q); #endif Kemudianketikkan code dibawah sebagai implementasi dan simpan file dengan queue.c: #include "queue.h" #include "fatal.h" #include <stdlib.h> struct QueueRecord{ int Capacity; int Front; int Rear; int Size; ElementType *Array; }; static int Succ(int value, Queue Q); void Enqueue(ElementType X, Queue Q){ if(IsFull(Q)) Error("Full Queue"); else{ Q->Size++; Q->Array[Q->Rear]=X; Q->Rear = Succ(Q->Rear,Q); } }
  • 3.
    Queue CreateQueue(int MaxElements){ QueueQ = NULL; Q=malloc(sizeof(struct QueueRecord)); if(Q==NULL) FatalError("Out of Space!!!"); Q->Array=malloc(sizeof(ElementType) * MaxElements); if(Q->Array==NULL) FatalError("Out of space!!!"); Q->Capacity=MaxElements; MakeEmpty(Q); return Q; } void Preemptive(ElementType X, Queue Q){ if(IsFull(Q)) Error("Full Queue"); else{ Q->Size++; Q->Array[Q->Front]=X; Q->Rear = Succ(Q->Rear,Q); } }
  • 4.
    ElementType Front(Queue Q){ if(!IsEmpty(Q)) returnQ->Array[Q->Front]; Error("Empty Queue"); return 0; } void Dequeue(Queue Q){ if(IsEmpty(Q)) Error("Empty queue"); else{ Q->Size--; Q->Front = Succ(Q->Front, Q); } } void DisposeQueue(Queue Q){ if(Q != NULL ){ free(Q->Array); free(Q); } } static int Succ(int Value, Queue Q){ if(++Value == Q->Capacity) Value=0; return Value; }
  • 5.
    void MakeEmpty(Queue Q){ Q->Size= 0; Q->Front = 0; Q->Rear = 0; } int IsEmpty(Queue Q){ return Q->Size == 0; } int IsFull(Queue Q){ return Q->Size == Q->Capacity; } Simpan file keempat dengan nama klien.c yang berfungsi sebagi klien: #include<stdio.h> #include "queue.h" int main(){ Queue Q; int i; int n=20; static int max_queue_size = 20; Q = CreateQueue(max_queue_size);
  • 6.
    Preemptive(90,Q); printf("%dt",Front(Q)); Dequeue(Q); for(i=0;i<n;i++){ for(i=0;i<n;i++){ if(Q!=NULL){ if(i%2==0){ Enqueue(i,Q); } } if(i%2==0) printf("%d,",i); } } DisposeQueue(Q); return 0; } Setelah dicompilemaka akan ditampilkan Hasil dari penjumlahan angka genap yang berderet dari mulai 0 sampai 18. Hasil dari penjumlahannya adalah angka 90. Maka deret bilangannya adalah: 90 0,2,4,6,8,10,12,14,16,18.