Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

using namespace std;

template

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

Queue::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

using namespace std;

template

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

#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,

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And Transact SQL

Authors: Lucy Scott

1st Edition

1974679985, 978-1974679980

More Books

Students also viewed these Databases questions

Question

What are the criteria for selecting investments?

Answered: 1 week ago

Question

7. Define cultural space.

Answered: 1 week ago

Question

8. Describe how cultural spaces are formed.

Answered: 1 week ago