Question

I 7/ Fig. 18.2: Stack.h 2 // Stack class template. #ifndef #de fine 3 STACK-H STACK-H 5 #include <deque> 7 template<typename

Define a Queue Class Template Queue.h that has member functions: 1. front returns a reference to the Queues front element 2.


Please Answer this question using the language of C++. I provide you with the picture of figure 18_02. Thank you.

Comments
    Answer #1

    As i can see that you have implemented stack with the help of deque. deque stands for double ended queue i.e. you can insert and delete from both end. you want to use stl i.e. standard template library so i'll show you how you can use deque in stl for implementing standard queue.

    #ifndef QUEUE_H

    #define QUEUE_H

    #include<deque>

    #include<iostream>

    using namespace std;

    template <class T>

    class Queue

    {

    public:

    //return the first element of the queue

    const T& front()

    {

       return queue.front();// it will return the first element's reference

    }

    // return the back element of the queue

    const T& back()

    {

       return queue.back();// it will return the back element's reference
    }

    // push element at the back of the queue

    void push(const T& pushValue)

    {

    queue.push_back(pushValue);

    }

    // pop element from the front of the queue

    void pop()

    {

    queue.pop_front();

    }

    // determine whether queue is empty

    bool isEmpty() const{

    return queue.empty();

    }

    //return size of the queue

    size_t size() const{

    return queue.size();

    }

    // to show the current queue
    void show()
    {
       typename deque <T>::iterator it;
       for(it=queue.begin();it!=queue.end();++it)
           cout<<'\t'<<*it;
       cout<<'\n';
    }
    private:

    deque <T> queue;

    };

    #endif


    int main()
    {
       Queue <int> qu;
       qu.push(10);
       qu.push(20);
       qu.push(30);
       cout<<"the queue is";
       qu.show();
       cout<<"\n queue size:"<<qu.size();
       cout<<"\n queue front:"<<qu.front();
       cout<<"\n queue back:"<<qu.back();
       qu.pop();
       cout<<"\n after removal of element:"; //value 10 is removed as this is the first element inserted
       qu.show();
       return 0;
    }

    Add a comment
    Know the answer?
    Add Answer to:
    Please Answer this question using the language of C++. I provide you with the picture of figure 1...
    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 need help fixing my code.   My output should be the following. Hello, world! : false...

      I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

    • // thanks for helping // C++ homework // The homework is to complete below in the...

      // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

    • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

      HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #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 top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

    • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

      - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

    • Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles...

      Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...

    • (C++) Two stacks of the same type are the same if they have the same number...

      (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

    • I was told I need three seperate files for these classes is there anyway to tie...

      I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

    • template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() =...

      template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...

    • Stack.h Stack_test.cpp What would happen if we add s2.pop(); or cout << s2.top(); to the end...

      Stack.h Stack_test.cpp What would happen if we add s2.pop(); or cout << s2.top(); to the end of the program (when s2 is empty?) Fix Stack.h so that these problems would not occur. #ifndef STACK H #define STACK H /1 your name // Stack.h // date // description #include «vector» using namespace std; template <typename T> class Stack vector<T> container; public: Stack (): container) void push (T x) container.push back(x); void pop) container.pop back); T top) t return container.back); bool empty()...

    • C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise...

      C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise 1 for the class linkedStackType question one:        Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...

    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