Question

Write a C++ program to implement Queue ADT using singly linked structure. The program includes the...

Write a C++ program to implement Queue ADT using singly linked structure. The program includes the following:

Define the Queue class template in the header file QueueLinked.h.

// QueueLinked.h

#ifndef QUEUE_H

#define QUEUE_H

#include <iostream>

using namespace std;

template <typename T>

class Queue

{

public:

// Constructor

Queue();

//Desctructor

~Queue();

// Makes the queue to the empty state.

void make_empty();

// Checks if the queue is empty.

bool empty() const;

// Inserts item at the end of the queue.

void enqueue(const T& item)

// Removes the element at the start of the queue.

void dequeue();

// returns the front element

const T& front_element() const;

// Prints the elements of the queue.

void print() const

private:

struct NodeType

{

        T data;

        NodeType* next;

               

        NodeType(const T & d = T()): data(d)

        {

               next = nullptr;

        }    

};

NodeType* front;

NodeType* back;

};

#endif

Please read the comments carefully and implement the Queue class template.

You can implement the Queue class template in the seperate file QueueLinked.cpp.

// QueueLinked.cpp

#include "QueueLinked.h"

template <typename T>

Queue<T>::Queue()

{

front = nullptr;

back = nullptr;

}

// add other member functions

// ....

You also can put the implementation of the Queue class template in Queue.h.

// QueueLinked.h

#ifndef QUEUE_H

#define QUEUE_H

#include <iostream>

using namespace std;

template <typename T>

class Queue

{

public:

// Constructor

Queue()

{

front = nullptr;

back = nullptr;

}

// add other member functions

// ....

private:

struct NodeType

{

        T data;

        NodeType* next;

               

        NodeType(const T & d = T()): data(d)

        {

               next = nullptr;

        }    

};

NodeType* front;

NodeType* back;

};

#endif

The main function is contained in the file lab05.cpp.

// lab05.cpp

#include <string>

#include "QueueLinked.h"

#include "QueueLinked.cpp" // add if the interface and implementation are seperate

int main()

{

....

}

The main function,

Declare a queue which stores int values.

Prompt the user to enter integers, enqueue the entered values, stop entering the integers when the user enter -1.

Print the elements of queue.

Prompt the user to enter a number k, and dequeue k values from the front of the queue.

Print the elements of queue.

Declare a queue which stores strings.

Prompt the user to enter strings, enqueue the entered strings, stop entering the strings when the user enter exit.

Print the elements of queue.

Prompt the user to enter a number k, and dequeue k strings from the front of the queue.

Print the elements of queue.

The expected result:

Enqueue positive numbers (enter -1 to stop): 1 2 3 4 5 6 7 8 9-1

print queue: 1, 2, 3, 4, 5, 6, 7, 8, 9,

How many numbers to be removed from queue: 5

print queue: 6, 7, 8, 9,

Enqueue string (enter exit to stop): abc def ghi jkl mno pqr exit

print queue: abc, def, ghi, jkl, mno, pqr,

How many strings to be removed from queue: 5

print queue: pqr,

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

-------->QueueLinked.cpp<---------------

#include "QueueLinked.h"

template <typename T>
Queue<T>::Queue()
{
front = nullptr;
back = nullptr;
}

----------->Desctructor<-----------------
template <typename T>
Queue<T>::~Queue()
{
make_empty();
}

template <typename T>
void Queue<T>::make_empty()
{
NodeType *temp=front;
while(temp!=back)
{
temp=front;
front=front->next;
delete temp;
}
}


template <typename T>
bool Queue<T>::empty() const
{
return front==nullptr;
}


template <typename T>
void Queue<T>::enqueue(const T& item)
{
if(empty())
{
front=back=new NodeType(item);
return;
}

back->next=new NodeType(item);
back=back->next;
}


template <typename T>
void Queue<T>::dequeue()
{
if(!empty())
{
NodeType *temp=front;
front=front->next;
delete temp;
}
}


