#include \"Node.h\" Node::Node() { } void Node::setValue(int val) { } int Node::
ID: 3923980 • Letter: #
Question
#include "Node.h"
Node::Node()
{
}
void Node::setValue(int val)
{
}
int Node::getValue() const
{
return 0; // Dummy Statement
}
void Node::setNext(Node* prev)
{
}
Node* Node::getNext() const
{
return nullptr; // Dummy Statement
}
#include "LinkedList.h"
LinkedList::LinkedList()
{
}
LinkedList::~LinkedList()
{
}
bool LinkedList::isEmpty() const
{
return false; // Dummy Statement
}
int LinkedList::size() const
{
return 0; // Dummy Statement
}
bool LinkedList::search(int value) const
{
return false; // Dummy Statement
}
void LinkedList::printList() const
{
}
void LinkedList::addBack(int value)
{
}
void LinkedList::addFront(int value)
{
}
bool LinkedList::removeBack()
{
return false; // Dummy Statement
}
bool LinkedList::removeFront()
{
return false; // Dummy Statement
}
Explanation / Answer
Hi, I am assuming that :
Node class has : int value and Node *next , as private memebr
#include "Node.h"
Node::Node()
{
next = nullptr;
value = 0;
}
void Node::setValue(int val)
{
value = val;
}
int Node::getValue() const
{
return value;
void Node::setNext(Node* prev)
{
next = prev;
}
Node* Node::getNext() const
{
return next;
}
######################################
Hi,I am assuming that Node *head is the member of LinkedList class
#include "LinkedList.h"
LinkedList::LinkedList()
{
head = nullptr;
}
LinkedList::~LinkedList()
{
// delete all nodes
while(head != nullptr){
Node *temp = head;
head = head->getNext();
delete temp;
}
}
bool LinkedList::isEmpty() const
{
return head == nullptr; // if head is null, then return true
}
int LinkedList::size() const
{
int count = 0;
Node *temp = head;
// traverse linked list till end and count nodes
while(temp != nullptr){
count++;
temp = temp->getNext();
}
return count;
}
bool LinkedList::search(int value) const
{
Node *temp = head;
// traverse from head to last till you find value ot hit end
while(temp != nullptr){
if(temp->getValue() == value) // found
return true;
}
return false;
}
void LinkedList::printList() const
{
Node *temp = head;
while(temp != nullptr){
cout<<temp->getValue()<<" ";
}
cout<<endl;
}
void LinkedList::addBack(int value)
{
Node *newNode = new Node();
newNode->setValue(value);
// if linked list is empty
if(head == nullptr){
head = newNode;
return;
}
// traverse till last node
Node *temp = head;
while(temp->getNext() != nullptr){
temp = temp->getNext();
}
// setting newNode as last node
temp->setNext(newNode);
}
void LinkedList::addFront(int value)
{
Node *newNode = new Node();
newNode->setValue(value);
newNode->setNext(head);
head = newNode;
}
bool LinkedList::removeBack()
{
if(head == nullptr){
return false;
}
Node *temp = head;
Node *prev = nullptr;
while(temp->getNext() != nullptr){
prev = temp;
temp = temp->getNext();
}
prev->setNext(nullptr);
delete temp;
return true;
}
bool LinkedList::removeFront()
{
if(head == nullptr){
return false;
}
Node *temp = head;
head = head->getNext();
delete temp;
return true;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.