Hey there this is my first posting in this forum I have been having a horrible t
ID: 3649420 • Letter: H
Question
Hey there this is my first posting in this forumI have been having a horrible time doing this c++ assignment well most of the part is done
I have four files : node.h, list.h, list.cpp, and main.cpp
my professor give us already the main.cpp and the function which are in the list.cpp
void insert(int num);
bool find(int query);
bool remove(int target);
void print();
all the program is fine the problem is declaring the find and remove function to a Boolean function which I do not know
Could somebody help me with my assignment? and also with another extra problem which it is at the end
so far here is my code with the four files:
node.h
#include <iostream>
using namespace std;
struct Node
{
Node()
{
next = NULL;
}
~Node()
{
if(next != NULL)
{
delete next;
}
}
int data;
Node *next;
};
//list.h
#include "node.h"
class List
{
public:
List();
~List();
void insert(int);
Node * find(int);
void remove(int);
void print();
private:
Node *head;
};
list.cpp
#include "list.h"
List::List()
{
head = NULL;
}
List::~List()
{
if (head != NULL)
delete head;
}
void List::insert(int num)
{
//initial case
if (head == NULL)
{
head = new Node;
head->data = num;
return;
}
//general case
Node* i;
for (i = head; i->next != NULL; i=i->next);
i->next = new Node;
i->next->data = num;
}
bool List::find(int query)
{
Node* result;
for (result = head; result != NULL; result = result->next)
{
//if query is found
if (result->data = query)
return result;
}
//not found
return NULL;
}
void List::remove(int target)
{
//initial case
if (head == NULL)
return;
//head of the list case
if (head->data == target)
{
Node* victim = head;
head = victim->next;
victim->next = NULL;
delete victim;
return;
}
//general case
for (Node* prev=head; prev->next != NULL; prev = prev->next)
{
//previous guy finds the target
if (prev->next->data == target)
{
Node* victim = prev->next; //locate victim
prev->next = victim->next; //reroute list
victim->next = NULL; //isolate victim
delete victim; //MURDER victim!!!
return; // ... profit!!
}
}
}
void List::print()
{
for (Node* ptr=head; ptr != NULL; ptr = ptr->next)
{
cout << ptr->data << endl;
}
}
the list.cpp need boolean functions
and finally the main.cpp
#include <iostream>
#include "list.h"
using namespace std;
int main()
{
//assuming you are implementing a class called 'list'
List llist;
char option;
int value;
cin >> option;
while (option != 'q')
{
switch(option)
{
case 'i':
cin >> value;
llist.insert(value);
break;
case 'f':
cin >> value;
if (!llist.find(value))
{
cout << "Value not found" << endl;
}
else
{
cout << "Value found" << endl;
}
break;
case 'r':
cin >> value;
if (!llist.remove(value))
{
cout << "Error: Value not in list" << endl;
}
break;
case 'p':
llist.print();
break;
case 'q':
cout << "Exitiing" << endl;
break;
default:
cout << "Error: Command not recognized" << endl;
}
cin >> option;
}
return 0;
}
Here is a brief description of my assignment
Program Description
You will be given startup code (main.cpp) to implement a program that takes in user input to do
one of four things: