Homework Help Question & Answers

C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int...

C++ program:

Convert the classes to template classes

#include <iostream>
#include <string>
using namespace std;
class Node {
  private:
    int data;
    Node* next;
  public:
    Node(int data) {  
      this->data=data;
      this->next = 0;
    }
    int getData(){return data;}
    Node* getNext(){return next;}
    void setNext(Node* next){this->next=next;}
};
class LinkedList {
  private:
    Node* head = 0;
  public:
    int isEmpty() {return head == 0;}
    void print() {
      Node* currNode = head;      
      while(currNode!=0) {
        cout << currNode->getData() << endl;
        currNode = currNode->getNext();
      }
    }
    void append(int data) {
      if(head == 0)
        head = new Node(data);
      else{
        Node* lastNode = head;
        while(lastNode->getNext()!=0)
          lastNode = lastNode->getNext();

        Node* newNode = new Node(data);
        lastNode->setNext(newNode);
      }
    }
    void recursiveDeleteAll() {
      recursiveDeleteAll(head);
      head=0;
    }
    void recursiveDeleteAll(Node* currNode){
      if(currNode != 0){
        recursiveDeleteAll(currNode->getNext());
        delete currNode;
      }
    }
    ~LinkedList() {
      recursiveDeleteAll();
/*
      Node* nodePtr = head;
      while(head!=0) {
        head = head->getNext();
        delete nodePtr;
        nodePtr = head;
      }
*/
    }
};
int main() {
  LinkedList myList;
  myList.append(99);
  myList.append(4);
  myList.append(0);
  myList.append(5);
  myList.print();
}
0 0
Next > < Previous
ReportAnswer #1
private: 8 9 10 clang version 7.8.0-3-ubuntue.18.84.1 (tags/RELEASE 7e8/final) T data; Node next; clang++-7 -pthread -o ain m

#include <iostream>
#include <string>

using namespace std;

template<class T>
class Node {
  private:
    T data;
    Node* next;
  public:
    Node(T data) {  
      this->data=data;
      this->next = 0;
    }
    T getData(){return data;}
    Node* getNext(){return next;}
    void setNext(Node* next){this->next=next;}
};

template<class T>
class LinkedList {
  private:
    Node<T>* head = 0;
  public:
    int isEmpty() {return head == 0;}
    void print() {
      Node<T>* currNode = head;      
      while(currNode!=0) {
        cout << currNode->getData() << endl;
        currNode = currNode->getNext();
      }
    }
    void append(T data) {
      if(head == 0)
        head = new Node<T>(data);
      else{
        Node<T>* lastNode = head;
        while(lastNode->getNext()!=0)
          lastNode = lastNode->getNext();

        Node<T>* newNode = new Node<T>(data);
        lastNode->setNext(newNode);
      }
    }
    void recursiveDeleteAll() {
      recursiveDeleteAll(head);
      head=0;
    }
    void recursiveDeleteAll(Node<T>* currNode){
      if(currNode != 0){
        recursiveDeleteAll(currNode->getNext());
        delete currNode;
      }
    }
    ~LinkedList() {
      recursiveDeleteAll();
/*
      Node* nodePtr = head;
      while(head!=0) {
        head = head->getNext();
        delete nodePtr;
        nodePtr = head;
      }
*/
    }
};

int main() {
  LinkedList<int> myList;
  myList.append(99);
  myList.append(4);
  myList.append(0);
  myList.append(5);
  myList.print();
}


Please upvote, as i have given the exact answer as asked in question. Still in case of any issues in code, let me know in comments. Thanks!
Add Homework Help Answer
Add Answer of:
C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
More Homework Help Questions Additional questions in this topic.
Need Online Homework Help?
Ask a Question
Get FREE Expert Answers
WITHIN MINUTES
Related Questions