template <typename T>
const T& Queue<T>::front_element() const
{
return front->data;
}


template <typename T>
void Queue<T>::print() const
{
NodeType *temp=front;
while(temp!=back)
{
cout<<temp->data<<", ";
temp=temp->next;
}
cout<<temp->data<<endl;
}

// lab05.cpp
#include <string>
#include "QueueLinked.h"
#include "QueueLinked.cpp"

int main()
{
Queue<int> q1;
int n;
cout<<"Enter positive numbers(enter -1 to stop) : ";
  
while(true)
{
cin>>n;
if(n==-1)
break;
q1.enqueue(n);
  
  
}
cout<<" print queue: ";
q1.print();
  
Queue<string> q2;
cout<<" Enqueue string (enter exit to stop): ";
string input;
while(true)
{
cin>>input;
if(input=="exit")
break;
q2.enqueue(input);
}
  
cout<<" print queue: ";
q2.print();
   return 0;
}

SCREEN SHOT:

Add a comment
Know the answer?
Add Answer to:
Write a C++ program to implement Queue ADT using singly linked structure. The program includes the...
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 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...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities....

    QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...

  • (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...

    (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert the next element even if there is a space in front of queue. Efficiently implement a queue class using a circular...

  • Write a function that takes a string parameter and determines whether the string contains matching grouping...

    Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }. For example, the string {a(b+ac)d[xy]g} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Your function must use...

  • please write the code in C++ Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as...

    please write the code in C++ Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as shown below. // BinarySearchTree.h // after Mark A. Weiss, Chapter 4, Dr. Kerstin Voigt #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <cassert> #include <iostream> using namespace std; template <typename C> class BinarySearchTree { public: BinarySearchTree( ) : root{ nullptr } { } ~BinarySearchTree( ) { makeEmpty(); } const C & findMin( ) const { assert(!isEmpty()); return findMin( root )->element; } const C & findMax( ) const...

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

  • How do I pass values to this function? class DynIntQueue { struct QueueNode { int value;...

    How do I pass values to this function? class DynIntQueue { struct QueueNode { int value; QueueNode *next; QueueNode(int value1, QueueNode *next1 = nullptr) { value = value1; next = next1; } }; // These track the front and rear of the queue QueueNode *front; QueueNode *rear; public: // Constructor and Destructor DynIntQueue(); ~DynIntQueue(); // Member functions void enqueue(int); void dequeue(int &); bool isEmpty() const; void clear(); }; main #include <iostream> #include "DynIntQueue.h" using namespace std; int main() {DynIntQueue list;...

  • In C++ and use functions that are asked for, thanks! Implement the BinarySearchTree ADT in a...

    In C++ and use functions that are asked for, thanks! Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as shown below. // BinarySearchTree.h // after Mark A. Weiss, Chapter 4, Dr. Kerstin Voigt #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <cassert> #include <iostream> using namespace std; template <typename C> class BinarySearchTree { public: BinarySearchTree( ) : root{ nullptr } { } ~BinarySearchTree( ) { makeEmpty(); } const C & findMin( ) const { assert(!isEmpty()); return findMin( root )->element; } const C...

  • Please use the linked approach implement the BST ADT, implement all the functions in the BSTree.cpp....

    Please use the linked approach implement the BST ADT, implement all the functions in the BSTree.cpp. Add the ouput of the implementation. use recursive functions to traverse the tree - read the implementation notes on using helper functions. Please use C++ programming ////////////////////////////////////////////////////////////// #include "BSTree.h" template <typename DataType, class KeyType> BSTree<DataType, KeyType>::BSTreeNode::BSTreeNode ( const DataType &nodeDataItem, BSTreeNode *leftPtr, BSTreeNode *rightPtr ) { } template < typename DataType, class KeyType > BSTree<DataType, KeyType>::BSTree () {    root = 0; } template...

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