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

write a c++ code and it has to work on linux machine so no conio.h....etc Let’s

ID: 3845142 • Letter: W

Question

write a c++ code and it has to work on linux machine so no conio.h....etc

Let’s design a class “linked_list” that can store a list of integers. For example: 22(head)->10->7->13->

Each node contains a numeric value and a pointer to the next node. The class should at least have the following member functions:

1. Insert an element

2. Delete an element (delete by value)

3. Search an element (search by value)

4. Print the list

Now, design another class “sort_my_list” that has a linked list member variable. Now, add a function, local_sort(), to sort the list in increasing order. You can choose to implement either one of the following three algorithms discussed in class:

Selection Sort Insertion Sort Merge Sort

Remember: you cannot copy the linked list into an array/vector while you sort it.

Create a file “program_02.cpp” that contains the main function. In your main, create an object of “sort_my_list” class. Define a menu to insert items in your list, delete items from the list, and print the list. Please carefully note that, when you print the list, we expect to see a sorted list of integers.

Explanation / Answer

#include<bits/stdc++.h>
using namespace std;

class linked_list{
   public:
struct node{
   int num;
   struct node* next;
};

struct node* head = NULL;
struct node* temp1 = NULL;
struct node* temp2 = NULL;
struct node* min = NULL;
void insert(int n){
   struct node* temp = (struct node*)(malloc(sizeof(struct node*)));
   temp->num = n;
   temp->next = head;
   head = temp;
}
void deleteNode(int n){
   struct node* temp = head;
   if(head->num == n){
       head = head->next;
   }
  
   else{
       while(temp->next->num != n){
           temp = temp->next;
       }
       temp->next = temp->next->next;
   }
}
void search(int n){
   struct node* temp = head;
   while(temp != NULL){
       if(temp->num == n){
           cout<<"Number Found"<<endl;
           break;
       }
       temp = temp->next;
   }
}
void printList(){
   struct node* temp = head;
   while(temp!=NULL){
       cout<<temp->num<<" ";
       temp = temp->next;
   }
   cout<<endl;
}


};


class sort_my_list{
   public:
   linked_list l1;
   sort_my_list(linked_list l3){
       l1 = l3;
   }
   void local_sort(linked_list l1){
   int buffer;
   l1.temp1 = l1.head;
   l1.temp2 = l1.head;
   while(l1.temp1 != NULL){
       l1.min = l1.temp1;
       l1.temp2 = l1.temp1;
       while(l1.temp2 != NULL){
           if(l1.min->num > l1.temp2->num){
               l1.min = l1.temp2;
           }
          
           l1.temp2 = l1.temp2->next;
       }
       buffer = l1.temp1->num;
       l1.temp1->num = l1.min->num;
       l1.min->num = buffer;
       l1.temp1 = l1.temp1->next;
   }
}
};


int main(){
   linked_list l1;
   sort_my_list s1(l1);
   int num = 1;
   int number;
   while(num){
       cout<<"Enter the Operations you wish to perform"<<endl;
       cout<<"Press 0 for Exit"<<endl;
       cout<<"Press 1 for Insert"<<endl;
       cout<<"Press 2 for Delete"<<endl;
       cout<<"Press 3 for Search"<<endl;
       cout<<"Press 4 for Print"<<endl;
       cout<<"Press 5 for SortLocal"<<endl;
       cin>>num;
       if(num == 1){
           cout<<"Enter a number"<<endl;
           cin>>number;
           l1.insert(number);
       }
       else if(num == 2){
           cout<<"Enter a number"<<endl;
           cin>>number;
           l1.deleteNode(number);
       }
       else if(num == 3){
           cout<<"Enter a number"<<endl;
           cin>>number;
           l1.search(number);
       }
       else if(num == 4){
           l1.printList();
       }
       else if(num == 5){
           s1.local_sort(l1);
       }
      
   }
   /*l1.insert(13);
   l1.insert(7);
   l1.insert(10);
   l1.insert(22);
   l1.printList();
   l1.search(13);
  
   sort_my_list s1(l1);
   s1.local_sort();
   l1.printList();*/
   return 0;
}