Question

Please write in c++ language: -Create a queue -Insert elements {1, 2, 3, 4, 5} -Print...

Please write in c++ language:

-Create a queue
-Insert elements {1, 2, 3, 4, 5}
-Print alternate numbers from queue

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;
int queue[100], n = 100, 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 : "<<endl;
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 : "<<endl;
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 :
1
Enter your choice :
1
Insert the element in queue :
2
Enter your choice :
1
Insert the element in queue :
3
Enter your choice :
1
Insert the element in queue :
4
Enter your choice :
1
Insert the element in queue :
5
Enter your choice :
3
Queue elements are : 1 2 3 4 5
Enter your choice :
4
Exit


Process exited normally.
Press any key to continue . . .

Add a comment
Know the answer?
Add Answer to:
Please write in c++ language: -Create a queue -Insert elements {1, 2, 3, 4, 5} -Print...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program to : Create array arr1 of size 10 Assign values to elements...

    Write a C++ program to : Create array arr1 of size 10 Assign values to elements such that - say index = i, arr1[i] = 10 - i. Print arr1 Create arr2 and copy alternate elements from arr1 starting from index 1. Print arr2. Sort arr1 in ascending array and print its elements from indices 3 to 8 Print every output on a different line and separate array elements by spaces.

  • Write a C++ program to implement a queue using linked lists. You can use the queue...

    Write a C++ program to implement a queue using linked lists. You can use the queue data structure from the Standard Template Library (STL). The program should provide the following functionality: Enqueue data into queue Dequeue data from queue Print data at the front Print data at the back Print the entire queue Check if the queue is empty Print the number of elements in the queue Test your program using at least the following test cases (considering the queue...

  • write a function to insert an element in the middle of a queue. Please use C++...

    write a function to insert an element in the middle of a queue. Please use C++ and make sure its a function. Thanks

  • Using C++ Insert elements into a partially filled array. a) Create a partially filled array of...

    Using C++ Insert elements into a partially filled array. a) Create a partially filled array of CAPACITY 100. b) Initialize the array with values 10,20,30,40,50,60,70,80,90,100 c) Create a print function for the array. d) Create an insert function which inserts an integer into the array in sorted position. e) Insert the numbers 5, 150 and 55. f) Print the array before and after each insertion. Output Example 10 20 30 40 50 60 70 80 90 100 5 10 20...

  • please do it in C# language Q. 1 part 1 Create an array named weekly Temp...

    please do it in C# language Q. 1 part 1 Create an array named weekly Temp that will hold values of weekly temperatures in Fahrenheit from Monday through Sunday. Calculate the average temperature for that week and display its average. part 2 Write while loop to display the numbers from 10 through 1 and the squares. The output should display as following: squared 100 81 04 part 3 Define a method that calculates calculateareaOfCircle by passing a value of radius...

  • LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display...

    LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display the output using inorder, preorder and postorder tree traversal methods.

  • Using the C Language - Calculate and print the first 20 elements of the Fibonacci series....

    Using the C Language - Calculate and print the first 20 elements of the Fibonacci series. You must use the recursive function call method to calculate the elements. Output the elements of the series in a fixed column width table format and calculate Phi for each step. Output how many iterations it would take to approach the theoretical value of Phi within 0.1%. For example: N    Fibonacci Phi -------------------------------------------------- 1 0    N/A 2    1    N/A 3...

  • use C language please Problem 5. (5 points] Use string functions. Write a program that: 1)...

    use C language please Problem 5. (5 points] Use string functions. Write a program that: 1) Ask the user to write two strings. 2) If both strings are equal, print an appropriate message then print out both strings with their length. 3) If the strings are not equal, print an appropriate message then concatenate both strings and print out the length of the concatenating strings. 4) In the main function prints both arrays.

  • using C language Create an array of doubles with 5 elements. In the array prompt the...

    using C language Create an array of doubles with 5 elements. In the array prompt the user to enter 5 temperature values (in degree Fahrenheit). Do this within main. Create a user defined function called convert2Cels. This function will not return any values to main with a return statement. It should have parameters that include the temperature array and the number of elements. The function should convert the values for Fahrenheit to Celsius and save them back into the same...

  • Answer should be in C programming language Write a program that will create an array of...

    Answer should be in C programming language Write a program that will create an array of 10 floating point numbers and take input 10 floats from keyboard. The program will print the position (index) of the maximum and minimum number among them

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT