Kuyrukla (Queue) İşlemler

Bu yazımda kuyruğa (queue) eleman ekleme, eleman görüntüleme, eleman silme, kuyruk boş ve dolu kontrollerini yapan C++ kodunu yazdım.

#include <iostream>
#define MAX 5
using namespace std;
void enqueue(int queue[], int &first, int &last, int value);
void dequeue(int queue[], int &first, int &last);
int peek(int queue[], int &first, int &last);
bool isFull(int &first, int &last);
bool isEmpty(int &first, int &last);
int main(){
	int queue[MAX], first=0, last=0;
	enqueue(queue, first, last, 10);
	enqueue(queue, first,last, 84);
	enqueue(queue, first,last, 92);
	while(!isEmpty(first, last)){
		cout << peek(queue, first, last) << " ";
		dequeue(queue, first, last);
	}
	dequeue(queue, first, last);
	enqueue(queue, first, last, 10);
	enqueue(queue, first,last, 84);
	enqueue(queue, first,last, 92);
	enqueue(queue, first, last, 10);
	enqueue(queue, first,last, 84);
	enqueue(queue, first,last, 92);
	while(!isEmpty(first, last)){
		cout << peek(queue, first, last) << " ";
		dequeue(queue, first, last);
	}
	return 0;
}
bool isFull(int &first, int &last){
	if((last-first) < MAX){
		return false;
	}
	return true;
}
bool isEmpty(int &first, int &last){
	if((last-first) == 0){
		return true;
	}
	return false;
}
void enqueue(int queue[], int &first, int &last, int value){
	if(!isFull(first, last)){
		queue[last] = value;
		last++;
	}else{
		cout << "KUYRUK DOLU!" << endl;
	}
}
void dequeue(int queue[], int &first, int &last){
	if(!isEmpty(first, last)){
		first++;
	}else{
		cout << "KUYRUK BOS!" << endl;
	}
}
int peek(int queue[], int &first, int &last){
	if(!isEmpty(first, last)){
		return queue[first];
	}
	return -1;
}

 

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir