Question

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
Add a comment Improve this question Transcribed image text
Answer #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 a comment
Know the answer?
Add Answer to:
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:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node();...

    // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node(); Node(int entry); int getEntry() const; void setEntry(int entry); Node *getNext(); void setNext(Node *next); }; #endif // Node.cpp #include "Node.h" Node::Node() { m_entry = 0; m_next = nullptr; } Node::Node(int entry) { m_entry = entry; } int Node::getEntry() const { return m_entry; } void Node::setEntry(int entry) { m_entry = entry; } Node *Node::getNext() { return m_next; } void Node::setNext(Node *next) { m_next = next; }...

  • #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:       ...

    #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:        int data;        Node* nextNodePtr;           public:        Node(){}               void setData(int d){            data = d;        }               int getData(){            return data;        }               void setNextNodePtr(Node* nodePtr){                nextNodePtr = nodePtr;        }                ...

  • CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare...

    CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node {    char c;    Node *next; }; class LinkedString {    Node *head; public:    LinkedString();    LinkedString(char[]);    LinkedString(string);    char charAt(int) const;    string concat(const LinkedString &obj) const;    bool isEmpty() const;    int length() const;    LinkedString substring(int, int) const;    //added helper function to add to linekd list private:    void add(char c); };...

  • // Code is buggy. Fix bugs. Do NOT re-write the whole code! #include <iostream> using namespace...

    // Code is buggy. Fix bugs. Do NOT re-write the whole code! #include <iostream> using namespace std; class LinkedList; class Node { private: string data; Node* next; public: Node(string s, Node* n); string getData() {return data;} Node* getNext() {return next;} } Node::Node(string s, Node* n) { data = s; next = n; } class LinkedList { private: Node* head; public: LinkedList(); bool insert(string s); friend ostream& operator<<(ostream& os, LinkedList l); }; LinkedList::LinkedList() : head(nullptr) { } bool LinkedList::insert(string s) {...

  • Using the provided Linked List template, add the following recursive functions and demonstrate them in a...

    Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...

  • #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {...

    #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {     Contact item;     NodePtr next;     friend class ContactList; }; class ContactList { public:     ContactList(char* clfile) ;     ~ContactList();     void display       (ostream & output) const;     int   insert        (Contact record_to_insert);     int   insert        (ContactList contact_list);     int   remove        (Contact record_to_delete);     int   size          () const;     int   save          () const;     void find_by_lname (ostream & output, string lname) const;     void...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT