0% found this document useful (0 votes)
56 views

Ex 5 Queue

The document discusses different implementations of queues: 1. Using an array, with functions to enqueue, dequeue and display elements. 2. Using a linked list, with similar enqueue, dequeue and display functions. 3. Using two stacks, by pushing elements to one stack and popping them to another to reverse the order and simulate a queue. 4. Reversing the order of a queue using recursion by removing the front element, recursively calling on the remaining queue, and adding the element to the back. 5. Reversing a stack using recursion by recursively calling on the remaining elements and prepending removed elements.

Uploaded by

Prabavathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Ex 5 Queue

The document discusses different implementations of queues: 1. Using an array, with functions to enqueue, dequeue and display elements. 2. Using a linked list, with similar enqueue, dequeue and display functions. 3. Using two stacks, by pushing elements to one stack and popping them to another to reverse the order and simulate a queue. 4. Reversing the order of a queue using recursion by removing the front element, recursively calling on the remaining queue, and adding the element to the back. 5. Reversing a stack using recursion by recursively calling on the remaining elements and prepending removed elements.

Uploaded by

Prabavathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

EX.

NO:5 QUEUE

20.01.21

AIM:

To understand the concept of queue

1. Implementation of queue using array

Program;

#include <iostream>

using namespace std;

int queue[100], n = 100, front = - 1, rear = - 1;

void enqueue() {

int val;

if (rear == n - 1)

cout<<"Queue Overflow"<<endl;

else {

if (front == - 1)

front = 0;

cout<<"Insert the element in queue : "<<endl;

cin>>val;

rear++;

queue[rear] = val;

void Dequeue() {

if (front == - 1 || front > rear) {

cout<<"Queue Underflow ";

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 elements are : ";

for (int i = front; i <= rear; i++)

cout<<queue[i]<<" ";

cout<<endl;

int main() {

int ch;

cout<<"1) Insert element to queue"<<endl;

cout<<"2) Delete element from queue"<<endl;

cout<<"3) Display all the elements of queue"<<endl;

cout<<"4) Exit"<<endl;

do {

cout<<"Enter your choice : "<<endl;

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;

default: cout<<"Invalid choice"<<endl;

} while(ch!=4);

return 0;

Output:

2. Implementation of queue using linked list

Program:

#include<iostream>

#include<stdlib.h>

using namespace std;

T.PRABAVATHI(MTECH)
20MA32
struct node {

int data;

struct node *next;

};

struct node* front =NULL;

struct node* rear =NULL;

struct node* newnode;

void enqueue() {

int val;

cout<<"Insert the element in queue : "<<endl;

cin>>val;

if (rear == NULL) {

rear = (struct node *)malloc(sizeof(struct node));

rear->next = NULL;

rear->data = val;

front = rear;

} else {

newnode=(struct node *)malloc(sizeof(struct node));

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) {

newnode = newnode->next; //initially the front is point to newnode we hsave to


delete the first element noew update the newnode as new of next

cout<<"Element deleted from queue is : "<<front->data<<endl;

free(front);

front = newnode;

} else {

cout<<"Element deleted from queue is : "<<front->data<<endl;

free(front);

front = NULL;

rear = NULL;

void Display() {

newnode = front;

if ((front == NULL) && (rear == NULL)) {

cout<<"Queue is empty"<<endl;

return;

cout<<"Queue elements are: ";

while (newnode != NULL) {

cout<<newnode->data<<" ";

newnode = newnode->next;

T.PRABAVATHI(MTECH)
20MA32
}

cout<<endl;

int main() {

int ch;

cout<<"1) Insert element to queue"<<endl;

cout<<"2) Delete element from queue"<<endl;

cout<<"3) Display all the elements of queue"<<endl;

cout<<"4) Exit"<<endl;

do {

cout<<"Enter your choice : "<<endl;

cin>>ch;

switch (ch) {

case 1: enqueue();

break;

case 2: Dequeue();

break;

case 3: Display();

break;

case 4: cout<<"Exit"<<endl;

break;

default: cout<<"Invalid choice"<<endl;

} while(ch!=4);

return 0;

T.PRABAVATHI(MTECH)
20MA32
Output:

3. Implementation of queue using stack

Program:

#include<iostream>

using namespace std;

void push1(int);

void push2(int);

int pop1();

T.PRABAVATHI(MTECH)
20MA32
int pop2();

void enqueue();

void dequeue();

void display();

void create();

int st1[100], st2[100];

int top1 = -1, top2 = -1;

int count = 0;

int main(){

int ch;

cout<<" 1.Enqueue element into queue"<<endl;

cout<<" 2.Dequeue element from queue"<<endl;

cout<<"3. Display from queue"<<endl;

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(){

top1 = top2 = -1;}

void push1(int data){

st1[++top1] = data;}

int pop1(){

return(st1[top1--]);}

void push2(int data){

st2[++top2] = data;}

int pop2(){

return(st2[top2--]);}

void enqueue(){

int data, i;

cout<<"Enter data into queue";

cin>>data;

push1(data);

count++;

void dequeue(){

int i;

for (i = 0;i <= count;i++){

push2(pop1());}

T.PRABAVATHI(MTECH)
20MA32
pop2();

count--;

for (i = 0;i <= count;i++){

push1(pop2());

void display(){

int i;

for (i = 0;i <= top1;i++){

cout<< " " <<st1[i];

Output:

4. Reverse data in queue using recursion

Program:

#include<bits/stdc++.h>

using namespace std;

T.PRABAVATHI(MTECH)
20MA32
void reverseQueue(queue<int> &q) {

// Base case

// reverse of an empty queue is an empty queue

if (q.empty()) {

return;

// remove an element from queue and store it in a variable, say curr

int curr = q.front();

q.pop();

// recursively call the reverseQueue method on remaining queue

reverseQueue(q);

// add the removed element to the end of the reversed queue

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:

5. Reverse a stack using recursion

Program:

#include <stdio.h>

#include <stdlib.h>

#include<iostream>

using namespace std;

int f = -1,r = -1;

int q[50];

void enqueue(int data,int l){

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;

cout<<" size of Queue:";

cin>>n;

cout<<"\nEnter Queue:";

while(i<n){

cin>>t;

T.PRABAVATHI(MTECH)
20MA32
enqueue(t,n);

i++;

cout<<"\n queue elements:";

print();

reverse();

cout<<"\n after reversing:";

print();

Output:

T.PRABAVATHI(MTECH)
20MA32

You might also like