Question

In: Computer Science

PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite...

PLEASE WRITE IN C++ PROGRAM THANKS

- QUEUES

Please study the code posted below.

Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same

/**
* Queue implementation using linked list C style implementation ( no OOP).
*/

#include <cstdio>
#include <cstdlib>
#include <climits>
#include <iostream>


#define CAPACITY 100 // Queue max capacity

using namespace std;
/** Queue structure definition */
struct QueueType
{
int data;
struct QueueType * next;
};
/** Queue size */
unsigned int size = 0;


int enqueue(QueueType * &rear, QueueType * &front, int data);
int dequeue(QueueType * &front);
int getRear(QueueType * &rear);
int getFront(QueueType * &front);
void display(QueueType * front);
int isEmpty();
int isFull();
string prepMenu();


int main()
{
int option, data;

QueueType *rear, *front;

rear = NULL;
front = NULL;
string menu = prepMenu();
cout << menu << endl;
cin >> option;
while (option !=7)
{

switch (option)
{
case 1:
cout << "\nEnter data to enqueue (-99 to stop): ";
cin >> data;
while ( data != -99)
{
/// Enqueue function returns 1 on success
/// otherwise 0
if (enqueue(rear, front, data))
cout << "Element added to queue.";
else
cout << "Queue is full." << endl;
cout << "\nEnter data to enqueue (-99 to stop): ";
cin >> data;
}


break;

case 2:
data = dequeue(front);

/// on success dequeue returns element removed
/// otherwise returns INT_MIN
if (data == INT_MIN)
cout << "Queue is empty."<< endl;
else
cout << "Data => " << data << endl;

break;

case 3:

/// isEmpty() function returns 1 if queue is emtpy
/// otherwise returns 0
if (isEmpty())
cout << "Queue is empty."<< endl;
else
cout << "Queue size => "<< size << endl;

break;

case 4:
data = getRear(rear);

if (data == INT_MIN)
cout << "Queue is empty." << endl;
else
cout << "Rear => " << data << endl;

break;

case 5:

data = getFront(front);

if (data == INT_MIN)
cout <<"Queue is empty."<< endl;
else
cout <<"Front => " << data << endl;

break;

case 6:
display(front);
break;

default:
cout <<"Invalid choice, please input number between (0-5).\n";
break;
}

cout <<"\n\n";
cout << menu<< endl;
cin >> option;
}
}

/**
* Enqueues/Insert an element at the rear of a queue.
* Function returns 1 on success otherwise returns 0.
*/
int enqueue(QueueType * &rear, QueueType * &front, int data)
{
QueueType * newNode = NULL;

/// Check queue out of capacity error
if (isFull())
{
return 0;
}

/// Create a new node of queue type
newNode = new QueueType;

/// Assign data to new node
newNode->data = data;

/// Initially new node does not point anything
newNode->next = NULL;

/// Link new node with existing last node
if ( (rear) )
{
rear->next = newNode;
}


/// Make sure newly created node is at rear
rear = newNode;

/// Link first node to front if its NULL
if ( !( front) )
{
front = rear;
}

/// Increment quque size
size++;

return 1;
}


/**
* Dequeues/Removes an element from front of the queue.
* It returns the element on success otherwise returns
* INT_MIN as error code.
*/
int dequeue(QueueType * &front)
{
QueueType *toDequque = NULL;
int data = INT_MIN;

// Queue empty error
if (isEmpty())
{
return INT_MIN;
}

/// Get element and data to dequeue
toDequque = front;
data = toDequque->data;

/// Move front ahead
front = (front)->next;

/// Decrement size
size--;

/// Clear dequeued element from memory
free(toDequque);

return data;
}


/**
* Gets, element at rear of the queue. It returns the element
* at rear of the queue on success otherwise return INT_MIN as
* error code.
*/
int getRear(QueueType * & rear)
{
// Return INT_MIN if queue is empty otherwise rear.
return (isEmpty())
? INT_MIN
: rear->data;
}


/**
* Gets, element at front of the queue. It returns the element
* at front of the queue on success otherwise return INT_MIN as
* error code.
*/
int getFront(QueueType * &front)
{
// Return INT_MIN if queue is empty otherwise front.
return (isEmpty())
? INT_MIN
: front->data;
}


/**
* Checks, if queue is empty or not.
*/
int isEmpty()
{
return (size <= 0);
}


/**
* Checks, if queue is within the maximum queue capacity.
*/
int isFull()
{
return (size > CAPACITY);
}
string prepMenu()
{

string menu = "";

menu+= " \n-------------------------------------------------------------------\n";
menu+= "1.Enqueue 2.Dequeue 3.Size 4.Get Rear 5.Get Front 6.Display 7.Exit\n";
menu+= "----------------------------------------------------------------------\n";
menu+= "Select an option: ";
return menu;
}
void display(QueueType * front)
{
for ( QueueType *t = front; t !=NULL; t = t->next)
cout <<t->data << " ";
cout << endl << endl;
}

Solutions

Expert Solution

Code: QueueUsingLinkList.cpp

#include <iostream>
#include<cstdlib>

using namespace std;

/** Queue structure definition */
template <class T>
class Node {
public:
   Node(); // default constructor
   Node(const T& data, Node<T>* next = NULL); // donstructor
   T data; // node data
   Node<T>* next; // node next pointer
};

template <class T>
class QueueType
{
        private:
                Node<T> *front, *rear;
                int size, CAPACITY;
        public:
                QueueType();
                ~QueueType();
                bool enqueue(T data);
                T dequeue();
                T getRear();
                T getFront();
                void display();
                bool isEmpty();
                bool isFull();
                int getSize();
};

template<class T>
Node<T>::Node()
{
}

// constructor
template<class T>
Node<T>::Node(const T& data, Node<T>* next)
{
   this->data = data;
   this->next = next;
}

/* Implementation of linked list */

// constructor
template <class T>
QueueType<T>::QueueType()
{
   rear = front = NULL;
   size = 0;
   CAPACITY= 100;
}

// destructor
template <class T>
QueueType<T>::~QueueType()
{
   Node<T>* current = front;
   while (current != 0) {
       Node<T>* next = current->next;
       delete current;
       current = next;
       size--;
   }
   front = NULL;
   rear = NULL;
}

/**
* Enqueues/Insert an element at the rear of a queue.
* Function returns True on success otherwise returns False.
*/
template <class T>
bool QueueType<T>::enqueue(T data)
{
        if(isFull()){
                return false;
        }
   Node<T> * newNode = new Node<T>(data, NULL);

   if (rear)
       rear->next = newNode;
        rear = newNode;

        if(!front)
   {
           front = rear;
   }
   size++;
   return true;
}

/**
* Dequeues/Removes an element from front of the queue.
*/
template <class T>
T QueueType<T>::dequeue()
{
   T data = front->data;
   Node<T> * temp = front;
   front = front->next;
   size--;
   free(temp);
   return data;
}

/**
* Checks, if queue is empty or not.
*/
template <class T>
bool QueueType<T>::isEmpty(){
        return (size<=0);
}

/**
* Checks, if queue is full or not.
*/
template <class T>
bool QueueType<T>::isFull(){
        return (size>CAPACITY);
}

/**
* display data of queue
*/
template <class T>
void QueueType<T>::display(){
        for ( Node<T> *t = front; t !=NULL; t = t->next)
                cout <<t->data << " ";
        cout << endl << endl;
}

/**
* Gets, element at front of the queue.
*/
template <class T>
T QueueType<T>::getFront()
{
        return front->data;
}

/**
* Gets, element at rear of the queue.
*/
template <class T>
T QueueType<T>::getRear()
{
        return rear->data;
}

/**
* Gets, size of the queue.
*/
template <class T>
int QueueType<T>::getSize()
{
        return size;
}

string prepMenu();

int main()
{
        int option, data;
        
        QueueType<int> queue;
        string menu = prepMenu();
        cout << menu << endl;
        cin >> option;
        while (option !=7)
        {
                switch (option)
                {
                case 1:
                        cout << "\nEnter data to enqueue (-99 to stop): ";
                        cin >> data;
                        while ( data != -99)
                        {
                                /// Enqueue function returns 1 on success
                                /// otherwise 0
                                if (queue.enqueue(data))
                                        cout << "Element added to queue.";
                                else
                                        cout << "Queue is full." << endl;
                                        cout << "\nEnter data to enqueue (-99 to stop): ";
                                        cin >> data;
                        }
                        break;
                
                case 2:
                        if(queue.isEmpty())
                                cout << "Queue is empty."<< endl;
                        else{
                                data = queue.dequeue();
                                cout << "Data => " << data << endl;
                        }
                        break;
                
                case 3:
                        /// isEmpty() function returns 1 if queue is emtpy
                        /// otherwise returns 0
                        if (queue.isEmpty())
                                cout << "Queue is empty."<< endl;
                        else
                                cout << "Queue size => "<< queue.getSize() << endl;
                        break;
                
                case 4:
                        if(queue.isEmpty())
                                cout <<"Queue is empty."<< endl;
                        else{
                                data = queue.getRear();
                                cout <<"Rear => " << data << endl;
                        }
                        break;
                
                case 5:
                        if(queue.isEmpty())
                                cout <<"Queue is empty."<< endl;
                        else{
                                data = queue.getFront();
                                cout <<"Front => " << data << endl;
                        }
                        break;
                
                case 6:
                        queue.display();
                        break;
                
                default:
                        cout <<"Invalid choice, please input number between (0-5).\n";
                        break;
        }
        
                cout <<"\n\n";
                cout << menu<< endl;
                cin >> option;
        }
}

//prepare Menu of this Program
string prepMenu()
{

string menu = "";

menu+= " \n-------------------------------------------------------------------\n";
menu+= "1.Enqueue 2.Dequeue 3.Size 4.Get Rear 5.Get Front 6.Display 7.Exit\n";
menu+= "----------------------------------------------------------------------\n";
menu+= "Select an option: ";
return menu;
}

Output:


Related Solutions

Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template....
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template. And rewrite your main function to test your template for integer array and double array. Following is my complete code: #include <iostream> using namespace std; class Array { private: // Pointer to memory block to store integers int* data; // Maximum size of memory block int cap; // Stores number of integers in an array int num; public: // Constructor Array(int size); // Default...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159 * radius * radius
*Please write code in C++* Write a program to verify the validity of the user entered...
*Please write code in C++* Write a program to verify the validity of the user entered email address.   if email is valid : output the stating that given email is valid. ex: "The email [email protected] is valid" else : output the statement that the email is invalid and list all the violations ex:  "The email sarahwinchester.com is invalid" * @ symbol * Missing Domain name The program should keep validating emails until user enter 'q' Upload your source code. ex: main.cpp
Code in C++ please You are going to write a program for Computer test which will...
Code in C++ please You are going to write a program for Computer test which will read 10 multiple choice questions from a file, order them randomly and provide the test to the user. When the user done the program must give the user his final score
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Instructions Write a program that implements...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Instructions Write a program that implements the algorithm given in Example 1 - 3 (Chapter 1), which determines the monthly wages of a salesperson. The instructions for Example 1 - 3have been posted below for your convenience. Example 1 - 3 Every salesperson has a base salary. The salesperson also receives a bonus at the end of each month, based on the following criteria: If the salesperson has been with...
Queues, Deques and Priority Queues. Enter the necessary code where indicated (using: MyQues.java - below and...
Queues, Deques and Priority Queues. Enter the necessary code where indicated (using: MyQues.java - below and questions) How would you use a stack to verify whether a given string is a palindrome or not? How would you check if a string is a palindrome or not, using queues and deques?
C++ code please: Write a program that first gets a list of integers from input. The...
C++ code please: Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates how much to multiply the array by. Finally, print out the entire array with each element multiplied by the last input. Assume that the list will always contain less than 20 integers. Ex: If the input is 4 4 8 -4 12...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT