1. Write a C++ program to implement queue using array.
#include<iostream>
using namespace std;
int queue[5], n=5, front=-1, rear=-1;
void Insert() {
int val;
if (rear==n-1)
cout<<“Queue Overflow”<<endl;
else {
if (front==-1)
front= 0;
cout<<“Insert the element in queue: “;
cin>>val;
rear++;
queue[rear]=val;
}
}
void Delete() {
if (front ==-1||front>rear) {
cout<<“Queue Underflow “;
return ;
} else {
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 : “;
cin>>ch;
switch(ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4:
cout<<“Exit”<<endl;
break;
default:
cout<<“Invalid choice”<<endl;
}
} while(ch!=4);
return 0;
}
Output:
1) Insert element to queue
2) Delete element from queue
3) Display all the elements of queue
4) Exit
Enter your choice : 1
Insert the element in queue : 12
Enter your choice : 1
Insert the element in queue : 23
Enter your choice : 1
Insert the element in queue : 45
Enter your choice : 2
Element deleted from queue is : 12
Enter your choice : 3
Queue elements are : 23 45
Enter your choice : 4
Exit
2. Write a C++ program to implement the queue data structure using class.
#include <iostream>
#define MAX_SIZE 5
using namespace std;
class Queue {
private:
int myqueue[MAX_SIZE],front,rear;
public:
Queue(){
front=-1;
rear=-1;
}
bool isFull(){
if(front==0 && rear==MAX_SIZE – 1){
return true;
}
return false;
}
bool isEmpty(){
if(front==-1) return true;
else return false;
}
void enQueue(int value){
if(isFull()){
cout << endl<< “Queue is full!!”;
} else {
if(front==-1) front = 0;
rear++;
myqueue[rear]=value;
cout<<value << ” “;
}
}
int deQueue(){
int value;
if(isEmpty()){
cout<<“Queue is empty!!”<<endl;
return(-1); }
else {
value=myqueue[front];
if(front>=rear) { //only one element in queue
front = -1;
rear = -1;
} else {
front++;
}
cout<<endl<<“Deleted=>”<<value<<“from
myqueue”;
return(value);
}
}
/* Function to display elements of Queue */
void displayQueue() {
int i;
if(isEmpty()) {
cout<<endl<<“Queue is Empty!!”<< endl;
}
else {
cout<<endl<<“Front=”<< front;
cout<<endl<<“Queue elements: “;
for(i=front;i<=rear;i++)
cout<<myqueue[i]<< “\t”;
cout<<endl<<“Rear=”<<rear<<endl;
}
}
};
int main()
{
Queue myq;
myq.deQueue();
cout<<“Queue created:”<<endl;
myq.enQueue(10);
myq.enQueue(20);
myq.enQueue(30);
myq.enQueue(40);
myq.enQueue(50);
//enqueue 60 => queue is full
myq.enQueue(60);
myq.displayQueue();
myq.deQueue();
myq.displayQueue();
return 0;
}
Output:
Queue is empty!!
Queue created:
10 20 30 40 50
Queue is full!!
Front = 0
Queue elements : 10 20 30 40 50
Rear = 4
Deleted =>10 from myqueue
Front = 1
Queue elements : 20 30 40 50
Rear = 4
Program iz Awesome! Its genuinely remarkable post, I have got much clear idea regarding from this post . Program iz
Live Coin Watch Pretty! This has been a really wonderful post. Many thanks for providing these details.
Pink Withney I just like the helpful information you provide in your articles
Fran Candelera Good post! We will be linking to this particularly great post on our site. Keep up the great writing
Blue Techker I just like the helpful information you provide in your articles
Just wanna tell that this is handy, Thanks for taking your time to write this.
I am always browsing online for posts that can aid me. Thx!
You must take part in a contest for top-of-the-line blogs on the web. I’ll advocate this site!
Thanks for another informative blog. Where else could I get that type of information written in such an ideal way? I’ve a project that I’m just now working on, and I’ve been on the look out for such information.
Appreciate it for helping out, fantastic info. “The health of nations is more important than the wealth of nations.” by Will Durant.
Keep up the superb piece of work, I read few content on this site and I think that your website is rattling interesting and has got sets of good information.
I think other web-site proprietors should take this website as an model, very clean and excellent user genial style and design, as well as the content. You are an expert in this topic!
I have recently started a blog, the information you offer on this website has helped me tremendously. Thank you for all of your time & work.
Some truly interesting points you have written.Aided me a lot, just what I was looking for : D.
Great V I should definitely pronounce, impressed with your website. I had no trouble navigating through all tabs as well as related information ended up being truly easy to do to access. I recently found what I hoped for before you know it in the least. Quite unusual. Is likely to appreciate it for those who add forums or anything, web site theme . a tones way for your client to communicate. Excellent task..
I must point out my gratitude for your kindness giving support to men and women who absolutely need guidance on the matter. Your special dedication to passing the message across ended up being wonderfully advantageous and have enabled people just like me to reach their endeavors. Your entire valuable facts indicates a great deal a person like me and especially to my office colleagues. Best wishes; from all of us.
I genuinely enjoy looking through on this web site, it has wonderful posts.
Thanks for another wonderful post. The place else could anybody get that kind of info in such a perfect means of writing? I have a presentation next week, and I’m on the search for such information.
You have brought up a very excellent points, appreciate it for the post.
I truly appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thank you again!
Merely a smiling visitor here to share the love (:, btw outstanding design and style. “Treat the other man’s faith gently it is all he has to believe with.” by Athenus.
Some genuinely nice and useful information on this internet site, also I believe the style has got fantastic features.
I am glad for writing to let you be aware of what a helpful encounter my wife’s daughter enjoyed studying your web site. She learned plenty of issues, which include how it is like to possess an awesome coaching heart to have many more smoothly know some complicated topics. You undoubtedly exceeded my expected results. Thanks for showing these practical, trustworthy, revealing and as well as unique tips about your topic to Julie.
I?¦ve recently started a site, the info you offer on this site has helped me tremendously. Thank you for all of your time & work.
I think other website proprietors should take this website as an model, very clean and great user friendly style and design, as well as the content. You’re an expert in this topic!
I have been examinating out many of your stories and i can state pretty clever stuff. I will make sure to bookmark your site.
There is apparently a lot to know about this. I feel you made certain good points in features also.
Sweet website , super design and style, real clean and apply friendly.
What’s Happening i’m new to this, I stumbled upon this I have found It absolutely helpful and it has helped me out loads. I hope to contribute & assist other users like its helped me. Great job.
I am glad to be a visitor of this utter weblog! , regards for this rare info ! .
Just about all of the things you state is supprisingly legitimate and it makes me wonder why I had not looked at this with this light previously. This particular piece really did turn the light on for me personally as far as this specific subject matter goes. Nevertheless there is actually one factor I am not really too cozy with and while I attempt to reconcile that with the core idea of your position, permit me observe what the rest of the readers have to say.Very well done.
This web site is really a walk-through for all of the info you wanted about this and didn’t know who to ask. Glimpse here, and you’ll definitely discover it.
Thanks for your marvelous posting! I seriously enjoyed reading it, you might be a great author.I will make sure to bookmark your blog and will eventually come back from now on. I want to encourage you continue your great work, have a nice morning!
I like this weblog very much so much good information.
WONDERFUL Post.thanks for share..extra wait .. …
Hi! This post couldn’t be written any better! Reading this post reminds me of my good old room mate! He always kept chatting about this. I will forward this page to him. Pretty sure he will have a good read. Thanks for sharing!
Sweet web site, super pattern, real clean and utilise genial.
Hello.This post was really interesting, particularly because I was looking for thoughts on this subject last Wednesday.
Very interesting info !Perfect just what I was looking for! “You have to be deviant if you’re going to do anything new.” by David Lee.
Some really good info , Gladiola I observed this.
Hey there, You’ve done an excellent job. I will definitely digg it and personally suggest to my friends. I’m confident they’ll be benefited from this site.
Way cool, some valid points! I appreciate you making this article available, the rest of the site is also high quality. Have a fun.
Hi my family member! I wish to say that this post is amazing, nice written and come with almost all important infos. I?¦d like to look more posts like this .
I¦ve been exploring for a bit for any high quality articles or weblog posts in this kind of house . Exploring in Yahoo I ultimately stumbled upon this site. Reading this information So i¦m satisfied to exhibit that I’ve a very good uncanny feeling I found out exactly what I needed. I so much definitely will make sure to do not fail to remember this web site and provides it a glance on a relentless basis.
Wow! This could be one particular of the most beneficial blogs We have ever arrive across on this subject. Basically Fantastic. I am also an expert in this topic so I can understand your hard work.
I haven’t checked in here for some time as I thought it was getting boring, but the last several posts are good quality so I guess I’ll add you back to my daily bloglist. You deserve it my friend 🙂
I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.
Hello there I am so thrilled I found your weblog, I really found you by accident, while I was searching on Digg for something else, Anyhow I am here now and would just like to say thank you for a tremendous post and a all round exciting blog (I also love the theme/design), I don’t have time to browse it all at the moment but I have saved it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the superb job.
You are a very bright individual!
Some truly nice and useful information on this web site, also I think the design has excellent features.
Regards for helping out, good info. “Courage comes and goes. Hold on for the next supply.” by Vicki Baum.
I’m not sure where you’re getting your info, but great topic. I needs to spend some time learning much more or understanding more. Thanks for great info I was looking for this info for my mission.
Thank you for every other wonderful post. Where else could anyone get that type of information in such a perfect way of writing? I’ve a presentation next week, and I’m on the search for such info.
Some genuinely nice stuff on this internet site, I enjoy it.
I have recently started a blog, the info you provide on this website has helped me tremendously. Thank you for all of your time & work.