Program:-
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct Queue{
int front,rear,size;
int capacity;
int *array;
};
struct Queue* createqueue(int capacity)
{
struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
queue->capacity = capacity;
queue->front = 0;
queue->rear = 0;
queue->size = 0;
queue->array = new int(capacity);
return queue;
}
int isempty(struct Queue *queue)
{
return (queue->size==0);
}
int isfull(struct Queue* queue)
{
return (queue->size==queue->capacity);
}
void enque(struct Queue* queue , int element)
{
if(isfull(queue)) return;
cout<<"Enqueue element is: "<<element<<endl;
queue->array[(queue->rear++)%queue->capacity] = element;
queue->size++;
}
void deque(struct Queue *queue)
{
if(isempty(queue)) return ;
cout<<"Dequeue element is: "<<queue->array[(queue->front++)%queue->capacity]<<endl;
queue->size--;
}
void printf(struct Queue* queue)
{
while(queue->front!=queue->rear)
{
cout<<"The element is: "<<queue->array[(queue->front++)%queue->capacity]<<endl;
}
}
int size(struct Queue* queue)
{
return queue->size;
}
int main() {
struct Queue *queue = createqueue(5);
enque(queue,10);
enque(queue,20);
enque(queue,30);
deque(queue);
deque(queue);
deque(queue);
cout<<"Size of Queue: "<<size(queue)<<endl;
enque(queue,40);
enque(queue,50);
enque(queue,60);
enque(queue,70);
enque(queue,80);
enque(queue,90);
printf(queue);
return 0;
}
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct Queue{
int front,rear,size;
int capacity;
int *array;
};
struct Queue* createqueue(int capacity)
{
struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
queue->capacity = capacity;
queue->front = 0;
queue->rear = 0;
queue->size = 0;
queue->array = new int(capacity);
return queue;
}
int isempty(struct Queue *queue)
{
return (queue->size==0);
}
int isfull(struct Queue* queue)
{
return (queue->size==queue->capacity);
}
void enque(struct Queue* queue , int element)
{
if(isfull(queue)) return;
cout<<"Enqueue element is: "<<element<<endl;
queue->array[(queue->rear++)%queue->capacity] = element;
queue->size++;
}
void deque(struct Queue *queue)
{
if(isempty(queue)) return ;
cout<<"Dequeue element is: "<<queue->array[(queue->front++)%queue->capacity]<<endl;
queue->size--;
}
void printf(struct Queue* queue)
{
while(queue->front!=queue->rear)
{
cout<<"The element is: "<<queue->array[(queue->front++)%queue->capacity]<<endl;
}
}
int size(struct Queue* queue)
{
return queue->size;
}
int main() {
struct Queue *queue = createqueue(5);
enque(queue,10);
enque(queue,20);
enque(queue,30);
deque(queue);
deque(queue);
deque(queue);
cout<<"Size of Queue: "<<size(queue)<<endl;
enque(queue,40);
enque(queue,50);
enque(queue,60);
enque(queue,70);
enque(queue,80);
enque(queue,90);
printf(queue);
return 0;
}
No comments:
Post a Comment