3 CONTOH PROGRAM QUEUE PADA C++

3 CONTOH PROGRAM QUEUE PADA C++



3 CONTOH PROGRAM QUEUE PADA C++
PROGRAM 1

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<iostream>
using namespace std;

typedef int ItemType;
typedef struct QueueNodeTag{
ItemType Item;
struct QueueNodeTag*Link;
}QueueNode;
typedef struct{
QueueNode *Front;
QueueNode *Rear;
}Queue;
void InitializeQueue(Queue *Q)
{
Q->Front=NULL;
Q->Rear=NULL;
}
int Empty(Queue *Q)
{
return(Q->Front==NULL);
}
int Full(Queue *Q)
{
return 0;
}
void Insert(ItemType R, Queue *Q)
{
QueueNode*Temp;
Temp=(QueueNode *)
malloc(sizeof(QueueNode));
if (Temp == NULL){
printf("Queue tidak dapat tercipta");
} else {
Temp->Item=R;
Temp->Link=NULL;
if(Q->Rear==NULL){
Q->Front=Temp;
Q->Rear=Temp;
}else{
Q->Rear->Link=Temp;
Q->Rear=Temp;
}
cout<<"\nItem dimasukkan: "<<R<<"\n";
}
}
void Remove( ItemType F, Queue *Q)
{
QueueNode *Temp;
if(Q->Front==NULL){
printf("Queue masih kosong!");
}else{
F=Q->Front->Item;
Temp=Q->Front;
Q->Front=Temp->Link;
free(Temp);
if(Q->Front==NULL)Q->Rear=NULL;
}
cout<<"\nItem dikeluarkan: "<<F<<"\n";
}
void Tampil(QueueNode *N) {
while (N !=NULL) {
printf("%5d", N->Item);
N=N->Link;
}
}
int main(){
Queue anam;
int Item;
anam.Front = anam.Rear = NULL;
Insert(100,&anam); Tampil(anam.Front);
Insert(200,&anam); Tampil(anam.Front);
Insert(300,&anam); Tampil(anam.Front);
cout<<"\n=====================================\n";
Remove(Item, &anam); Tampil(anam.Front);
Remove(Item, &anam); Tampil(anam.Front);
Remove(Item, &anam); Tampil(anam.Front);
return 0;
}


PROGRAM 2 QUEUE

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define maxqueue 100
using namespace std;

typedef int itemtype;
typedef struct{
int count;
int front;
int rear;
itemtype item[maxqueue] ;
} queue;

void initializequeue(queue *Q){
Q->count=0;
Q->front=0;
Q->rear=0;
}

int empty(queue *Q){
return Q->count=0;
}

int full(queue *Q){
return Q->count == maxqueue;
}

void insert(itemtype ins, queue *Q){
if (Q->count==maxqueue){
printf("Tidak dapat memasukkan data, Queue penuh!!");
}
else{
Q->item[Q->rear]=ins;
Q->rear = (Q->rear+1)%maxqueue;
++(Q->count);
}
}

void remove(queue *Q, itemtype *rm){
if(Q->count==0){
printf("Tidak dapat mengambil data! Queue kosong!!");
}
else{
*rm=Q->item[Q->front];
Q->front = (Q->front+1)%maxqueue;
--(Q->count);
}
}

void display(queue *Q){
if(Q->count==0){
cout<<"DATA KOSONG";
}else{
cout <<"Data dalam Queue : ";
for(int i=Q->front; i<=Q->rear-1;i++){
cout<<Q->item[i]<<" ";
}
}
}

int main(){
queue anam;
itemtype rm;
initializequeue(&anam);
insert(100, &anam);
display(&anam);
cout<<endl;
insert(200, &anam);
display(&anam);
cout<<endl;
insert(300, &anam);
display(&anam);
cout<<endl;
cout<<endl;
cout<<"REMOVE KE-1 BERJALAN"<<endl;
remove(&anam, &rm);
display(&anam);
cout<<endl;
cout<<"REMOVE KE-2 BERJALAN"<<endl;
remove(&anam, &rm);
display(&anam);
cout<<endl;
cout<<"REMOVE KE-2 BERJALAN"<<endl;
remove(&anam, &rm);
display(&anam);
cout<<endl;
return 0;
}


PROGRAM 3 QUEUE

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

typedef int itemtype;
typedef struct queuenodetag{
itemtype item;
struct queuenodetag *link;
}queuenode;

typedef struct{
queuenode *front;
queuenode *rear;
} queue;

void initializequeue(queue *Q){
Q->front=NULL;
Q->rear =NULL;
}

int empty(queue *Q){
return(Q->front==NULL);
}

int full(queue *Q){
return 0;
}

void insert(itemtype R, queue *Q){
queuenode *temp;
temp=(queuenode*)malloc(sizeof(queuenode));
if(temp == NULL){
cout<<"Queue tidak dapat tercipta";
}
else{
temp->item=R;
temp->link=NULL;
if(Q->rear=NULL){
Q->front=temp;
Q->rear=temp;
} else{
Q->rear->link=temp;
Q->rear=temp;
}
}
}

void remove(queue *Q, itemtype *F){
queuenode *temp;
if(Q->front==NULL){
cout<<"Queue Masih Kosong!!";
} else {
*F=Q->front->item;
Q->front = temp->link;
free(temp);
if(Q->front==NULL){
Q->rear=NULL;
}
}
cout<<"ITEM DIREMOVE !!!"<<endl;
}

void display(queuenode *Q){
while (Q!=NULL){
cout<<Q->item<<" ";
Q=Q->link;
}
}

int main(){
queue anam;
int data;
anam.front = anam.rear = NULL;
insert(100,&anam); display(anam.front);
insert(200,&anam); display(anam.front);
insert(300,&anam); display(anam.front);
cout<<"\n=====================================\n";
remove(&anam, &data); display(anam.front);
remove(&anam, &data); display(anam.front);
remove(&anam, &data); display(anam.front);
return 0;
}


Related Posts

Previous
Next Post »