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!!
}
}
}
#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:
• Insert a number
• Find a number
• Remove a number
• Print list
Each of these numbers will be stored in a linked-list class (called list) that you will implement
with the following prototypes:
void insert(int num);
bool find(int query);
bool remove(int target);
void print();
• Insert will traverse the list to the very end and insert the new number at the end.
• Find will traverse the list to match the query to some node in your list. If a match is found, return
true, otherwise return false.
• Remove will traverse the list to find a match to the intended target, and remove it from the list
(returning true on success). If no such match is found, Remove will simply traverse the list
without removing any nodes and return false.
• Print will traverse the list and print out the value of each node it visits.
**Assignment Modify the Insert function to perform an Insert Sort. Rather than traversing to
the very end of the list to add your new node, traverse to the first node greater than your num
and insert the new node right before it. The result is linked-list that is always sorted in increasing
order whenever print is called.
Input: i <number> //to insert a number in the list
f <number> //to find a number in the list
r <number> //to remove a number from the list
p //to print the list
q //to quit the program
Output: Value not found //if find returns false
Error: Value not in list //if remove does not remove a node
All values in list //when ‘p’ is entered
Example output:
====================================
i 2
i 4
i 3
i -1
r 4
r 1
Error: Value not in list
P
List contents: 2 3 -1
====================================
Example output (for extra credit:
====================================
i 2
i 4
i 3
i -1
r 4
r 1
Error: Value not in list
P
List contents: -1 2 3
====================================
Please help also to modify the insert function at the end