In: Computer Science
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();
}
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