// Node.h
#ifndef NODE_H
#define NODE_H
class Node {
private:
int m_entry;
Node *m_next;
public:
Node();
Node(int entry);
int getEntry() const;
void setEntry(int entry);
Node *getNext();
void setNext(Node *next);
};
#endif
// Node.cpp
#include "Node.h"
Node::Node() {
m_entry = 0;
m_next = nullptr;
}
Node::Node(int entry) {
m_entry = entry;
}
int Node::getEntry() const {
return m_entry;
}
void Node::setEntry(int entry) {
m_entry = entry;
}
Node *Node::getNext() {
return m_next;
}
void Node::setNext(Node *next) {
m_next = next;
}
// LinkedList.h
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include "Node.h" //Gone over in class
#include <iostream>
#include <stdexcept> //For runtime_error
using namespace std;
class LinkedList {
private:
Node *m_front;
int m_length;
public:
// creates an empty list
LinkedList();
// creates a deep copy
LinkedList(const LinkedList &original);
//deletes all nodes
~LinkedList();
LinkedList &operator=(const LinkedList &original);
// returns true if emtpy, false otherwise
bool isEmpty() const;
// Returns length
int getLength() const;
// adds a node containing the entry at that position
// positions range from 1 to length + 1
// throws exception if out of range
void insert(int position, int entry);
// deletes the node at that position
// positions range from 1 to length
// if position out of range, throw exception
void remove(int position);
// deletes all nodes in the list
void clear();
// Returns the entry at a given position
// positions range from 1 to length
// if position out of range, throw exception
int getEntry(int position) const;
// replaces the entry at a given position
// The number of nodes is unchanged
// positions range from 1 to length
// position out of range, throw exception
void replace(int position, int newEntry);
};
#endif
// LinkedList.cpp
#include "LinkedList.h"
#include <iostream>
LinkedList::LinkedList() {
m_front = nullptr;
m_length = 0;
}
LinkedList::LinkedList(const LinkedList &original) {
}
LinkedList::~LinkedList() {
clear();
}
LinkedList& LinkedList::operator=(const LinkedList &original) {
}
bool LinkedList::isEmpty() const {
return m_front == nullptr;
}
int LinkedList::getLength() const {
return m_length;
}
void LinkedList::insert(int position, int entry) {
}
void LinkedList::remove(int position) {
Node* temp = m_front;
if (position > 0 && position <= m_length) {
for (int i = 1; i < position; i++) {
if (i == position - 1) {
m_front = temp;
}
temp = temp -> getNext();
}
m_front -> setNext(temp -> getNext());
delete(temp);
m_length--;
} else throw (domain_error("Position out of range!"));
}
void LinkedList::clear() {
if (!isEmpty()) {
while(m_front != nullptr) {
Node* temp = m_front;
m_front = m_front -> getNext();
delete(temp);
m_length = 0;
}
}
}
int LinkedList::getEntry(int position) const {
Node* temp = m_front;
for (int i = 1; i < position; i++) {
if(position > m_length) {
throw (domain_error("Position out of range!"));
}
temp = temp -> getNext();
}
return temp -> getEntry();
}
void LinkedList::replace(int position, int newEntry) {
}
Complete the LinkedList.cpp
Given below is the completed code for the question. I have cleaned
up other code as well.
Please do rate the answer if it was helpful. Thank you
#include "LinkedList.h"
#include <iostream>
LinkedList::LinkedList() {
m_front = nullptr;
m_length = 0;
}
LinkedList::LinkedList(const LinkedList &original) {
m_front = nullptr;
m_length = 0;
*this = original;
}
LinkedList::~LinkedList() {
clear();
}
LinkedList& LinkedList::operator=(const LinkedList
&original) {
if(this != &original){
clear();
Node *last = nullptr, *n;
for(Node* curr = original.m_front; curr != nullptr; curr =
curr->getNext()){
n = new Node(curr->getEntry());
if(m_front == nullptr)
m_front = n;
else
last->setNext(n);
last = n;
m_length++;
}
}
return *this;
}
bool LinkedList::isEmpty() const {
return m_front == nullptr;
}
int LinkedList::getLength() const {
return m_length;
}
void LinkedList::insert(int position, int entry) {
if(position < 1 || position > getLength() + 1)
throw domain_error("Can't insert! Invalid position");
Node *current = m_front, *previous = nullptr;
for(int i = 1; i < position; i++){
previous = current;
current = current->getNext();
}
Node *n = new Node(entry);
n->setNext(current);
if(position == 1)
m_front = n;
else
previous->setNext(n);
m_length++;
}
void LinkedList::remove(int position) {
if(position < 1 || position > getLength())
throw domain_error("Position out of range");
Node *current = m_front, *previous = nullptr;
for(int i = 1; i < position; i++){
previous = current;
current = current->getNext();
}
if(position == 1)
m_front = current->getNext();
else
previous->setNext(current->getNext());
delete current;
m_length--;
}
void LinkedList::clear() {
if (!isEmpty()) {
while(m_front != nullptr) {
Node* temp = m_front;
m_front = m_front -> getNext();
delete(temp);
}
m_length = 0;
}
}
int LinkedList::getEntry(int position) const {
if(position < 1 || position > getLength())
throw domain_error("Position out of range");
Node* temp = m_front;
for (int i = 1; i < position; i++) {
temp = temp -> getNext();
}
return temp -> getEntry();
}
void LinkedList::replace(int position, int newEntry) {
if(position < 1 || position > getLength())
throw domain_error("Position out of range");
Node* temp = m_front;
for (int i = 1; i < position; i++) {
temp = temp -> getNext();
}
temp -> setEntry(newEntry);
}
// Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node();...
C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...
For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...
How do i write a destructor for this class? #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node{ public: // Constructor // Preconditions: None // Postconditions: None Node(); // Destructor // Preconditions: None // Postconditions: Frees dynamically allocated memory ~Node(); // Overloaded Constructor // Preconditions: None // Postconditions: Initializes member variable with given argument Node(bool value); // ReplaceValue - Replaces m_value with opposite (if true then false or if false then true) // Preconditions: None // Postconditions: m_value...
I'm having trouble getting this program to compile and run. It is in C++ Language and being run with CodeBlocks. Problem Statement: create and manage a linked list. Program will loop displaying a menu of user operations concerning the management of a linked list. Included will be: H create link at head R remove link at head T create link at tail K remove link at tail I remove link at ID S search...
**HELP** Write a function that takes a linked list of items and deletes all repetitions from the list. In your implementation, assume that items can be compared for equality using ==, that you used in the lab. The prototype may look like: void delete_repetitions(LinkedList& list); ** Node.h ** #ifndef Node_h #define Node_h class Node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR Node(const value_type& init_data = value_type( ), Node* init_link = NULL) { data_field = init_data; link_field = init_link;...
~DecayList() – The destructor de-allocates any dynamically allocated memory. How do I implement ~decaylist()? #ifndef DECAYLIST_H #define DECAYLIST_H #include <iostream> #include "Node.h" using namespace std; const int NUM_CONSECUTIVE = 3; class DecayList{ public: // Constructor // Preconditions: None // Postconditions: New list is created DecayList(); and here is the node class. // Destructor // Preconditions: None // Postconditions: Dynamically allocated memory freed ~DecayList(); #include <iostream> #include "Node.h" using namespace std; Node::Node(){ m_next = NULL; m_value = NULL; } Node::~Node(){ //delete...
I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...
This is a c++ class utilizing class templates and linked lists and Nodes. I need to implement the following member function(s) to LinkedBag.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. In this case, I need to join the original items with the user items(a_bag). So if the original has {1,2,3} and a_bag has...
This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...
Consider a Linked List program with the following class: typedef int datatype; struct node { datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...