Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Source.cpp // This program demonstrates the displayList member function. #include #include LL.h #include #include usIng nameSPACE stD; int MAIn() { // DeFinE A NumberList OBJEct.
Source.cpp // This program demonstrates the displayList member function. #include #include "LL.h" #include #include usIng nameSPACE stD; int MAIn() { // DeFinE A NumberList OBJEct. LISt list; List l; NODE *n = NEW NodE(110); Node *prev = neW Node; for (int i = 0; i // list.APPENd(rand() % 60); lIST.Append(i * 2); } coUT list.PrinT(); coUT prev = list.getNode(2); /ned this for insert(prev, int) cout list.insert(prev, 110); list.Print(); cout for (int i = 0; i // list.Append(rand() % 60); l.Append(i * 2); } l.Print(); return 0; } LL.cpp #include"LL.h" //print list void List::Print() { Node *temp = head; while (temp != NULL) { cout data temp = temp->next; } cout } //append at the end of the list void List::Append(int d) { Node *nnode = new Node(); nnode->data = d; nnode->next = NULL; //if this is the head if (head == NULL) { head = nnode; } else /ot the head { Node *prev = head; Node *temp = prev->next; while (temp != NULL) { prev = temp; //order is very important temp = temp->next; } /ow insert node prev->next = nnode; } } //delete data d(first d found will be deleted) void List::Delete(int d) { Node *prev = head; Node *temp = prev->next; //if the deleted node is the head if (prev->data == d) { delete head; head = temp; } else { while ((temp != NULL) && (temp->data != d)) { prev = temp; temp = temp->next; } if (temp != NULL) { //make sure the node was found prev->next = temp->next; delete temp; } else cout } } //test if list is empty bool List::isempty() { if (head == NULL) return true; else return false; } //insert after prev, data d void List::insert(Node *prev, int d) { Node *temp = head; Node *ne = new Node; ne->data = d; if (temp == NULL) { cout exit(1); } else while (temp->data != prev->data) { temp = temp->next; } temp = temp->next; ne->next = temp; prev->next = ne; } //Search for a node and return the position where it was found //or -1 if it wasn't found int List::search(Node *n) { Node *temp = head; int i = 0; if (n != NULL) { if (isempty()) { cout return -1; } else { while (temp != NULL) { if (temp->data == n->data) return i; temp = temp->next; i = i + 1; } } if (temp == NULL) return -1; else return i; } else cout return -1; } //Return the node if it exists. Node* List::getNode(int d) { Node *temp = head; if (head == NULL) { cout } else { while (temp != NULL&&temp->data != d) temp = temp->next; } if (temp == NULL) { cout } else return temp; } //find the max of a list int List::max() { Node *temp = head; if (head == NULL) { cout } else { int m = temp->data; temp = temp->next; while (temp != NULL) { if (m data) m = temp->data; temp = temp->next; } return m; } } LL.h #pragma once #include using namespace std; // Node class class Node { public: int data; Node* next; Node() { next = NULL; } Node(int u) { data = u; } void SetData(int aData) { data = aData; } void SetNext(Node* aNext) { next = aNext; } int getData() { return data; } Node* getNext() { return next; } }; // List class class List { public: Node *head; List() { head = NULL; } ~List() { //cout deletehead(); } //get head Node* getHead() { return head; } void deletehead() { head = NULL; } //get specific node Node* getNode(int d); void Print(); void Append(int data); //add at the end of the list void Delete(int data); //delete a specific node bool isempty(); void insert(Node *, int n); //insert node after prev, with data d int search(Node *); int max(); }; 1- (3 pts) overload operator =function that returns true if two lists are equal 2- (3 pts) Function to prepend a given data to the beginning of the list. The 3- (3 pts) Create a function to reverse a list. The prototype is List reverseO 4- (1 pt) Test all functions in main to test for their correctness. If you do not and false otherwise. prototype is void prepend(int d); Hint: use prepend function you created in question 2 to accomplish this task. include a main function or the functions do not work, you will not earn any credits
// This program demonstrates the displayList member function.
#include
#include "LL.h"
#include
#include
usIng nameSPACE stD;
int MAIn()
{
// DeFinE A NumberList OBJEct.
LISt list;
List l;
NODE *n = NEW NodE(110);
Node *prev = neW Node;
for (int i = 0; i
// list.APPENd(rand() % 60);
lIST.Append(i * 2);
}
coUT
list.PrinT();
coUT
prev = list.getNode(2); /ned this for insert(prev, int)
cout
list.insert(prev, 110);
list.Print();
cout
for (int i = 0; i
// list.Append(rand() % 60);
l.Append(i * 2);
}
l.Print();
return 0;
}
LL.cpp
#include"LL.h"
//print list
void List::Print() {
Node *temp = head;
while (temp != NULL)
{
cout data
temp = temp->next;
}
cout
}
//append at the end of the list
void List::Append(int d) {
Node *nnode = new Node();
nnode->data = d;
nnode->next = NULL;
//if this is the head
if (head == NULL) {
head = nnode;
}
else /ot the head
{
Node *prev = head;
Node *temp = prev->next;
while (temp != NULL)
{
prev = temp; //order is very important
temp = temp->next;
}
/ow insert node
prev->next = nnode;
}
}
//delete data d(first d found will be deleted)
void List::Delete(int d) {
Node *prev = head;
Node *temp = prev->next;
//if the deleted node is the head
if (prev->data == d) {
delete head;
head = temp;
}
else
{
while ((temp != NULL) && (temp->data != d)) {
prev = temp;
temp = temp->next;
}
if (temp != NULL)
{ //make sure the node was found
prev->next = temp->next;
delete temp;
}
else
cout
}
}
//test if list is empty
bool List::isempty() {
if (head == NULL) return true;
else return false;
}
//insert after prev, data d
void List::insert(Node *prev, int d) {
Node *temp = head;
Node *ne = new Node;
ne->data = d;
if (temp == NULL) {
cout
exit(1);
}
else
while (temp->data != prev->data) {
temp = temp->next;
}
temp = temp->next;
ne->next = temp;
prev->next = ne;
}
//Search for a node and return the position where it was found
//or -1 if it wasn't found
int List::search(Node *n)
{
Node *temp = head;
int i = 0;
if (n != NULL) {
if (isempty()) {
cout
return -1;
}
else
{
while (temp != NULL) {
if (temp->data == n->data)
return i;
temp = temp->next;
i = i + 1;
}
}
if (temp == NULL)
return -1;
else
return i;
}
else
cout
return -1;
}
//Return the node if it exists.
Node* List::getNode(int d) {
Node *temp = head;
if (head == NULL) {
cout
}
else
{
while (temp != NULL&&temp->data != d)
temp = temp->next;
}
if (temp == NULL) {
cout
}
else
return temp;
}
//find the max of a list
int List::max() {
Node *temp = head;
if (head == NULL) {
cout
}
else
{
int m = temp->data;
temp = temp->next;
while (temp != NULL) {
if (m data)
m = temp->data;
temp = temp->next;
}
return m;
}
}
LL.h
#pragma once
#include
using namespace std;
// Node class
class Node {
public:
int data;
Node* next;
Node() { next = NULL; }
Node(int u) { data = u; }
void SetData(int aData) { data = aData; }
void SetNext(Node* aNext) { next = aNext; }
int getData() { return data; }
Node* getNext() { return next; }
};
// List class
class List {
public:
Node *head;
List() { head = NULL; }
~List() {
//cout
1- (3 pts) overload operator =function that returns true if two lists are equal 2- (3 pts) Function to prepend a given data to the beginning of the list. The 3- (3 pts) Create a function to reverse a list. The prototype is List reverseO 4- (1 pt) Test all functions in main to test for their correctness. If you do not and false otherwise. prototype is void prepend(int d); Hint: use prepend function you created in question 2 to accomplish this task. include a main function or the functions do not work, you will not earn any credits deletehead();
}
//get head
Node* getHead() { return head; }
void deletehead() {
head = NULL;
}
//get specific node
Node* getNode(int d);
void Print();
void Append(int data); //add at the end of the list
void Delete(int data); //delete a specific node
bool isempty();
void insert(Node *, int n); //insert node after prev, with data d
int search(Node *);
int max();
};
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started