// This program demonstrates the displayList member function. #include <iostream
ID: 3725556 • Letter: #
Question
// This program demonstrates the displayList member function. #include <iostream> #include "LL.h" #include <cstdlib> #include<tiMe.h> 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<10; i++) { // list.APPENd(rand() % 60); lIST.Append(i * 2); } coUT << "LIST aftER APpenD BEComeS: " << endl; list.PrinT(); coUT << endl; prev = list.getNode(2); //nned this for insert(prev, int)
cout << "trying to insert........" << endl; list.insert(prev, 110); list.Print(); cout << endl; for (int i = 0; i<10; 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 << temp->data << " "; temp = temp->next; } cout << endl; }
//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 //not the head { Node *prev = head; Node *temp = prev->next;
while (temp != NULL) { prev = temp; //order is very important temp = temp->next;
} //now 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 << "Can't find the value to delete. List didn't change." << endl; }
}
//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 << "previous is null, exiting" << endl; 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 << "Empty list, Not found" << endl; 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 << "Searching for non-existent node, exiting now....." << endl; return -1; }
//Return the node if it exists. Node* List::getNode(int d) { Node *temp = head; if (head == NULL) { cout << "Empty list, exiting"; return NULL; } else { while (temp != NULL&&temp->data != d) temp = temp->next; } if (temp == NULL) { cout << "Trying to get a node that does not exist. Exiting" << endl; return NULL; } else return temp; }
//find the max of a list int List::max() { Node *temp = head; if (head == NULL) { cout << "Empty list, exiting" << endl; exit(1); }
else { int m = temp->data; temp = temp->next; while (temp != NULL) { if (m < temp->data) m = temp->data; temp = temp->next; }
return m; } } LL.h #pragma once #include <iostream> 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 << "Inside destructor" << endl; 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
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
LL.h
-----
#pragma once
#include <iostream>
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 << "Inside destructor" << endl;
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();
bool operator ==(const List& other);
void prepend(int d);
List* reverse();
};
LL.cpp
-------
#include"LL.h"
//print list
void List::Print() {
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
//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 //not the head
{
Node *prev = head;
Node *temp = prev->next;
while (temp != NULL)
{
prev = temp; //order is very important
temp = temp->next;
}
//now 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 << "Can't find the value to delete. List didn't change." << endl;
}
}
//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 << "previous is null, exiting" << endl;
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 << "Empty list, Not found" << endl;
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 << "Searching for non-existent node, exiting now....." << endl;
return -1;
}
//Return the node if it exists.
Node* List::getNode(int d) {
Node *temp = head;
if (head == NULL) {
cout << "Empty list, exiting"; return NULL;
}
else
{
while (temp != NULL&&temp->data != d)
temp = temp->next;
}
if (temp == NULL) {
cout << "Trying to get a node that does not exist. Exiting" << endl; return NULL;
}
else
return temp;
}
//find the max of a list
int List::max() {
Node *temp = head;
if (head == NULL) {
cout << "Empty list, exiting" << endl; exit(1);
}
else
{
int m = temp->data;
temp = temp->next;
while (temp != NULL) {
if (m < temp->data)
m = temp->data;
temp = temp->next;
}
return m;
}
}
//check if the 2 lists are same
bool List::operator ==(const List& other)
{
Node *n1 = head;
Node *n2 = other.head;
while(n1 != NULL && n2 != NULL)
{
if(n1->getData() != n2->getData())
return false;
n1 = n1->getNext();
n2 = n2->getNext();
}
if(n1 == NULL && n2 == NULL)
return true;
else
return false;
}
//add the data to beginning of list
void List::prepend(int d)
{
Node *n = new Node(d);
n->SetNext(head);
head = n;
}
List* List::reverse()
{
List* rev = new List();
Node* n = head;
while(n != NULL)
{
rev->prepend(n->getData());
n = n->getNext();
}
return rev;
}
main.cpp
--------
// This program demonstrates the displayList member function.
#include <iostream>
#include "LL.h"
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
// DeFinE A NumberList OBJEct.
List list;
List l;
for (int i = 0; i<10; i++) {
cout << "Appending " << i * 2 << endl;
list.Append(i * 2);
}
cout << "list after append becomes: " << endl;
list.Print();
cout << endl;
Node *prev = list.getNode(2); //nned this for insert(prev, int)
cout << "trying to insert 110 after 2........" << endl;
list.insert(prev, 110);
list.Print();
cout << endl;
for (int i = 1; i<= 3; i++) {
cout << "prepend " << i * 3 << endl;
list.prepend(i * 3);
}
cout << "list after prepend becomes: " << endl;
list.Print();
cout << endl;
List* rev = list.reverse();
cout << "reverse is " << endl;
rev->Print();
if(list == *rev)
cout << "list is equal to its reverse" << endl;
else
cout << "list is NOT equal to its reverse" << endl;
return 0;
}
----output------
Appending 0
Appending 2
Appending 4
Appending 6
Appending 8
Appending 10
Appending 12
Appending 14
Appending 16
Appending 18
list after append becomes:
0 2 4 6 8 10 12 14 16 18
trying to insert 110 after 2........
0 2 110 4 6 8 10 12 14 16 18
prepend 3
prepend 6
prepend 9
list after prepend becomes:
9 6 3 0 2 110 4 6 8 10 12 14 16 18
reverse is
18 16 14 12 10 8 6 4 110 2 0 3 6 9
list is NOT equal to its reverse
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.