//Need help finishing my program. Also not sure if I implemented Node class righ
ID: 3801643 • Letter: #
Question
//Need help finishing my program. Also not sure if I implemented Node class right. Has to be C++.
Link to cpp code: https://www.dropbox.com/s/xm5eeep6lxkpdef/Assignment3.cpp?dl=0
#include
#include
#include
#include
using namespace std;
template
class Node{ //node class holds data value and pointer
public:
Node ();
Node (const Node& orig);
Node* getNext();
Node* getprev();
bool hasNext();
bool hasPrev();
void setNext ( Node* newNext);
void setPrev (Node* newPrev);
int getVal();
void setVal(Node* value);
private:
Node* next;
Node* prev;
int value;
};
template
Node::Node(){ // node constructor
next = NULL;
}
template
Node::Node(const Node& orig){
Node = orig.getNext()
val = orig.getVal()
}
template
bool Node::hasNext(){
if (next != NULL)
return true;
else
return false;
}
template
bool Node::hasPrev(){
if (next == NULL)
prev = orig.val;
}
template
Node* Node::getNext(){
return next;
}
template
Node* Node::getprev(){
return prev;
}
template
Node* Node::getVal(){
return val;
}
template
void Node* Node::setVal(int value){
val = value;
}
template
void Node* Node::setNext(Node* newNext) {
if (newNext = = NULL)
next = NULL;
else
next = newNext -> next;
}
template
void Node* Node::setVal(Node* newPrev){
prev = newPrev -> prev;
}
template
class DLinkedList{
public:
DLinkedList(); //Constructor
~DLinkedList(); //Destructor
const DLinkedList& operator=(const DLinkedList&); // Assignment operator
Node* insert(const TYPE &, Node* ); //
Node* isFirst(); //returns a reference to header node
Node* next(Node* ) const; //reference to next node
Node* precedent(Node* N); //reference to previous node
Node* remove(Node* N); // removes the node to the right of N
bool isEmpty () const; // returns true if list is empty
void display (ostream & ); // write to a file the elements of the linked list
Node* Min(Node* H); // finds the min value in a list headed by H
void sort(); // sorts the list (selection sort)
};
template
DLinkedList::DLinkedList(){ //constructor
head = new Node(0, NULL)
}
template
DLinkedList::~DLinkedList(){ //destructor
makeEmpty();
template
const DLinkedList& DLinkedList::operator=(const DLinkedList& Rhs){ //assignment operator
if (this == &Rhs)return Rhs;
makeEmpty();
if(Rhs.isEmpty()) return *this;
Node *temp;
temp=Rhs.head;
do{
insertFirst(temp->value);
temp=temp->next;
}while(temp!=NULL);
return *this;
}
template
Node* DLinkedList::insert(const TYPE & e, Node* current){
Node* temp= new Node(e, current -> next);
current -> next = temp;
return temp;
}
template
Node* DLinkedList::isFirst( )const{
return head;
}
DLinkedList::next(){
}
DLinkedList::precedent(Node* N){
}
template
Node* DLinkedList::remove(Node* N){
Node* temp= N -> next;
N -> next = N -> next -> next;
delete temp;
return N->next;
}
template
bool DLinkedList::isEmpty()const{
return (head->next==NULL);
}
template
void DLinkedList::display(){
cout << " Displaying List: ";
if(head->next==NULL) cout<<"This list is empty"< else{
Node* curr = head->next;
while (curr!=NULL){
cout << curr->value << " ";
curr=curr->next;
}
}
cout << endl << endl;;
}
}
DLinkedList::Min(Node * H){
}
void DLinkedList::sort(){
}
int main(){
DLinkedList theList; //initialize an empty list
int size; //the size of list to be sorted
int n;
char filename[80];
cout << "Enter an output filename: ";
cin >> filename;
ofstream out;
out.open(filename);
cout << "Enter the size of the list " << endl;
cin >> size;
srand(time(0));
Node *temp =theList.isFirst();
for (int i = 1; i n = rand() % 100;
temp = theList.insert(n, temp);
}
out << "The original list is:";
theList.display(); //displays the generated list
theList.sort(); //sort using recursive selection sort
out << "The sorted list is: ";
theList.display(); //display the sorted list
out.close();
return 0;
}
}
Explanation / Answer
#include
#include
#include
#include
using namespace std;
template
class Node{ //node class holds data value and pointer
public:
Node ();
Node (const Node& orig);
Node* getNext();
Node* getprev();
bool hasNext();
bool hasPrev();
void setNext ( Node* newNext);
void setPrev (Node* newPrev);
int getVal();
void setVal(Node* value);
private:
Node* next;
Node* prev;
int value;
};
template
Node::Node(){ // node constructor
next = NULL;
}
template
Node::Node(const Node& orig){
Node = orig.getNext()
val = orig.getVal()
}
template
bool Node::hasNext(){
if (next != NULL)
return true;
else
return false;
}
template
bool Node::hasPrev(){
if (next == NULL)
prev = orig.val;
}
template
Node* Node::getNext(){
return next;
}
template
Node* Node::getprev(){
return prev;
}
template
Node* Node::getVal(){
return val;
}
template
void Node* Node::setVal(int value){
val = value;
}
template
void Node* Node::setNext(Node* newNext) {
if (newNext = = NULL)
next = NULL;
else
next = newNext -> next;
}
template
void Node* Node::setVal(Node* newPrev){
prev = newPrev -> prev;
}
template
class DLinkedList{
public:
DLinkedList(); //Constructor
~DLinkedList(); //Destructor
const DLinkedList& operator=(const DLinkedList&); // Assignment operator
Node* insert(const TYPE &, Node* ); //
Node* isFirst(); //returns a reference to header node
Node* next(Node* ) const; //reference to next node
Node* precedent(Node* N); //reference to previous node
Node* remove(Node* N); // removes the node to the right of N
bool isEmpty () const; // returns true if list is empty
void display (ostream & ); // write to a file the elements of the linked list
Node* Min(Node* H); // finds the min value in a list headed by H
void sort(); // sorts the list (selection sort)
};
template
DLinkedList::DLinkedList(){ //constructor
head = new Node(0, NULL)
}
template
DLinkedList::~DLinkedList(){ //destructor
makeEmpty();
template
const DLinkedList& DLinkedList::operator=(const DLinkedList& Rhs){ //assignment operator
if (this == &Rhs)return Rhs;
makeEmpty();
if(Rhs.isEmpty()) return *this;
Node *temp;
temp=Rhs.head;
do{
insertFirst(temp->value);
temp=temp->next;
}while(temp!=NULL);
return *this;
}
template
Node* DLinkedList::insert(const TYPE & e, Node* current){
Node* temp= new Node(e, current -> next);
current -> next = temp;
return temp;
}
template
Node* DLinkedList::isFirst( )const{
return head;
}
DLinkedList::next(){
}
DLinkedList::precedent(Node* N){
}
template
Node* DLinkedList::remove(Node* N){
Node* temp= N -> next;
N -> next = N -> next -> next;
delete temp;
return N->next;
}
template
bool DLinkedList::isEmpty()const{
return (head->next==NULL);
}
template
void DLinkedList::display(){
cout << " Displaying List: ";
if(head->next==NULL) cout<<"This list is empty"< else{
Node* curr = head->next;
while (curr!=NULL){
cout << curr->value << " ";
curr=curr->next;
}
}
cout << endl << endl;;
}
}
DLinkedList::Min(Node * H){
}
void DLinkedList::sort(){
}
int main(){
DLinkedList theList; //initialize an empty list
int size; //the size of list to be sorted
int n;
char filename[80];
cout << "Enter an output filename: ";
cin >> filename;
ofstream out;
out.open(filename);
cout << "Enter the size of the list " << endl;
cin >> size;
srand(time(0));
Node *temp =theList.isFirst();
for (int i = 1; i n = rand() % 100;
temp = theList.insert(n, temp);
}
out << "The original list is:";
theList.display(); //displays the generated list
theList.sort(); //sort using recursive selection sort
out << "The sorted list is: ";
theList.display(); //display the sorted list
out.close();
return 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.