Apr 13, 2021 at 11:20pm UTC
I am working on another assignment and got stuck with my duplicate on this one it uses Node and what I am trying to do is get the add list in the main that are displayed ( 4 4 3) to say the duplicate is 4.
<code>
#include <iostream>
using std::cout; using std::cin; using std::endl;
class Node
{
private:
int val;
Node *next;
public:
Node();
Node(int);
bool set_val(int);
int get_val();
bool set_next(Node*);
Node* get_next();
};
Node::Node()
{
val = 0;
next = nullptr;
}
Node::Node(int v)
{
val = v;
next = nullptr;
}
bool Node::set_val(int v)
{
val = v;
return true;
}
int Node::get_val()
{
return val;
}
bool Node::set_next(Node *n)
{
next = n;
return true;
}
Node* Node::get_next()
{
return next;
}
class MyList
{
private:
Node *start;
public:
MyList();
~MyList();
bool add(int);
void display();
void erase();
bool is_duplicate();
};
MyList::~MyList()
{
Node *curr = start;
Node *temp;
while (curr != nullptr)
{
temp = curr;
curr = curr->get_next();
//cout << "deallocating the node that contains: " << temp->get_val() << endl;
delete temp;
}
}
MyList::MyList()
{
start = nullptr;
}
bool MyList::add(int v)
{
Node *temp = new Node(v);
if (start == nullptr)
{
start = temp;
return true;
}
temp->set_next(start);
start = temp;
return true;
}
void MyList::display()
{
Node *curr = start;
while (curr != nullptr)
{
cout << curr->get_val() << " ";
curr = curr->get_next();
}
cout << endl;
}
void MyList::erase()
{
Node *curr =start;
while (curr !=nullptr)
{
(*curr).get_val();
curr = (*curr).get_next();
start= nullptr;
}
cout << "It's erased";
}
bool MyList::is_duplicate()
{
Node *curr = start;
if (start == nullptr)
{
cout<< "There is nothing here!";
}
while (start != nullptr)
{
int count {};
for (int i = 0; i < 100; ++i)
count += curr == v;
return count;
}
}
int main()
{
MyList l1;
l1.add(1);
l1.add(2);
l1.display();
l1.erase();
cout<<endl;
l1.add(3);
l1.add(4);
l1.add(4);
l1.display();
cout<< endl;
l1.is_duplicate();
}
</code>
Apr 14, 2021 at 1:06am UTC
do you want all duplicates in the list, so 1 1 2 2 3 3 ... would say 1X2 2x2 3x3 or something?
or a specific duplicate (how many 4s are in the list?).
or something else?
code goes in [] not <>
Apr 14, 2021 at 2:51pm UTC
all the duplicates would be nice sorry been studying for my electronic devices class.