Question

C++ Data Strucutres and Algorithm

Topic: Queues ADT

Exercise 3 (Must use linked structure-based Queue) rite an application level function with the following signature: Queue Type joinQueuesSeg Const QueueType gl. const Queue q2) Type& It should return a new queue containing all qi items followed by all q2 items sequentially, while leaving gl and q2 unchanged For example: If gl contains {1. 3, 5, 7) and g2 contains 12.4) the new queue will contain 11, 3, 5, 7, 2, 4

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

#include <iostream>
#include <cstdlib>
#include <queue>

using namespace std;

queue<int> joinQueueSeq(queue<int>& q1,queue<int>& q2){
queue<int> q;
while (!q1.empty()) {
q.push(q1.front());
q1.pop();
}
while (!q2.empty()) {
q.push(q2.front());
q2.pop();
}
return q;
}

int main(void) {
srand(time(0));
queue<int> q1,q2,q;
int i,temp;
for (i = 0; i < 5; ++i){
temp=rand()%10;
cout<<temp<<" ";
q1.push(temp);
}
cout<<endl;
for (i = 0; i < 10; ++i){
temp=rand()%10;
cout<<temp<<" ";
q2.push(temp);
}
cout<<endl;
  
q = joinQueueSeq(q1,q2);

cout << "Contents of resultant queue" << endl;
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}

return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ Data Strucutres and Algorithm Topic: Queues ADT Write an application level function with the following...
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
  • In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and...

    In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and the Stack ADT. In addition, you will be using two different implementations for each ADT: Array Based Linked You will also be writing a driver to test your Queue and Stack implementations and you will be measuring the run times and memory use of each test case. You will also be adding some functionality to the TestTimes class that you created for Homework 1....

  • 1) Write a segment of code (application level) to perform each of the following operations. Assume...

    1) Write a segment of code (application level) to perform each of the following operations. Assume myStack is an object of the class ArrayStack. You may call any of the public methods of ArrayStack. You may declare additional stack objects. a) Set secondElement to the second element from the top of myStack, leaving myStack without its original top two elements. b) Set bottom equal to the bottom element in myStack, leaving myStack empty. c) Set bottom equal to the bottom...

  • True or False: 1. Much like a list, a set contains items that are in a...

    True or False: 1. Much like a list, a set contains items that are in a particular order. ANSWER 2. With a set, the difference and subset operations are not symmetric. ANSWER 3. The submethod of the set class returns a set containing items in s1 that are not in s2. ANSWER: 4. If SI and s2 are sets, the expression s1.issubset(s2) returns True if s2 is a subset of s1. ANSWER 5.A set is similar to a bag, but...

  • Queues This programming exercise introduces the Queue data structure. The students must create all the necessary...

    Queues This programming exercise introduces the Queue data structure. The students must create all the necessary methods for the queue and use the queue in a Java program. Step 1 - Create a new project in Netbeans Use the following naming convention: “Module4_Lastname_Assignment1”. Step 2 - Build a solution You have been asked to create a customer service program for a new phone store that will be opening soon. Since this company anticipates being the agent for a rising new...

  • help me answer the following questions please 1. Stack (LIFO) & its application 1. Stack overflow...

    help me answer the following questions please 1. Stack (LIFO) & its application 1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking Exercises: 1) Which of the following applications may use a stack? A. parentheses balancing program. B. Keeping track of local variables at run time. C. Syntax analyzer for a compiler. D. All of the above. 2) Consider the usual algorithm for determining whether a sequence of parentheses is balanced....

  • 2. Given two unsorted STL lists X and P, write a valid C++ function intersection(X, P)...

    2. Given two unsorted STL lists X and P, write a valid C++ function intersection(X, P) that returns a new list that contains the elements common to both X and P. For example, if X = 5, 2, 1, 4 and P = 4, 5, 7; your algorithm should return a new list containing 5 and 4 (order is not important). Also specify the running time of your algorithm using Big-Oh notation. Hint: As the lists are unsorted, a straightforward...

  • Q1)(20p)Write a static member function called removeLongestRepeatingSegment that takes a String a...

    Q1)(20p)Write a static member function called removeLongestRepeatingSegment that takes a String argument. The method will return a new String by removing the logest segment that contains consequtively repeating characters. public static void main(String []args){ String s=”1111222223333311111111444455552222”; String res= removeLongestRepeatingSegment(s); will return “11112222233333444455552222” } public static String removeLongestRepeatingSegment(String s){ --------------- Q2)15p)Given a list of objects stored (in ascending order) in a sorted linked list, implement member method which performs search as efficiently as possible for an object based on a key....

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • Design and implement a class Q that uses Q.java as a code base. The queue ADT...

    Design and implement a class Q that uses Q.java as a code base. The queue ADT must use class LinkedList from Oracle's Java class library and its underlying data structure (i.e. every Q object has-a (contains) class LinkedList object. class Q is not allowed to extend class LinkedList. The methods that are to be implemented are documented in Q.java. Method comment blocks are used to document the functionality of the class Q instance methods. The output of your program must...

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

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