Question

In: Computer Science

Can someone please tell me on how can I have a double and character as one...

Can someone please tell me on how can I have a double and character as one of the items on my list? Thanks!
This is what I got so far and the values I am getting are all integers.

#pragma once
#include <iostream>

class Node
{
public:
   int data;
   Node* next;       // self-referential

   Node()
   {
       data = 0;
       next = nullptr;
   }

   Node(int value)
   {
       data = value;
       next = nullptr;
   }

   ~Node()
   {
       if (next != nullptr)
           delete next;

       // DEBUG:
       std::cout << "Deleting " << data << std::endl;

   }
};

#include <iostream>
using namespace std;
#include "Node.h"


class LinkedList
{
private:
Node* first;
public:
LinkedList()
{
first = new Node();
}

~LinkedList()
   {
       delete first;
   }

   bool isEmpty()
   {
       return first->next == nullptr;
   }

   void addFront(int value)
   {
       Node* newNode = new Node(value);
       newNode->next = first->next;
       first->next = newNode;
   }

   void addBack(int value)
   {
       Node* t = first; // traversal pointer

       // find the end of the list
       while (t->next != nullptr)
           t = t->next;

       // attach new node
       t->next = new Node(value);
   }

   void removeFront()
   {
       Node* t = first->next;

       first->next = t->next;

       t->next = nullptr;
       delete t;
   }

   void removeBack()
   {
       if (isEmpty()) return;

       Node* trailer = first;
       Node* trav = first->next;

       while (trav->next != nullptr)
       {
           trav = trav->next;
           trailer = trailer->next;
       }

       trailer->next = nullptr;
       delete trav;
   }

   void display()
   {
       Node* t = first->next;

       while (t != nullptr)
       {
           std::cout << t->data << " ";
           t = t->next;
       }
       std::cout << std::endl;
   }

   Node* find(int value)
   {
       Node* t = first->next;

       while (t != nullptr)
       {
           if (t->data == value)
               return t;
           t = t->next;
       }

       return nullptr;
   }


   void duplicate()
   {
       Node* temp = first->next;

       while (temp != nullptr)
       {
           int key = temp->data;
           Node* trailer = temp;
           Node* searchPtr = temp->next;
           while (searchPtr != nullptr)
           {
               if (searchPtr->data == key)
               {
                   trailer->next = searchPtr->next;
                   searchPtr->next = nullptr;
                   delete searchPtr;
                   searchPtr = trailer->next;
               }
               else
               {
                   trailer = trailer->next;
                   searchPtr = searchPtr->next;
               }
           }
           temp = temp->next;
       }
   }


};

template <class T>
class Data
{
private:
   T value;

public:
   T get()
   {
       return value;
   }

   void set(T data)
   {
       value = data;
   }
};

int main()
{
   LinkedList list;

   Data<int> intData;
   Data<double> doubleData;
   Data<char> charData;
  

   intData.set(25);
   doubleData.set(14.82);
   charData.set('K');
  

   if (list.isEmpty()) cout << "List is empty.\n";
   else cout << "List contains data.\n";
  
   for (int x = 0; x < 2; x++)
   {
           list.addFront(intData.get());
           list.addBack(charData.get());
           list.addFront(doubleData.get());
   }
  

   if (list.isEmpty()) cout << "List is empty.\n";
   else cout << "List contains data.\n";

   cout << "List contents: ";
   list.display();

   cout << "\nFind a value\n";
   cout << "Enter a value to search: ";
   int key;
   cin >> key;

   Node* p = list.find(key);
   if (p == nullptr)
       cout << "Did not find " << key << endl;
   else
       cout << "Found " << key << " at " << p << endl << endl;

   cout << "Removing duplicates...\n";
   list.duplicate();

   list.display();

   cout << endl << "Removing front...\n";
   list.removeFront();
   list.display();

   cout << "\nRemoving back...\n";
   list.removeBack();

   list.display();
}

Solutions

Expert Solution

Hi

Step 1 : Please add template keyword above the class Node as shown

Step 2 : change the data type from int to T, as done below

template <typename T>

class Node
{
public:
   T data;
   Node* next;       // self-referential

