Answered 2 days AfterMay 29, 2021

Answer To: Dummy

Mani answered on May 31 2021
142 Votes
Makefile
# use g++ with C++11 support
CXX=g++
CXXFLAGS=-Wall -pedantic -g -O0 -std=c++11
OUTFILES=QueueTest
all: $(OUTFILES)
QueueTest: QueueTest.cpp Queue.cpp Queue.h
    $(CXX) $(CXXFLAGS) -o QueueTest QueueTe
st.cpp Queue.cpp
clean:
    $(RM) $(OUTFILES) *.o
Queue.cpp
#include "Queue.h"
/**
* Implement Queue constructor
*/
Queue::Queue() {
head = NULL;
tail = NULL;
}
/**
* Implement the Queue destructor
*/
Queue::~Queue() {
Node* temp;
while(head) {
    temp = head;
    head = head->next;
    delete temp;
}
head = tail = NULL;
}
/**
* Implement size() correctly
*/
unsigned int Queue::size() {
Node* temp = head;
unsigned int count = 0;
while(temp) {
    temp = temp->next;
    count++;
}
return count;
}
/**
* Implement clear() correctly without memory leaks
*/
void Queue::clear() {
    Node* temp;
while(head) {
    temp = head;
    head = head->next;
    delete temp;
}
}
/**
* Implement push() correctly
*/
void Queue::push(string s) {
    // we will insert at the end - rear of the queue
    Node* new_node = new Node(s);
    // if queue is empty
    if(!head) {
        head = tail = new_node;
        return;
    }
    // if its not empty, update tail accordingly
    tail->next = new_node;
    tail = new_node;
}
/**
* Implement pop() correctly without memory leaks
*/
string Queue::pop() {
    // empty queue
if(!head) {
    return NULL;
}
Node* remove_node = head;
head = head->next;
// if list becomes empty on popping out
if(head==NULL) {
    tail = NULL;
}
string data = remove_node->data;
delete remove_node;
return data;
}
Queue.h
#ifndef QUEUE_H
#define QUEUE_H
#include
using namespace std;
/**
* Class to represent a Queue (FIFO) using a Singly-Linked List
*/
class Queue {
private:
/**
* Nested helper Node class
*/
class Node {
public:
/**
* The data stored within a Node
*/
string data;
/**
*...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here