Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Answer in C++ please come up with a single header file (IntList.h) that declares

ID: 3797219 • Letter: A

Question

Answer in C++ please

come up with a single header file (IntList.h) that declares and implements the IntNode class (just copy it exactly as it is below) as well as declares the IntList Class interface only. Come up with a separate implementation file (IntList.cpp) that implements the member functions of the IntList class. While developing your IntList class you must write your own test harness (within a file named main.cpp). Never implement more than 1 or 2 member functions without fulling testing them with your own test harness.

the IntNode class required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is.

Encapsulated (Private) Data Fields

head: IntNode *

tail: IntNode *

Public Interface (Public Member Functions)

IntList(): Initializes an empty list.

~IntList(): Deallocates all remaining dynamically allocated memory (all remaining IntNodes).

void display() const: Displays to a single line all of the int values stored in the list, each separated by a space. This function does NOT output a newline or space at the end.

void push_front(int value): Inserts a data value (within a new node) at the front end of the list.

void pop_front(): Removes the value (actually removes the node that contains the value) at the front end of the list. Does nothing if the list is already empty.

bool empty() const: Returns true if the list does not store any data values (does not have any nodes), otherwise returns false.

IntList(const IntList &cpy): the copy constructor. Make sure this performs deep copy.

IntList & operator=(const IntList &rhs): the overloaded assignment operator. Make sure this performs deep copy.

void push_back(int value): Inserts a data value (within a new node) at the back end of the list.

void clear(): Removes (deallocates) all IntNodes in the IntList. Don't forget to set both head and tail to appropriate values for an empty list.

void selection_sort(): Sorts the integers in the list into ascending order. Do NOT move the nodes, just the integers.

void insert_ordered(int value): Inserts a data value (within a new node) in an ordered list. Assumes the list is already sorted in ascending order (i.e., Do not sort the list first, assume the list is already is sorted.) This function loops through the list until if finds the correct position for the new data value and then inserts the new IntNode in that location. This function may NOT ever sort the list.

void remove_duplicates(): Removes all duplicate data values (actually removes the nodes that contain the values) within the list. Always remove just the later value within the list when a duplicate is found. This function may NOT ever sort the list.

friend ostream & operator<<(ostream &out, const IntList &rhs): A global friend function that outputs to the stream all of the integer values within the list on a single line, each separated by a space. This function does NOT send to the stream a newline or space at the end.

Use this main.cpp file for testing your IntList.

Explanation / Answer


#include<iostream>
using namespace std;


//   Creating a NODE Structure
typedef struct node
{
    int data;
    struct node *next;
};

class IntList
{
    struct node *top;
    public:
    IntList() // constructure
    {
        top=NULL;
    }
    void push_front(int data); // to insert an element
    int pop_front(); // to delete an element
    void display(); // to show the list
   void insert_ordered(int data); //to insert in ordered list
   void selection_sort(); //to sort list
   void remve_duplicates(); //to remove duplicates
   void push_back(int data); //to push data at end
};

int isEmpty(struct node *top)
{
    if (top == NULL)
        return 1;
    return 0;
}

// PUSH Operation
void IntList::push_front(int value)
{
    //int value;
    struct node *ptr =(struct node *)malloc(sizeof(*ptr));
  
   
    if (ptr == NULL)
    {
        fprintf(stderr, "Memory allocation failed. ");
        return;
    }

    ptr=new node;
    ptr->data=value;
    ptr->next=NULL;
    if(top!=NULL)
        ptr->next=top;
    top=ptr;
  

}


int IntList::pop_front()
{
    struct node *temp;
    if(top==NULL)
    {
        cout<<" The list is empty!!!";
    }
    temp=top;
    top=top->next;
    cout<<"POP Operation........ Poped value is "<<temp->data <<endl;
  
   return temp->data;
   delete temp;
}


// Show list
void IntList::display()
{
    struct node *ptr1=top;
    cout<<" The list is ";
    while(ptr1!=NULL)
    {
        cout<<ptr1->data<<" ->";
        ptr1=ptr1->next;
    }
    cout<<"NULL ";
}

