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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Types of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education