   Node()
   {
       data = 0;
       next = nullptr;
   }

   Node(T value)
   {
       data = value;
       next = nullptr;
   }

   ~Node()
   {
       if (next != nullptr)
           delete next;

       // DEBUG:
       std::cout << "Deleting " << data << std::endl;

   }
};

Step 3: add template <typename T> above

void addFront(int value)

void addBack(int value)

void addBack(int value)

Step4: Replace the calls to above functions with template calls passing data types

I hope you got the information, kindly let me know for any clarifications

Thanks


Related Solutions

I have to complete a template for pathophysiology , can someone tell me the pathophysiology of...
I have to complete a template for pathophysiology , can someone tell me the pathophysiology of pain. This is for a pathophysiology class
please show me or tell me a trick, on how can i tell right away when...
please show me or tell me a trick, on how can i tell right away when a characteristics equation(system) is 1)overdamped 2)underdamped 3)critically damped 4) nonlinear show each an example write neatly!!!!!
Can someone please tell me why I am getting errors. I declared the map and it's...
Can someone please tell me why I am getting errors. I declared the map and it's values like instructed but it's telling me I'm wrong. #include <iostream> #include <stdio.h> #include <time.h> #include <chrono> #include <string> #include <cctype> #include <set> #include <map> #include "d_state.h" using namespace std; int main() { string name; map<string,string> s; map<string,string>::iterator it; s[“Maryland”] = "Salisbury"; s[“Arizona”] = "Phoenix"; s[“Florida”] = "Orlando"; s[“Califonia”] = "Sacramento"; s[“Virginia”] = "Richmond"; cout << "Enter a state:" << endl; cin >> name;...
Okay, can someone please tell me what I am doing wrong?? I will show the code...
Okay, can someone please tell me what I am doing wrong?? I will show the code I submitted for the assignment. However, according to my instructor I did it incorrectly but I am not understanding why. I will show the instructor's comment after providing my original code for the assignment. Thank you in advance. * * * * * HourlyTest Class * * * * * import java.util.Scanner; public class HourlyTest {    public static void main(String[] args)     {        ...
can someone tell me if i am wrong on any of these???? THANKS In order to...
can someone tell me if i am wrong on any of these???? THANKS In order to be able to deliver an effective persuasive speech, you need to be able to detect fallacies in your own as well as others’ speeches. The following statements of reasoning are all examples of the following fallacies: Hasty generalization, mistaken cause, invalid analogy, red herring, Ad hominem, false dilemma, bandwagon or slippery slope. 1. __________bandwagon fallacy_______ I don’t see any reason to wear a helmet...
Can someone please tell me the negative aspects of dunaliella salinasure (microbiology) in lamens terms please...
Can someone please tell me the negative aspects of dunaliella salinasure (microbiology) in lamens terms please , please also provide as much detail as possible I will rate thank you  
Can someone please tell me if these calculations are correct! I'm reviewing my notes, and my...
Can someone please tell me if these calculations are correct! I'm reviewing my notes, and my professor said to always multiply the lipids by 3 and then divide by 7 to get the total amount of cals of lipids per day... I'm not completely sure why you do that? Can someone explain. Why don't you just stop at 700 cals for lipids? 1. Calculate the number of calories and grams protein for the following TPN solution: D50W in 500cc 10%...
Can someone please explain the edgeworth box to me by typing in a way i can...
Can someone please explain the edgeworth box to me by typing in a way i can see and understand? what determines the contract curves path?
CAN SOMEONE WRITE THIS PAPER FOR ME PLEASE, I DON'T KNOW HOW TO DRIVE RECOMMENDATION REPORT,...
CAN SOMEONE WRITE THIS PAPER FOR ME PLEASE, I DON'T KNOW HOW TO DRIVE RECOMMENDATION REPORT, PLEASE HELP ME !!!!!! RECOMMENDATION REPORT ON HONDA ACCORD ( WORD 250)
Can someone tell me the complications and safety for hemorrhagic stroke? this is for a pathophysiology...
Can someone tell me the complications and safety for hemorrhagic stroke? this is for a pathophysiology class
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT