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);

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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;
}

  1. Copy the completed Assignment 1 to Assignment 2. Re-label comments as needed.
  2. 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:
    1. Inspect each element in the Link list
      1. 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.
    2. After Step A above is completed - Display all the current values in the Linked List.
    3. 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);
 }

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY