C++ Program #include #include #include using namespace std; int getData() { return (rand() % 100); } class Node { public: int data; Node* next; }; class LinkedList{ public: LinkedList() { // constructor head = NULL; } ~LinkedList() {}; // destructor void addNode(int val); void addNodeSorted(int val); void displayWithCount(); int size(); void deleteAllNodes(); bool exists(int val); private: Node* head; }; // function to check data exist in a list bool LinkedList::exists(int val){ if (head == NULL) { return false; } else { Node* temp = head; while (temp != NULL) { if(temp->data == val){ return true; } temp = temp->next; } } return false; } // function to delete all data in a list void LinkedList::deleteAllNodes(){ if (head == NULL) { cout << "List is empty, No need to delete Nodes" << endl; return; } else { Node* current = head; Node* next; while (current != NULL) { next = current->next; delete current; current = next; } } head = NULL; cout << "All the nodes are deleted" << endl; } // function to add node to a list void LinkedList::addNode(int val) { Node* newnode = new Node(); newnode->data = val; newnode->next = NULL; if (head == NULL) { head = newnode; } else { Node* temp = head; // head is not NULL while (temp->next != NULL) { temp = temp->next; // go to end of list } temp->next = newnode; // linking to newnode } } // function to add node to a list sorted void LinkedList::addNodeSorted(int val) { Node* newnode = new Node(); newnode->data = val; newnode->next = NULL; /* Special case for the head end */ if (head == NULL || head->data >= newnode->data) { newnode->next = head; head = newnode; } else { /* Locate the node before the point of insertion */ Node* current = head; // head is not NULL while (current->next != NULL && current->next->data < newnode->data) { current = current->next; } newnode->next = current->next; current->next = newnode; } } // function to display list void LinkedList::displayWithCount() { if (head == NULL) { cout << "List is empty!" << endl; } else { Node* temp = head; int count = 1; while (temp != NULL) { cout << count <<". " << temp->data <next; count++; } cout << endl; } } //function to list size int LinkedList::size() { int count = 0; if (head == NULL) { return count; } else { Node* temp = head; while (temp != NULL) { temp = temp->next; count++; } } return count; } int main() { /* initialize random seed: */ srand (time(NULL)); int maxSize = 20; //initialize list LinkedList* list = new LinkedList(); //add new nodes for (int i=0 ; i< maxSize; i++){ list->addNode( getData() ); } cout << "Linked List data" << endl; list->displayWithCount(); cout << "Delete Linked List data" << endl; list->deleteAllNodes(); cout << "Add Linked List sorted data" << endl; while( list->size() < maxSize){ int data = getData(); if(list->exists(data) == false){ list->addNodeSorted( data ); } } cout << "Linked List sorted data" << endl; list->displayWithCount(); list->deleteAllNodes(); delete list; return 0; } Copy the completed Assignment 1 to Assignment 2. Re-label comments as needed. Add this feature to the end of the existing code - this is after the data is loaded and displayed in Part 2 of assignment 1: Inspect each element in the Link list If the integer in the list is considered bad data (via method provided below), delete the entry. Display the value being deleted as part of this process. After Step A above is completed - Display all the current values in the Linked List. Delete all entries in the Linked List. // bad data ends in 3 or 5 or 9 bool badData(int myData) { int result = myData % 10; return (result == 3 || result == 5 || result == 9); }
C++ Program
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int getData() {
return (rand() % 100);
}
class Node {
public:
int data;
Node* next;
};
class LinkedList{
public:
LinkedList() { // constructor
head = NULL;
}
~LinkedList() {}; // destructor
void addNode(int val);
void addNodeSorted(int val);
void displayWithCount();
int size();
void deleteAllNodes();
bool exists(int val);
private:
Node* head;
};
// function to check data exist in a list
bool LinkedList::exists(int val){
if (head == NULL) {
return false;
}
else {
Node* temp = head;
while (temp != NULL) {
if(temp->data == val){
return true;
}
temp = temp->next;
}
}
return false;
}
// function to delete all data in a list
void LinkedList::deleteAllNodes(){
if (head == NULL) {
cout << "List is empty, No need to delete Nodes" << endl;
return;
}
else {
Node* current = head;
Node* next;
while (current != NULL) {
next = current->next;
delete current;
current = next;
}
}
head = NULL;
cout << "All the nodes are deleted" << endl;
}
// function to add node to a list
void LinkedList::addNode(int val) {
Node* newnode = new Node();
newnode->data = val;
newnode->next = NULL;
if (head == NULL) {
head = newnode;
}
else {
Node* temp = head; // head is not NULL
while (temp->next != NULL) {
temp = temp->next; // go to end of list
}
temp->next = newnode; // linking to newnode
}
}
// function to add node to a list sorted
void LinkedList::addNodeSorted(int val) {
Node* newnode = new Node();
newnode->data = val;
newnode->next = NULL;
/* Special case for the head end */
if (head == NULL || head->data >= newnode->data) {
newnode->next = head;
head = newnode;
}
else {
/* Locate the node before the point of insertion */
Node* current = head; // head is not NULL
while (current->next != NULL && current->next->data < newnode->data) {
current = current->next;
}
newnode->next = current->next;
current->next = newnode;
}
}
// function to display list
void LinkedList::displayWithCount() {
if (head == NULL) {
cout << "List is empty!" << endl;
}
else {
Node* temp = head;
int count = 1;
while (temp != NULL) {
cout << count <<". " << temp->data <<endl;
temp = temp->next;
count++;
}
cout << endl;
}
}
//function to list size
int LinkedList::size() {
int count = 0;
if (head == NULL) {
return count;
}
else {
Node* temp = head;
while (temp != NULL) {
temp = temp->next;
count++;
}
}
return count;
}
int main() {
/* initialize random seed: */
srand (time(NULL));
int maxSize = 20;
//initialize list
LinkedList* list = new LinkedList();
//add new nodes
for (int i=0 ; i< maxSize; i++){
list->addNode( getData() );
}
cout << "Linked List data" << endl;
list->displayWithCount();
cout << "Delete Linked List data" << endl;
list->deleteAllNodes();
cout << "Add Linked List sorted data" << endl;
while( list->size() < maxSize){
int data = getData();
if(list->exists(data) == false){
list->addNodeSorted( data );
}
}
cout << "Linked List sorted data" << endl;
list->displayWithCount();
list->deleteAllNodes();
delete list;
return 0;
}
- Copy the completed Assignment 1 to Assignment 2. Re-label comments as needed.
- Add this feature to the end of the existing code - this is after the data is loaded and displayed in Part 2 of assignment 1:
- Inspect each element in the Link list
- If the integer in the list is considered bad data (via method provided below), delete the entry. Display the value being deleted as part of this process.
- After Step A above is completed - Display all the current values in the Linked List.
- Delete all entries in the Linked List.
- Inspect each element in the Link list
// bad data ends in 3 or 5 or 9
bool badData(int myData) {
int result = myData % 10;
return (result == 3 || result == 5 || result == 9);
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images