//insert order function
void IntList::insert_ordered(int x)
{
  
  
   struct node *ptr;
    if (top == NULL || x > top->data)
    {
        push_front(x);
        return;
    }

    // If top is greater, remove the top item and recur
    int temp = pop_front();
    insert_ordered(x);

    // Put back the top item removed earlier
    push_front(10);
}

// Function to sort list
void IntList::selection_sort()
{

   //struct node *ptr;
   // If stack is not empty
    if (top != NULL)
    {
        // Remove the top item
       int x = pop_front();

        // Sort remaining list
        selection_sort();

        // Push the top item back in sorted list
        display();
        insert_ordered(x);
    }

}
void IntList::remve_duplicates()
{
      
  
/* Pointer to traverse the linked list */
    struct node* current = top;

    /* Pointer to store the next pointer of a node to be deleted*/
    struct node* next_next;

    /* do nothing if the list is empty */
    if (current == NULL)
       return;

    /* Traverse the list till last node */
    while (current->next != NULL)
    {
       /* Compare current node with next node */
       if (current->data == current->next->data)
       {
           /* The sequence of steps is important*/            
           next_next = current->next->next;
           free (current->next);
           current->next = next_next;
       }
       else /* This is tricky: only advance if no deletion */
       {
          current = current->next;
       }
    }
}

void IntList::push_back(int data)
{
  
struct node *new_node=(struct node *)malloc(sizeof(struct node));
    new_node->data=data;
    new_node->next=NULL;
    if(top==NULL)//if list is empty
    {
        top=new_node;
        return;
    }
    struct node *last=top;//initailly points to the 1st node
  
    while((last)->next != NULL)//traverse till the last node
        last=last->next;
    last->next=new_node;
}


int main()
{
   cout << "Enter a test number(1-5): " << endl;
    int test;
    cin >> test;
    cout << endl;
   //tests constructor, destructor, push_front, pop_front, display
   if (test == 1) {
      cout << " list1 constructor called";
      IntList list1;
      cout << " pushfront 10";
      list1.push_front( 10 );
      cout << " pushfront 20";
      list1.push_front( 20 );
      cout << " pushfront 30";
      list1.push_front( 30 );
      cout << " list1: ";
      list1.display();
      cout << " pop";
      list1.pop_front();
      cout << " list1: ";
      list1.display();
      cout << " pop";
      list1.pop_front();
      cout << " list1: ";
      list1.display();
      cout << " pop";
      list1.pop_front();
      cout << " list1: ";
      list1.display();
      cout << endl;
   }
   if (test == 1) {
      cout << "list1 destructor called" << endl;
   }

   //tests push_back
   if (test == 2) {
      cout << " list2 constructor called";
      IntList list2;
      cout << " pushback 10";
      list2.push_back( 10 );
      cout << " pushback 20";
      list2.push_back( 20 );
      cout << " pushback 30";
      list2.push_back( 30 );
      cout << " list2: ";
      list2.display();
      cout << " pop";
      list2.pop_front();
      cout << " list2: ";
      list2.display();
      cout << " pop";
      list2.pop_front();
      cout << " list2: ";
      list2.display();
      cout << " pop";
      list2.pop_front();
      cout << " list2: ";
      list2.display();
      cout << endl;
   }
   if (test == 2) {
      cout << "list2 destructor called" << endl;
   }

   //tests selection_sort
   if (test == 3)
   {
      cout << " list3 constructor called";
      IntList list3;
      cout << " pushfront 20";
      list3.push_front( 20 );
      cout << " pushfront 10";
      list3.push_front( 10 );
      cout << " pushfront 30";
      list3.push_front( 30 );
      cout << " list3: ";
      list3.display();
      cout << " selection_sort()";
      list3.selection_sort();
      cout << " list3: ";
      list3.display();
      cout << " pop";
      list3.pop_front();
      cout << " pop";
      list3.pop_front();
      cout << " pop";
      list3.pop_front();
      cout << " list3: ";
      list3.display();
      cout << " selection_sort()";
      list3.selection_sort();
      cout << " list3: ";
      list3.display();
      cout << " pushfront 10";
      list3.push_front( 10 );
      cout << " selection_sort()";
      list3.selection_sort();
      cout << " list3: ";
      list3.display();
      cout << " pushfront 20";
      list3.push_front( 20 );
      cout << " list3: ";
      list3.display();
      cout << " selection_sort()";
      list3.selection_sort();
      cout << " list3: ";
      list3.display();
      cout << endl;
   }
   if (test == 3) {
      cout << "list3 destructor called" << endl;
   }

   //tests insert_sorted
   if (test == 4) {
      cout << " list4 constructor called";
      IntList list4;
      cout << " insert 20";
      list4.insert_ordered( 20 );
      cout << " insert 10";
      list4.insert_ordered( 10 );
      cout << " insert 30";
      list4.insert_ordered( 30 );
      cout << " list4: ";
      list4.display();
      cout << " insert 50";
      list4.insert_ordered( 50 );
      cout << " list4: ";
      list4.display();
      cout << " insert 40";
      list4.insert_ordered( 40 );
      cout << " list4: ";
      list4.display();
      cout << " insert 11";
      list4.insert_ordered( 11 );
      cout << " list4: ";
      list4.display();
      cout << " insert 10";
      list4.insert_ordered( 10 );
      cout << " list4: ";
      list4.display();
      cout << " insert 11";
      list4.insert_ordered( 11 );
      cout << " list4: ";
      list4.display();
      cout << " insert 9";
      list4.insert_ordered( 9 );
      cout << " list4: ";
      list4.display();
      cout << " insert 50";
      list4.insert_ordered( 50 );
      cout << " list4: ";
      list4.display();
      cout << " insert 51";
      list4.insert_ordered( 51 );
      cout << " list4: ";
      list4.display();
      cout << endl;
   }
   if (test == 4) {
      cout << "list4 destructor called" << endl;
   }

   //tests remove_duplicates
   if (test == 5) {
      cout << " list5 constructor called";
      IntList list5;
      cout << " pushfront 10";
      list5.push_front( 10 );
      cout << " pushfront 20";
      list5.push_front( 20 );
      cout << " pushfront 10";
      list5.push_front( 10 );
      cout << " pushfront 30";
      list5.push_front( 30 );
      cout << " list5: ";
      list5.display();
      cout << " remove_duplicates()";
      list5.remve_duplicates();
      cout << " list5: ";
      list5.display();
      cout << " pushfront 10";
      list5.push_front( 10 );
      cout << " list5: ";
      list5.display();
      cout << " remove_duplicates()";
      list5.remve_duplicates();
      cout << " list5: ";
      list5.display();
      cout << " pushfront 20";
      list5.push_front( 20 );
      cout << " list5: ";
      list5.display();
      cout << " remove_duplicates()";
      list5.remve_duplicates();
      cout << " list5: ";
      list5.display();
      cout << " pushfront 20";
      list5.push_front( 20 );
      cout << " list5: ";
      list5.display();
      cout << " remove_duplicates()";
      list5.remve_duplicates();
      cout << " list5: ";
      list5.display();
      cout << " pushfront 20";
      list5.push_front( 20 );
      cout << " pushfront 20";
      list5.push_front( 20 );
      cout << " list5: ";
      list5.display();
      cout << " remove_duplicates()";
      list5.remve_duplicates();
      cout << " list5: ";
      list5.display();
      cout << " remove_duplicates()";
      list5.remve_duplicates();
      cout << " list5: ";
      list5.display();
      cout << " pop";
      list5.pop_front();
      cout << " pop";
      list5.pop_front();
      cout << " push_front(30)";
      list5.push_front(30);
      cout << " list5: ";
      list5.display();
      cout << " remove_duplicates()" << flush;
      list5.remve_duplicates();
      cout << " list5: " << flush;
      list5.display();
      cout << " push_front(30)";
      list5.push_front(30);
      cout << " push_front(30)";
      list5.push_front(30);
      cout << " list5: ";
      list5.display();
      cout << " remove_duplicates()" << flush;
      list5.remve_duplicates();
      cout << " list5: " << flush;
      list5.display();
      cout << " remove_duplicates()" << flush;
      list5.remve_duplicates();
      cout << " list5: " << flush;
      list5.display();
      cout << " pop";
      list5.pop_front();
      cout << " list5: ";
      list5.display();
      cout << " remove_duplicates()" << flush;
      list5.remve_duplicates();
      cout << " list5: " << flush;
      list5.display();
      cout << endl;
   }
   if (test == 5) {
      cout << "list5 destructor called" << endl;
   }
}