Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

FIC - CMPT 135 202202 - Assignment 2

In this assignment, we will be working with C++ classes to learn data structures in C++. A data structure is a     programmer defined data type (either using a class or using a struct) in order to store data in some structured way. To this end, we will build C++ classes to represent the nodes of linked lists and the linked list data             structure. We will then use the linked list data structure in order to store the unsigned binary, sign and             magnitude binary, and two's complement binary representations of decimal numbers.

For a detailed description of the linked list data structure, please refer to the supplementary material posted on Moodle together with this assignment.

Part I: Representing Linked Lists with C++ class

We may design a C++ class in order to represent a linked list. From our discussion in the supplementary          material, we observe that a linked list class requires only one member variable which is the head pointer and then we could implement all the functions discussed in the supplementary material as member functions.

You are provided the following Node class declaration and definition as well as LinkedList class declaration. The definitions of the member and friend functions of the LinkedList class are almost identical to the non-  member functions discussed in the supplementary material. The only difference is that the head pointer           parameter of the non-member functions discussed in the supplementary material is removed from these          member and friend functions because the head pointer is a member variable of the class and therefore each    of these member and friend functions has access to it and doesn't need it as a parameter.

#include <iostream>

#include <cassert>

using namespace std;

class Node

{

typedef Node* NodePtr;

private:

int data;

NodePtr link;

public:

Node();

Node(const int &);

Node(const Node &);

int getData() const;

NodePtr getLink() const;

void setData(const int &);

void setLink(const NodePtr &);

friend ostream& operator << (ostream &, const Node &);

};

typedef Node* NodePtr;

Node::Node() : data(0), link(nullptr) {}

Node::Node(const int &d) : data(d), link(nullptr){}

Node::Node(const Node &n) : data(n.data), link(n.link){}

int Node::getData() const { return data; }

NodePtr Node::getLink() const { return link; }

void Node::setData(const int &d) { data = d; }

void Node::setLink(const NodePtr &p) { link = p; }

ostream& operator << (ostream& out, const Node& n)

{

out << n.data;

return out;

}

typedef Node* NodePtr;

class LinkedList

{

private:

NodePtr head;

public:

LinkedList(); //default constructor: assigns the head pointer member variable a nullptr value LinkedList(const LinkedList &); //copy constructor (deep copy)

~LinkedList(); //destructor (must delete all the nodes from the heap) LinkedList& operator= (const LinkedList &); //Assignment operator (deep copy)

bool isEmpty() const; //return true if the length of the calling object is 0 and false otherwise NodePtr getHeadPtr() const; //return the head member variable of the calling object int getLength() const; //return the number of nodes in the calling object

void head_insert(const int &); //as described in the supplementary material

NodePtr search_node(const int &) const; //as described in the supplementary material

void insert_after(const NodePtr &, const int &) const; //as described in the supplementary material

void remove_node(const NodePtr &); //as described in the supplementary material void remove_node(const int &); //as described in the supplementary material void remove_all(const int &); //as described in the supplementary material void tail_insert(const int &); //as described in the supplementary material

void insert_before(const NodePtr &, const int &); //as described in the supplementary material

friend ostream& operator << (ostream&, const LinkedList &); //Implemented for you };

ostream& operator << (ostream &out, const LinkedList &LL)

{

if (LL.isEmpty())

out << "EMPTY LINKED LIST";

else

{

NodePtr temp = LL.head;

while(temp != nullptr)

{

out << *temp << " ";

temp = temp->getLink();

}

}

return out;

}

You are required to implement the LinkedList class taking into account the following restrictions and requirements.

Restrictions and Requirements

You are not allowed to add or remove any include directive, namespace, member function, or friend

function to the provided Node and LinkedList classes.

You are not allowed to declare, define, or use any container variables (objects) such as static arrays,

dynamic arrays, or any STL container such as vectors in your LinkedList class definition.

You are not allowed to use any STL algorithm in your LinkedList class definition.

Testing the Node and LinkedList classes

You are provided the following test program and its sample run output in order to help you test your program. Please note that you don't need to submit this test program when you submit your work. It is only provided to help you confirm your classes are built correctly and you can then use them in the next section of the                assignment. Please make sure this test program works without any syntax, runtime, linking, or semantic errors


in order for you to be successful in the next section.