Question

In: Computer Science

Please solve in C++ only class is not templated You need to write a class called...

Please solve in C++ only

class is not templated

You need to write a class called LinkedList that implements the following List operations:

public void add(int index, Object item);
// adds an item to the list at the given index, that index may be at start, end or after or before the

// specific element

2.public void remove(int index);

// removes the item from the list that has the given index

3.public void remove(Object item);

// finds the item from list and removes that item from the list

4.public List duplicate();

// creates a duplicate of the list

// postcondition: returns a copy of the linked list

5.public List duplicateReversed();

// creates a duplicate of the list with the nodes in reverse order

// postcondition: returns a copy of the linked list with the nodes in

6.public List ReverseDisplay();

//print list in reverse order

7.public Delete_Smallest();

// Delete smallest element from linked list

8.public List Delete_duplicate();

// Delete duplicate elements from a given linked list.Retain the earliest entries.

9 Make a function that adds a linked list to itself at the end.

Input:

4 -> 2 -> 1 -> NULL

Output:

4 -> 2 -> 1 -> 4 -> 2 -> 1 -> NULL

note : code should work on Visual studio 2017 and provide screenshot of output

Solutions

Expert Solution

C++ code is:

#include <iostream>

using namespace std;

struct node
{
    int data;
    node *next;
};

class linked_list
{
private:
    node *head;
    int size;
public:
    linked_list()
    {
        head = NULL;
    //    tail = NULL;
        size=0;
    }

    void add(int index,int item) {
        if(index < 0 or index>size) cout << "Invalid position!" << endl; 

        else {
          node* tmp = new node();
          tmp->data = item;
          tmp->next = NULL;
          if(index==0){
            tmp->next = head;
            head = tmp;
          }else{
            node* curr = head;
           int i=0;
           while(i<index-1){
              curr=curr->next;
              i++;
           }
           tmp->next = curr->next;
           curr->next = tmp;
          }
          size++;
          }
        }

      void remove(int index){
        if(head==nullptr)return;
        node* curr = head;
        if(index==0){
          head = head->next;
          free(curr);
          return;
        }
         for (int i=0; curr!=NULL && i<index-1; i++) 
         curr = curr->next; 

    if (curr == NULL || curr->next == NULL) 
         return; 

    node *next = curr->next->next; 
    free(curr->next); 
    curr->next = next;
      }

      //HERE I have used int as item data type, since there is sim with above method, do change the data type of int to delete by index
      void removeItem(int item){
        node* curr = head;
        if(head->data==item){
            head = head->next;
          free(curr);
          return;
        }else{
            while(curr->next->data!=item)curr=curr->next;
            node* f = curr->next;
            curr->next = f->next;
            free(f);
            return;
        }
      }

       linked_list duplicate(){
         linked_list newlinklist ;
          node* curr = head;
          int i=0;
          while(curr->next!=nullptr){
              newlinklist.add(i++,curr->data);
              curr=curr->next;
             
          }
          newlinklist.add(i++,curr->data);
          return newlinklist;
       }

        linked_list duplicateReversed(){
           linked_list newlinklist ;
          node* curr = head;
          while(curr->next!=nullptr){
              newlinklist.add(0,curr->data);
              curr=curr->next;
             
          }
          newlinklist.add(0,curr->data);
          return newlinklist;
        }

       void ReverseDisplay(){
          node* current = head; 
        node *prev = NULL, *next = NULL; 
  
        while (current != NULL) { 
            next = current->next; 
            current->next = prev;
            prev = current; 
            current = next; 
        } 
        head = prev;
        println();
      }
      
      void Delete_Smallest(){
        int x = head->data;
        node* curr = head;
        while(curr!=nullptr){
          if(curr->data<x)x=curr->data;
          curr=curr->next;
        }
        removeItem(x);
      }

      void Delete_duplicate(){
        node *ptr1, *ptr2, *dup; 
        ptr1 = head; 
         while (ptr1 != NULL && ptr1->next != NULL) 
        { 
        ptr2 = ptr1; 
        while (ptr2->next != NULL) 
        { 
            if (ptr1->data == ptr2->next->data) 
            { 
                dup = ptr2->next; 
                ptr2->next = ptr2->next->next; 
                delete(dup); 
            } 
            else 
                ptr2 = ptr2->next; 
        } 
        ptr1 = ptr1->next; 
       } 
      }

      void add_linkedlist(){
        linked_list samelink = duplicate();
        node* curr = head;
        while(curr->next!=nullptr)curr=curr->next;
        curr->next = samelink.head;
      }

      void println(){
         node* curr = head;
        while(curr!=nullptr){
          cout<<curr->data<<" --> ";
          curr=curr->next;
        }
        cout<<endl;
      }
};

int main(){
  linked_list l;
  l.add(0,12);
  l.add(1,24);
  l.add(2,45);
  l.add(2,32);
  l.add(4,55);
  l.add(3,11);
  l.add(3,45);
  l.println();
  l.remove(0);
  l.println();
  l.removeItem(11);
  l.println();
  linked_list news;
  news = l.duplicate();
   news.println();
 // l.duplicate().println();
  linked_list rev_news = news.duplicateReversed();
  rev_news.println();
   rev_news.Delete_Smallest();
  rev_news.println();
   rev_news.Delete_duplicate();
   rev_news.println();

  return 0;
}

Output:


Related Solutions

This question is in C++ Problem 3 Write a templated C++ class to represent an itinerary....
This question is in C++ Problem 3 Write a templated C++ class to represent an itinerary. An itinerary has a title, a source position and a destination position. In addition, it may include intermediate positions. All positions have the same abstract type T. The real type can be decided later, for example it can be a cartesian point with x,y,z coordinates, or an airport code, or a city name, or a country name, etc .. The itinerary includes a vector...
Please solve questions in C++ ASAP!! thank you (a) Write a function in C++ called readNumbers()...
Please solve questions in C++ ASAP!! thank you (a) Write a function in C++ called readNumbers() to read data into an array from a file. Function should have the following parameters: (1) a reference to an ifstream object (2) the number of rows in the file (3) a pointer to an array of integers The function returns the number of values read into the array. It stops reading if it encounters a negative number or if the number of rows...
C# please! A friend wants you to start writing a video game. Write a class called...
C# please! A friend wants you to start writing a video game. Write a class called “Player” that includes the location of the player, her/his current score, and the ancestry of the player (e.g. “Elf”, “Goblin”, “Giant”, “Human”). A player will always start at location (0, 0) and have a score of 0. Always. Write accessors/modifiers (or “setters/getters”) for each of those characteristics. Write an “appropriate” constructor for the class (based on what’s described above). Write methods moveNorth(), moveSouth(), moveEast()...
(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an...
(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an integer n and returns 1 if n is a perfect number, otherwise it will return 0. If the sum of a number’s proper divisors are equal to the number, than the number is called a perfect number. For example, 6 is a perfect number: 6=1+2+3.
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
Please solve using jupyter notebook . 10.10- (Invoice Class) Create a class called Invoice that a...
Please solve using jupyter notebook . 10.10- (Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as data attributes—a part number (a string), a part description (a string), a quantity of the item being purchased (an int) and a price per item (a Decimal). Your class should have an __init__ method that initializes the four data attributes....
FOR C++ A friend wants you to start writing a video game. Write a class called...
FOR C++ A friend wants you to start writing a video game. Write a class called “Player” that includes the location of the player, her/his current score, and the ancestry of the player (e.g. “Elf”, “Goblin”, “Giant”, “Human”). A player will always start at location (0, 0) and have a score of 0. Write accessors/modifiers (or “setters/getters”) for each of those characteristics. Write an “appropriate” constructor for the class (based on what’s described above). Write methods moveNorth(), moveSouth(), moveEast() and...
C++ Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that...
C++ Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that will allow creation and handling of univariate monomials, as described below. Monomial Description Each monomial object has following properties: Each monomial has a coefficient and a power. Monomial power is always positive. Monomial power is always integer. Monomials cannot be added if they have different powers Class Details The single constructor for the Monomial class should have 2 parameters: an floating point coefficient value...
In C++, implement a class called setOper that provides several simple set operations. The class only...
In C++, implement a class called setOper that provides several simple set operations. The class only needs to deal with sets that are closed intervals specified by two real numbers; for example, the pair (2.5, 4.5) represent the interval [2.5, 4.5]. The following operations should be supported: - Check if the value x belongs to the given interval. - Check if the value x belongs to the intersection of two intervals. - Check if the value x belongs to the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT