Question

IMPLEMENT IN C++ 2. Although a queue is "best" implemented with a list, it can be...

IMPLEMENT IN C++

2. Although a queue is "best" implemented with a list, it can be implemented with a vector if you take into account the starting position of the queue. For example, if five elements are pushed onto the queue, the start of the queue is at position zero and the end is at position 4. If we, then, pop two elements, the start would be at position 2 and the end at position 4. The two "popped" elements are not really removed from the vector, and that avoids the O(N) time problem for the pop function.
Implement a class which uses a vector to store the queue. Be mindful of performance, such that if the queue is empty, the size of the underlying vector is "reset."

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

#include<iostream>

#include<vector>

using namespace std;

//templated Queue class using vector

template<class T>

class Queue{

                vector<T> data;

                int startPos; //start position of front element

                int count; //current number of elements in queue

public:

                Queue(){

                                //setting 0 as startPos and count

                                startPos=0;

                                count=0;

                }

                //method to add an element to the back

                void enqueue(T element){

                                //simply appending element to the end and updating count

                                data.push_back(element);

                                count++;

                }

                //method to remove front element

                void dequeue(){

                                //checking if queue is not empty

                                if(count>0){

                                                //simply advancing startPos

                                                startPos++;

                                                //decrementing size

                                                count--;

                                                //if count is 0, resetting vector, setting startPos to 0

                                                if(count==0){

                                                                data.clear();

                                                                startPos=0;

                                                }

                                }

                }

               

                //returns the front element without removing

                T front() const{

                                //assuming queue is not empty, returning element at startPos

                                return data[startPos];

                }

               

                //returns true if queue is empty

                bool isEmpty() const{

                                return count==0;

                }

};

//main method for testing

int main(){

                //creating an integer queue, adding numbers 1 to 5

                Queue<int> q;

                for(int i=1;i<=5;i++){

                                q.enqueue(i);

                }

                //displaying and removing two front values,

                cout<<q.front()<<endl; //1

                q.dequeue();

                cout<<q.front()<<endl; //2

                q.dequeue();

                //adding 6 and 7

                q.enqueue(6);

                q.enqueue(7);

                //looping until q is empty

                while(!q.isEmpty()){

                                //displaying and removing front value

                                cout<<q.front()<<endl; //should display values 3 to 7 in each line

                                q.dequeue();

                }

                return 0;

}

/*OUTPUT*/

1

2

3

4

5

6

7

Add a comment
Know the answer?
Add Answer to:
IMPLEMENT IN C++ 2. Although a queue is "best" implemented with a list, it can be...
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
  • I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (ont...

    I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (onto the queue) or pushed and popped (onto the stack). I are required to use STL data structures to implement and create the stack and queue for my program. ----- testfile1.tst -------- enqueue 5 enqueue 7 push blooper push rookie dequeue push demerits pop enqueue 3 enqueue 8 push showplace enqueue 9 dequeue pop dequeue push palmetto push zebra...

  • // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. //...

    // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. // the variable: back "points" at the first node in the linked list // new elements ( enqueued) are added at the back // the variable: front "points" at the last node in the linked list. // elements are removed (dequeued) from the front // // Several queue instance methods are provided for you; do not change these // Other instance methods are left for...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • Implement a c++ 'List' class to handle a list with general operations. That means you can...

    Implement a c++ 'List' class to handle a list with general operations. That means you can insert and delete any element anywhere in the list. The list has no order, except for the order you insert or delete.   The methods you are to implement are as given: Constructor methods (two, default and copy constructor, a list to a newly defined list, ie 'List listA(listB)' ) empty returns true or false if list is empty or not. front makes current position...

  • // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations // ...

    // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations //   - (i.e. queue_t* create_queue(unsigned int _capacity) should not have additional arguments) // - You should not have any 'printf' statements in your queue functions. //   - (You may consider using these printf statements to debug, but they should be removed from your final version) // ==================================================...

  • Code in C++. Can someone make it so that the code below can be compiled? ▪...

    Code in C++. Can someone make it so that the code below can be compiled? ▪ Creating an empty queue ▪ Inserting a value ▪ Removing a value ▪ Finding the size of the queue ▪ Printing the contents of the queue ▪ Adding the contents of one queue to the end of another ▪ Merging the contents of two queues into a third, new, queue Class Attributes Your class should be implemented using a linked list and should have...

  • Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your...

    Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue (ABQ). A stack is a linear data structure which follows the Last-In, First-Out (LIFO) property. LIFO means that the data most recently added is the first data to be removed. (Imagine a stack of books, or a stack of papers on a desk—the first one to be removed is the last one placed on top.) A queue...

  • 1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class...

    1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...

  • 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...

  • e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return t...

    e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } } f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex...

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