Ex 5 Queue
Ex 5 Queue
NO:5 QUEUE
20.01.21
AIM:
Program;
#include <iostream>
void enqueue() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cin>>val;
rear++;
queue[rear] = val;
void Dequeue() {
return ;
} else {
T.PRABAVATHI(MTECH)
20MA32
cout<<"Element deleted from queue is : "<< queue[front] <<endl;
front++;
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<queue[i]<<" ";
cout<<endl;
int main() {
int ch;
cout<<"4) Exit"<<endl;
do {
cin>>ch;
switch (ch) {
case 1: enqueue();
break;
case 2: Dequeue();
T.PRABAVATHI(MTECH)
20MA32
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
} while(ch!=4);
return 0;
Output:
Program:
#include<iostream>
#include<stdlib.h>
T.PRABAVATHI(MTECH)
20MA32
struct node {
int data;
};
void enqueue() {
int val;
cin>>val;
if (rear == NULL) {
rear->next = NULL;
rear->data = val;
front = rear;
} else {
rear->next = newnode;
newnode->data = val;
newnode->next = NULL;
rear = newnode;
void Dequeue() {
newnode = front;
if (front == NULL) {
T.PRABAVATHI(MTECH)
20MA32
cout<<"no element "<<endl;
return;
else
if (newnode->next != NULL) {
free(front);
front = newnode;
} else {
free(front);
front = NULL;
rear = NULL;
void Display() {
newnode = front;
cout<<"Queue is empty"<<endl;
return;
cout<<newnode->data<<" ";
newnode = newnode->next;
T.PRABAVATHI(MTECH)
20MA32
}
cout<<endl;
int main() {
int ch;
cout<<"4) Exit"<<endl;
do {
cin>>ch;
switch (ch) {
case 1: enqueue();
break;
case 2: Dequeue();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
} while(ch!=4);
return 0;
T.PRABAVATHI(MTECH)
20MA32
Output:
Program:
#include<iostream>
void push1(int);
void push2(int);
int pop1();
T.PRABAVATHI(MTECH)
20MA32
int pop2();
void enqueue();
void dequeue();
void display();
void create();
int count = 0;
int main(){
int ch;
cout<<" 4.Exit"<<endl;
create();
while (1){
cout<<"\nEnter choice";
cin>>ch;
switch (ch){
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
T.PRABAVATHI(MTECH)
20MA32
break;
case 4:
// Exit(0);
default:
cout<<"Wrong choice";
}}}
void create(){
st1[++top1] = data;}
int pop1(){
return(st1[top1--]);}
st2[++top2] = data;}
int pop2(){
return(st2[top2--]);}
void enqueue(){
int data, i;
cin>>data;
push1(data);
count++;
void dequeue(){
int i;
push2(pop1());}
T.PRABAVATHI(MTECH)
20MA32
pop2();
count--;
push1(pop2());
void display(){
int i;
Output:
Program:
#include<bits/stdc++.h>
T.PRABAVATHI(MTECH)
20MA32
void reverseQueue(queue<int> &q) {
// Base case
if (q.empty()) {
return;
q.pop();
reverseQueue(q);
q.push(curr);
int main() {
cout<<endl;
// Example 2
queue<int> q2;
q2.push(11);
q2.push(5);
q2.push(6);
q2.push(4);
q2.push(0);
q2.push(21);
T.PRABAVATHI(MTECH)
20MA32
q2.push(9);
q2.push(2);
reverseQueue(q2);
while (!q2.empty()) {
cout<<q2.front()<<" ";
q2.pop();
cout<<endl;
return 0;
Output:
Program:
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
int q[50];
if(r==l-1){
T.PRABAVATHI(MTECH)
20MA32
cout<<"Queue is full";}
else if((f==-1)&&(r==-1)){
f = r = 0;
q[r] = data;}
else{
r++;
q[r] = data;
}}
void print(){
int i;
for(i=f;i<=r;i++){
cout<<" "<<q[i];
}}
void reverse(){
int i,j,t;
for(i=f,j=r;i<j;i++,j--){
t = q[i];
q[i] = q[j];
q[j] = t;
}}
int main(){
int n,i=0,t;
cin>>n;
cout<<"\nEnter Queue:";
while(i<n){
cin>>t;
T.PRABAVATHI(MTECH)
20MA32
enqueue(t,n);
i++;
print();
reverse();
print();
Output:
T.PRABAVATHI(MTECH)
20MA32