Operation Queue

Here will be explained a little about the implementation of the Queue with Premtive ( Priority ) .

Without further ado immediately wrote to code themselves :

First type in the code below , and save it as interface.h :

#include

#include

#define Error(Str) FatalError(Str)

#define FatalError(Str) fprintf(stderr, "%s\n", Str), exit(1)

Then save the file queue.h code below :

#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

Then type the code below and save the file as an implementation with queue.c :

#include "queue.h"

#include "fatal.h"

#include

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;

}

Save the file with the name fourth klien.c that functions as a client

#include

#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("%d\t",Front(Q));

Dequeue(Q);

for(i=0;i

for(i=0;i

if(Q!=NULL){

if(i%2==0){

Enqueue(i,Q);

}

}

if(i%2==0)

printf("%d,",i);

}

}

DisposeQueue(Q);

return 0;

}

Once compiled it will display results from the sum of the even numbers in a row from the start of 0 to 18 .Results of the sum is number 90 .Number is the series are :

90 0,2,4,6,8,10,12,14,16,18.

<p>thank you</p>
<p>Welcome to Instructables. Thanks for sharing. It's a great coding project.</p>